kurukuru-papaのブログ

主に、ソフトウェア開発に関連したメモを書き溜めたいと思います。

Macのメタ情報(EA)を除去するAppleScriptを作ってみました

Mac OS XのTigerからEA(Extended Attributes)というメタデータ機能が追加されたようですが、このEAと相性が悪いアプリケーションがあったりするような感じがしています。そこで、ファイルのドラッグ&ドロップで、EAを除去するスクリプトを作成しましたのでご紹介します。

使い方

下記に記述したダウンロードページから、ZIPファイルをダウンロードして下さい。ダウンロードしたZIPファイルを展開すると「ClearEa.app」(Finderの設定によっては拡張子appが非表示になっているかもしれません。)というファイルがありますので、任意の場所に配置して下さい。

ClearEa.appと、EAを除去したい対象ファイルをFinderで表示しておき、対象ファイルをドラッグしClearEa.appへドロップするだけで、EAが除去されます。複数ファイルやフォルダをドラッグ&ドロップすることも可能です。

ダウンロード

次のページから、ClearEa-XXX.zip(XXXはバージョン番号)をダウンロードして下さい。

AppleScriptの中身

ドロップされたファイルを、標準コマンドのxattrへ渡しているだけです。

property appName : "ClearEaツール"
property dropFlag : false

--ダイアログボックスを表示する
on showDialog(msg)
	display dialog msg buttons {"OK"} default button 1 with icon 1 with title appName
end showDialog

--ドラッグ&ドロップで実行
on open of dropFiles
	set dropFlag to true
	set command to ""
	
	try
		tell application "Finder"
			--コマンド文字列を作成する
			set pathArgs to ""
			repeat with dropFile in dropFiles
				set pathArgs to pathArgs & " " & quoted form of POSIX path of dropFile
			end repeat
			set command to "xattr -cr " & pathArgs
			
			--拡張情報の除去を実施する
			do shell script command
		end tell
		
		--完了メッセージ
		showDialog("完了しました。")
		
	on error errMsg number errNo
		--エラーメッセージを表示する
		showDialog("実行に失敗しました。" & return & "command=" & command & return & "errMsg=" & errMsg & return & "errNo=" & errNo)
	end try
end open

--ファイルがドロップされていなかった場合使い方を表示する
if not dropFlag then
	showDialog("起動方法が違います。" & return & "当アプリケーションファイルに対象ファイルをドラッグ&ドロップしてください。")
end if

EAの確認方法

ファイルに付加されているEAは、ターミナルからlsやxattrというコマンドで確認することができます。

lsコマンドでは、オプション「-l@」をつけることでEAを確認できます。次の例のように、ファイル属性の末尾に「@」が付加されることで何かしらのEAが付いていることを確認でき、com.apple.FinderInfoというEAが32Byte、com.apple.TextEncodingが15Byte付いていることがわかります。

$ ls -l@
total 8
-rw-r--r--@ 1 hiro  staff   10  3 11 21:33 test.txt
	com.apple.FinderInfo	 32 
	com.apple.TextEncoding	 15 

xattrコマンドのオプション「-l」を使うとEAの中身も確認することができます。次のようにバイナリコードとASCII表示が出力されます。

$ xattr -l test.txt
com.apple.FinderInfo:
00000000  54 45 58 54 63 45 64 31 00 00 00 00 00 00 00 00  |TEXTcEd1........|
00000010  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  |................|
00000020
com.apple.TextEncoding: utf-8;134217984

xattrコマンドではEAを除去することも出来ます。次の例のようにして、オプション「-c」でEAをすべてクリア、オプション「-d」で特定のEAのみを除去できます。なお、上述のスクリプトでもこれらのコマンドを使用しています。

$ xattr -c test.txt
$ ls -l test.txt
-rw-r--r--  1 hiro  staff  10  3 11 21:33 test.txt
$ ls -l@ test.txt
-rw-r--r--@ 1 hiro  staff  10  3 11 22:01 test.txt
	com.apple.FinderInfo	32 
	com.apple.TextEncoding	15 
$ xattr -d com.apple.FinderInfo test.txt
$ ls -l@ test.txt
-rw-r--r--@ 1 hiro  staff  10  3 11 22:01 test.txt
	com.apple.TextEncoding	15 

動作確認環境