applescript:刪除 USB 隨身碟的 .* 檔案

本區專門提供 Mac OS X 使用者交換使用心得

版主: ross_ttbryanchangdigdogchester

內容
發表人
留言五百如一日
文章: 610
註冊時間: 12/25/2001 1:01 am

applescript:刪除 USB 隨身碟的 .* 檔案

#1 文章 »

Mac OS X 在掛載某些檔案系統時;為了記錄檔案資訊,會產生很多 .* 檔案。

例:

/Volumes/ABEILLE/.DS_Store
/Volumes/ABEILLE/._.DS_Store
/Volumes/ABEILLE/港自殺率高於全球平均.html
/Volumes/ABEILLE/._港自殺率高於全球平均.html
/Volumes/ABEILLE/自殺.html
/Volumes/ABEILLE/._自殺.html
/Volumes/ABEILLE/青少年對「自殺行為」的態度與看法.html
/Volumes/ABEILLE/._青少年對「自殺行為」的態度與看法.html

其中 ABEILLE 為 USB 隨身碟的名稱。若此隨身碟拿到 PC 上 ,會看到很多 .* 檔案,令人煩惱。

刪除這些 .* 檔案的 shell command 為:

代碼: 選擇全部

$ find pathToVolume -name '.*' -exec rm -f {} \;
其中 pathToVolume 為檔案系統的絕對路徑;若掛載點為 /Volumes,循上例:

代碼: 選擇全部

$ find /Volumes/ABEILLE -name '.*' -exec rm -f {} \;
若不欲每次輸入指令,卸載檔案系統前可執行這 [url=applescript://com.apple.scripteditor/?action=new&script=tell%20application%20%22System%20Events%22%20to%20set%20targetDisk%20to%20name%20of%20disks%20whose%20ejectable%20is%20true%20and%20not%20%28format%20is%20audio%20format%29%0D%0Drepeat%20with%20i%20in%20targetDisk%0D%09set%20chosenDisk%20to%20quoted%20form%20of%20POSIX%20path%20of%20%28contents%20of%20i%29%0D%09set%20cmd%20to%20%28%22diskutil%20info%20%22%20as%20Unicode%20text%29%20%26%20chosenDisk%20%26%20%22%20%7C%20grep%20Protocol%22%0D%09if%20word%20%2D1%20of%20%28do%20shell%20script%20cmd%29%20%3D%20%22USB%22%20then%0D%09%09try%20%2D%2D%20delete%20%2E%2A%20files%0D%09%09%09set%20dcmd%20to%20%28%22find%20%22%20as%20Unicode%20text%29%20%26%20chosenDisk%20%26%20%22%20%2Dname%20%27%2E%2A%27%20%2Dexec%20rm%20%2Df%20%7B%7D%20%5C%5C%3B%22%0D%09%09%09do%20shell%20script%20dcmd%0D%09%09end%20try%0D%09end%20if%0Dend%20repeat%0Dsay%20%22done%22%0D]applescript[/url];會自行偵測 可退出、使用 USB 的卷宗,並刪除所有.* 檔案。

代碼: 選擇全部

tell application "System Events" to set targetDisk to name of disks whose ejectable is true and not (format is audio format)

repeat with i in targetDisk
	set chosenDisk to quoted form of POSIX path of (contents of i)
	set cmd to ("diskutil info " as Unicode text) & chosenDisk & " | grep Protocol"
	if word -1 of (do shell script cmd) = "USB" then
		try -- delete .* files
			set dcmd to ("find " as Unicode text) & chosenDisk & " -name '.*' -exec rm -f {} \\;"
			do shell script dcmd
		end try
	end if
end repeat
say "done"

頭像
janusng
嗜冰客
文章: 1048
註冊時間: 04/30/2001 1:01 am
來自: Metropolis Asylum
聯繫:

#2 文章 janusng »

謝謝悲兄介紹! :D

可惜的是所有 Firewire Hard disk 的 ejectable property 都是 false!奇怪! :?

還有似乎 find ".*"是太大意了,很多 hidden file 也是以 "." 開頭的。

轉為分開 find ".-*"、find ".DS_Store" 和 find ".Trashes" 三句,分三次執行似乎比較保險。 :wink:
圖檔
It is not god who created man. It is man who created God.

Light travels faster than sound. This is why some people appear bright until you hear them speak.
頭像
janusng
嗜冰客
文章: 1048
註冊時間: 04/30/2001 1:01 am
來自: Metropolis Asylum
聯繫:

#3 文章 janusng »

再詳細研究,小弟寫了另一個 version。
請悲兄指教指教。 :)

代碼: 選擇全部

on run
	-- get a list of non-native Apple formatted partitions
	tell application "System Events" to set targetDisk to name of disks whose (format is not Mac OS Extended format) and (format is not Mac OS format) and (format is not AppleShare format)
	
	repeat with i in targetDisk
		set chosenDisk to quoted form of POSIX path of (contents of i)
		-- ignore Network or it will have errors
		if not (chosenDisk = "'/Network'") then
			-- find out the protocol
			set cmd to ("diskutil info " as Unicode text) & chosenDisk & " | grep Protocol:"
			set prot to word -1 of (do shell script cmd)
			-- ignore ATA and SCSI drives. they can't be disconnected anyway
			if (prot ≠ "ATA") and (prot ≠ "SCSI") then
				set cmd to ("diskutil info " as Unicode text) & chosenDisk & " | grep Read\\ Only:"
				set ro to word -1 of (do shell script cmd)
				-- ignore read only partitions
				if (ro = "No") then
					cleanUp(chosenDisk)
				end if
			end if
		end if
	end repeat
	say "Disks cleaned"
end run

on cleanUp(foundDisk)
	try -- delete ._* files
		set dcmd to ("find " as Unicode text) & foundDisk & " -name '._*' -exec rm -f {} \\;"
		do shell script dcmd
	end try
	try -- delete .DS_Store folders
		set dcmd to ("find " as Unicode text) & foundDisk & " -name '.DS_Store' -exec rm -R -f {} \\;"
		do shell script dcmd
	end try
	try -- delete .Trashes folder
		set dcmd to ("find " as Unicode text) & foundDisk & " -name '.Trashes' -exec rm -R -f {} \\;"
		do shell script dcmd
	end try
	
end cleanUp
圖檔
It is not god who created man. It is man who created God.

Light travels faster than sound. This is why some people appear bright until you hear them speak.
頭像
ulysses
討論區管理員
文章: 2475
註冊時間: 05/18/2001 1:01 am
來自: Forgotten Realm
聯繫:

#4 文章 ulysses »

不行喔,執行的時候出現 Type 1 Error,
Trace 時發現是 diskutil info '/Volumes/xxxx' | grep Protocol 指令出錯,
在 Terminal 裡面測試,結果問題是出在執行 diskutil info 需要 root 權限。
ash nazg durbatuluk, ash nazg gimbatul,
ash nazg thrakatuluk agh burzum-ishi krimpatul.
頭像
janusng
嗜冰客
文章: 1048
註冊時間: 04/30/2001 1:01 am
來自: Metropolis Asylum
聯繫:

#5 文章 janusng »

問題不是權限,是 network volume 是不在 diskutil list 內。

剛剛又改寫了另一個有選擇的 AppleScript:

代碼: 選擇全部

on run
	-- get a list of Non-native Apple formatted partitions, and excluding Network
	tell application "System Events" to set potentialVolumes to name of disks whose (format is not Mac OS Extended format) and (format is not Mac OS format) and (format is not AppleShare format) and name is not "Network"
	-- ask user to choose volumes
	set chosenVolumes to (choose from list potentialVolumes with prompt "Please choose the Volume to be cleaned" with multiple selections allowed without empty selection allowed)
	if chosenVolumes is not false then
		repeat with i in chosenVolumes
			cleanUp(i)
		end repeat
		say "Disks cleaned"
	end if
end run

on cleanUp(targetVolume)
	set mountedPath to quoted form of POSIX path of (contents of targetVolume)
	try -- delete ._* files
		set dcmd to ("find " as Unicode text) & mountedPath & " -name '._*' -exec rm -f {} \\;"
		do shell script dcmd
	end try
	try -- delete .DS_Store folders
		set dcmd to ("find " as Unicode text) & mountedPath & " -name '.DS_Store' -exec rm -R -f {} \\;"
		do shell script dcmd
	end try
	try -- delete .Trashes folder
		set dcmd to ("find " as Unicode text) & mountedPath & " -name '.Trashes' -exec rm -R -f {} \\;"
		do shell script dcmd
	end try
end cleanUp
圖檔
It is not god who created man. It is man who created God.

Light travels faster than sound. This is why some people appear bright until you hear them speak.
留言五百如一日
文章: 610
註冊時間: 12/25/2001 1:01 am

#6 文章 »

可惜的是所有 Firewire Hard disk 的 ejectable property 都是 false!奇怪!


小弟的外置 firewire HD 的 ejectable 可是 true;你的系統版本是什?

還有似乎 find ".*"是太大意了,很多 hidden file 也是以 "." 開頭的。

轉為分開 find ".-*"、find ".DS_Store" 和 find ".Trashes" 三句,分三次執行似乎比較保險。
對,這樣較保險。小弟編寫時只考慮了 FAT32 的 USB 隨身碟,所有 .* 檔案皆刪之無憾。

不行喔,執行的時候出現 Type 1 Error,
Trace 時發現是 diskutil info '/Volumes/xxxx' | grep Protocol 指令出錯,
在 Terminal 裡面測試,結果問題是出在執行 diskutil info 需要 root 權限。
ulysses 兄,小弟瞎猜:

SMB volume 已 mount 時的絕對路徑為何?
若有其絕對路徑,也可不檢測,下指令刪除 .* 檔案。
若有權限問題,用 sudo,例:

代碼: 選擇全部

$ sudo find pathToSMB -name '.*' -exec rm -f {} \;
頭像
janusng
嗜冰客
文章: 1048
註冊時間: 04/30/2001 1:01 am
來自: Metropolis Asylum
聯繫:

#7 文章 janusng »

悲兄, SMB mount 回來的 volume,也是會在 /Volumes directory 內加入 alias 的。
同時在 AppleScript 中的 System Events application 傳回的 disks 中也會包含的,SMB volume 的 format 屬性是 NFS format。

小弟的是 Mac OS X 10.3.6,三個 Firewire cases,五個 partitions 的 ejectable 屬性也是 false,奇怪。連 diskutil 也回答 Ejectable: no。 :roll:

SMB mount 的 volumes 也是不會在 diskutil list 中列出的,根本不能使用 diskutil。
圖檔
It is not god who created man. It is man who created God.

Light travels faster than sound. This is why some people appear bright until you hear them speak.
留言五百如一日
文章: 610
註冊時間: 12/25/2001 1:01 am

#8 文章 »

小弟的是 Mac OS X 10.3.6,三個 Firewire cases,五個 partitions 的 ejectable 屬性也是 false,奇怪。連 diskutil 也回答 Ejectable: no。
再瞎猜:

跟 partitions 有關。

因 ejectable 與實體 disk 相對應,如 CD、DVD 或只有一個 partition 的 HD。這時,ejectable = unmountable。

如實體 disk 可有多個 partitions,只可 unmount 某一 partition,但不能只 eject 某一 partition;所以 partition 的 ejectable 是 false。

精確的,要查看 Mac OS X 如何定義 disk、volume 及 partition。
頭像
janusng
嗜冰客
文章: 1048
註冊時間: 04/30/2001 1:01 am
來自: Metropolis Asylum
聯繫:

#9 文章 janusng »

可能吧,只是 System Events 傳回的 Disks 實則是 partitions。diskutil info 問回來的也是 partition 的 info。
圖檔
It is not god who created man. It is man who created God.

Light travels faster than sound. This is why some people appear bright until you hear them speak.
留言五百如一日
文章: 610
註冊時間: 12/25/2001 1:01 am

#10 文章 »

又,/usr/bin/smbutil 轉回什麼?
頭像
janusng
嗜冰客
文章: 1048
註冊時間: 04/30/2001 1:01 am
來自: Metropolis Asylum
聯繫:

#11 文章 janusng »

smbutil 好像沒有關係吧? :?
圖檔
It is not god who created man. It is man who created God.

Light travels faster than sound. This is why some people appear bright until you hear them speak.
留言五百如一日
文章: 610
註冊時間: 12/25/2001 1:01 am

#12 文章 »

:oops: ,繼這:
SMB mount 回來的 volume,也是會在 /Volumes directory 內加入 alias 的。
同時在 AppleScript 中的 System Events application 傳回的 disks 中也會包含的,SMB volume 的 format 屬性是 NFS format。
頭像
janusng
嗜冰客
文章: 1048
註冊時間: 04/30/2001 1:01 am
來自: Metropolis Asylum
聯繫:

#13 文章 janusng »

在 Terminal 打入 smbutil 後:

代碼: 選擇全部

usage: smbutil [-hv] subcommand [args]
where subcommands are:
 crypt          slightly obscure password
 help           display help on specified subcommand
 lc             display active connections
 login          login to specified host
 logout         logout from specified host
 lookup         resolve NetBIOS name to IP address
 print          print file to the specified remote printer
 status         resolve IP address or DNS name to NetBIOS names
 view           list resources on specified host
更奇的是, lc 和 help subcommands 也是不存在的。
餘下的,也和 mounted 了的 volume 無關。 :(
圖檔
It is not god who created man. It is man who created God.

Light travels faster than sound. This is why some people appear bright until you hear them speak.
cyhuang
新生訓練中
文章: 8
註冊時間: 08/30/2004 1:57 am

#14 文章 cyhuang »

janusng 寫:問題不是權限,是 network volume 是不在 diskutil list 內。

剛剛又改寫了另一個有選擇的 AppleScript:

代碼: 選擇全部

on run
	-- get a list of Non-native Apple formatted partitions, and excluding Network
	tell application "System Events" to set potentialVolumes to name of disks whose (format is not Mac OS Extended format) and (format is not Mac OS format) and (format is not AppleShare format) and name is not "Network"
	-- ask user to choose volumes
	set chosenVolumes to (choose from list potentialVolumes with prompt "Please choose the Volume to be cleaned" with multiple selections allowed without empty selection allowed)
	if chosenVolumes is not false then
		repeat with i in chosenVolumes
			cleanUp(i)
		end repeat
		say "Disks cleaned"
	end if
end run

on cleanUp(targetVolume)
	set mountedPath to quoted form of POSIX path of (contents of targetVolume)
	try -- delete ._* files
		set dcmd to ("find " as Unicode text) & mountedPath & " -name '._*' -exec rm -f {} \\;"
		do shell script dcmd
	end try
	try -- delete .DS_Store folders
		set dcmd to ("find " as Unicode text) & mountedPath & " -name '.DS_Store' -exec rm -R -f {} \\;"
		do shell script dcmd
	end try
	try -- delete .Trashes folder
		set dcmd to ("find " as Unicode text) & mountedPath & " -name '.Trashes' -exec rm -R -f {} \\;"
		do shell script dcmd
	end try
end cleanUp
您好,我執行這段scipt時,出現「The variable potentialVolumes is not defined.」的錯誤訊息,不知道應該怎麼解決呢?
頭像
janusng
嗜冰客
文章: 1048
註冊時間: 04/30/2001 1:01 am
來自: Metropolis Asylum
聯繫:

#15 文章 janusng »

cyhuang 君,小弟在自己的 TiBook 上執行,並無出現錯誤。

不過閣下提醒了在下,應該在沒有發現合適的碟片時,不出現選擇項目,取而代之,是沒有合用的碟片提示。小弟又再改動了一點,但應該之前的 Applescript 也一樣可行。

代碼: 選擇全部

on run
	-- get a list of Non-native Apple formatted partitions, and excluding Network
	tell application "System Events" to set potentialVolumes to name of disks whose (format is not Mac OS Extended format) and (format is not Mac OS format) and name is not "Network"
	-- (format is not 《constant ****as》) 
	if length of potentialVolumes = 0 then
		display dialog "No disk requires cleaning!" buttons {"OK"} default button 1
	else
		-- ask user to choose volumes
		set chosenVolumes to (choose from list potentialVolumes with prompt "Please choose the Volume to be cleaned" with multiple selections allowed without empty selection allowed)
		if chosenVolumes is not false then
			repeat with i in chosenVolumes
				tell application "System Events" to set f to (format of disks whose name is i)
				cleanUp(i)
			end repeat
			say "Disks cleaned"
		end if
	end if
end run

on cleanUp(targetVolume)
	set mountedPath to quoted form of POSIX path of (contents of targetVolume)
	try -- delete ._* files
		set dcmd to ("find " as Unicode text) & mountedPath & " -name '._*' -exec rm -f {} \\;"
		do shell script dcmd
	end try
	try -- delete .DS_Store folders
		set dcmd to ("find " as Unicode text) & mountedPath & " -name '.DS_Store' -exec rm -R -f {} \\;"
		do shell script dcmd
	end try
	try -- delete .Trashes folder
		set dcmd to ("find " as Unicode text) & mountedPath & " -name '.Trashes' -exec rm -R -f {} \\;"
		do shell script dcmd
	end try
end cleanUp
cyhuang 君的錯誤會否是 copy-and-pase 時出現錯誤?不如試試直接下載這個 Clean Up Non Mac Disks 的 AppleScript:wink:
最後由 janusng 於 08/11/2005 12:01 am 編輯,總共編輯了 1 次。
圖檔
It is not god who created man. It is man who created God.

Light travels faster than sound. This is why some people appear bright until you hear them speak.
頭像
janusng
嗜冰客
文章: 1048
註冊時間: 04/30/2001 1:01 am
來自: Metropolis Asylum
聯繫:

#16 文章 janusng »

之前 revised 了少少,加了 droplet 功能,現在再介紹過。

按此下載

請下載後,雙響滑鼠來爆開成為「Clean Up Non Mac Disks」,閣下可以將它放到硬碟任何地方,本人提議放到 [Macintosh HD]:Applications: 之中。

共有兩個使用辦法,一個是簡單的雙響滑鼠「Clean Up Non Mac Disks」它便會找出所有可以退出,又可以寫,同時又不是用 Apple File System (HFS+、HFS、AFS)的硬碟,包括網絡上 mount 回來的硬碟。然後列個清單,讓閣下選擇。可以選擇多於一個硬碟。按下 OK 制就會替閣下清理了。完成後會用聲音告訴大家的。

另一個用法,便是把一個或多個硬碟拖到「Clean Up Non Mac Disks」上,它便會自動清理。完成後,同樣會告訴你的。

之後便可以退出那個沒有 Mac 專用 meta files 的硬碟了。

[Updated the download link]
最後由 janusng 於 10/29/2006 9:42 pm 編輯,總共編輯了 1 次。
圖檔
It is not god who created man. It is man who created God.

Light travels faster than sound. This is why some people appear bright until you hear them speak.
頭像
janusng
嗜冰客
文章: 1048
註冊時間: 04/30/2001 1:01 am
來自: Metropolis Asylum
聯繫:

#17 文章 janusng »

今日更新了為 v.1.1。

現在清理了之後,會退出硬碟,也加快了清理速度快了 50%。 :)
圖檔
It is not god who created man. It is man who created God.

Light travels faster than sound. This is why some people appear bright until you hear them speak.
頭像
ykk
基本會員
文章: 34
註冊時間: 05/01/2001 1:01 am
來自: H↑K

#18 文章 ykk »

太好了
此後使用別人的 usb thumbdrive 後不會再被投訴了
:lol:
頭像
whindang
討論區管理員
文章: 3927
註冊時間: 11/17/2004 5:06 pm
聯繫:

#19 文章 whindang »

讚讚讚~~

比findercleaner更好用呢!! 8)
頭像
進藤光
冰果室元老
文章: 3205
註冊時間: 03/16/2005 5:18 pm
來自: insoler.com
聯繫:

#20 文章 進藤光 »

真是很棒!! 用力鼓掌 =D> =D> =D> =D> =D>

請問 janusng,要如何把 AppleScript 編譯成執行程式?
N 年前我也是 Programmer,C/C++/Assembly 一把罩,現在是 Program 笨蛋....

可簡要說一下,要使用哪些工具呢?詳細步驟我自己會摸索,爬文去.....
世界初、支援RAW相片上傳、分享、學習交流的 insoler • 社群網站https://www.insoler.com
攝影書:「美食写真の達人」進藤ヒカル・著
回覆文章