2010/02/16

Remove resource fork in AppleScript

私は Twitter に参加していませんが何日か前に Google Reader で
急募: OS XのiTunesから、mp3ファイルを_リソースフォークなしに...
を見かけました。

早速 AppleScript で try

tell application "Finder"
  set src to quoted form of POSIX path of (choose folder)
  set dst to quoted form of POSIX path of (choose folder)
  set str to "ditto -v --norsrc  " & src & "  " & dst
  do shell script str
end tell

日本語名のフォルダを指定しても大丈夫でした。でもこれだけだと愛想がないのでいろいろ付け足し。

# remove-resource-forlk.scpt
tell application "Finder"
  # フォルダを選択
  set src to quoted form of POSIX path of (choose folder with prompt "リソースフォークをまるっと取り除きたいフォルダを選択してください。")
  # 保存先フォルダの選択
  set dst to quoted form of POSIX path of (choose folder with prompt "保存先であるフォルダを選択してください。")
  # コマンド文字列
  set str to "ditto -v --norsrc  " & src & "  " & dst
  if src is dst then
    set err_str to "指定したフォルダと保存先であるフォルダが同じです!異なるフォルダを選択してください。"
    display dialog err_str buttons {"OK"} default button 1 with icon 2 with title "Error"
    return
  end if
  set warn_str to "多くのファイルが含まれているフォルダを選択した場合時間がかかる(数分)場合があります。よろしいですか?"
  display dialog warn_str buttons {"OK", "Cancel"} default button 2 with icon 2 with title "Warning"
  try
  # コマンドの実行
  set res to do shell script str
  on error num
    display dialog "エラーが発生しました。" buttons {"OK"} default button 1
    return
  end try
  if res is "" then display dialog "おわりました。" buttons {"OK"} default button 1 with icon 1 with title "Success"
end tell


[用途]
フォルダに含まれるファイルからリソースフォークを削除したファイルを保存先フォルダに保存します。(もとのファイル名は維持されます。)
複数のフォルダを内包しているフォルダを指定した場合、保存先フォルダの階層は、もとのフォルダの階層を維持します。

# iTunes Music フォルダにあるミュージシャン(Artist) の名前がついたフォルダを選択すると、そこに含まれるアルバム名のついたフォルダにある音楽ファイル( 末尾に.m4a .mp3 などの名前のついたファイル)から、リソースフォークを削除したものを、保存先フォルダに保存します。
# ターミナルからコマンドを叩くことなく、操作できます。

[使い方]
Finder メニュ-> 移動-> アプリケーションを開く (Command + Shift + A でApplication フォルダ を表示)
->ユーティリティ ->AppleScript Editor.app(AppleScript エディタ.app) をダブルクリックで起動し新しいファイルを作り上記をコピー and ペーストします。

1, コマンドKey(⌘) + K でコンパイルします。
2, コマンドKey(⌘) + S で File 保存(必ずスクリプト として)
3, コマンドKey(⌘) + R で実行 です。

処理がおわると『おわりました。』と表示します。エラーが発生した場合は『エラーが発生しました。』と表示します。

複数のアルバムフォルダが含まれている ミュージシャン(Artist) のフォルダなどを選択するとファイル数にして何百曲などのファイルを処理することになります。その場合ものすごく時間がかかる(数分)こともあります。なので最初は下記の手順でテストしてみてどのくらい時間がかかるものかを体感してみてください。

[テスト手順]
(1) デスクトップに2つ新規フォルダを作りそれぞれ「SRC」「DST」と名付けておく。
(2) 「SRC」フォルダの中にリソースフォークを取り除きたいフォルダを1 つだけコピーしておく。
  その際極力ファイル数の少ないフォルダを選ぶ。
「DST」フォルダは空っぽのままにしておく。
(3) 保存しておいた AppleScript ファイルを開いてから コマンドKey(⌘) + R をおす。または「実行」ボタンを押す。
「リソースフォークをまるっと...」ときかれたら「SRC」を選ぶ。
「保存先...」 ときかれたら 「DST」を選ぶ。

処理している間はなんのメッセージも表示しません。
10 枚程のファイルを処理した場合 2,3 秒でおわります。

# 注意1:
OS X 10.5x 時代のファイルにはリソースフォークがついてるけど、Snow Leopard 時代に作ったファイルにはついてません。

# 注意2:
指定フォルダとしてミュージシャン(Artist)名のついたフォルダを指定し
保存先フォルダには適当な名前(「test」とか)のフォルダを指定し実行した場合

iTunes->詳細->”iTunes Media”フォルダを整理する
にチェックをいれてから保存先フォルダ(「test」とか)を iTunes に取り込むと本来のミュージシャン(Artist)名のついたフォルダ名に自動変換されます。
(iTunes 9.03)

[ターミナルで確認]

# % ditto -h
# % xattr -h

# 指定フォルダ: test-src
# 保存先フォルダ: test-dst


指定フォルダ内

% ls -FRl test-src/
total 0
drwx------  3 foo  staff  102  2 15 16:35 Makana/
drwx------  3 foo  staff  102  2  4 11:48 平沢 進/

test-src//Makana:
total 0
drwx------  17 foo  staff  578  2 15 16:33 Different Game/

test-src//Makana/Different Game:
total 92824
-rw-------@ 1 foo  staff  4428672  6  6  2009 01 Away.m4p
-rw-------@ 1 foo  staff   623834  2  4 18:09 02 Interlude I.m4p
-rw-------@ 1 foo  staff  3821305  6  6  2009 03 Mars Declares.m4p
# ... 略

test-src//平沢 進:
total 0
drwx------  4 foo  staff  136  2  4 11:48 時空の水/

test-src//平沢 進/時空の水:
total 15344
-rw-------@ 1 foo  staff  4542192  4  4  2008 01 ハルディン・ホテル.m4p
-rw-------@ 1 foo  staff  3311160  4  4  2008 10 金星.m4p

% xattr -x test-src/Makana/Different\ Game/01\ Away.m4p 
# => com.apple.FinderInfo

% xattr -x test-src/平沢\ 進/時空の水/01\ ハルディン・ホテル.m4p
# => com.apple.FinderInfo


AppleScipt 実行後
保存先フォルダ内

% ls -FRl test-dst/
total 0
drwx------  3 foo  staff  102  2 15 16:35 Makana/
drwx------  3 foo  staff  102  2  4 11:48 平沢 進/

test-dst//Makana:
total 0
drwx------  17 foo  staff  578  2 15 16:33 Different Game/

test-dst//Makana/Different Game:
total 92824
-rw-------  1 foo  staff  4428672  6  6  2009 01 Away.m4p
-rw-------  1 foo  staff   623834  2  4 18:09 02 Interlude I.m4p
-rw-------  1 foo  staff  3821305  6  6  2009 03 Mars Declares.m4p
# ... 略

test-dst//平沢 進:
total 0
drwx------  4 foo  staff  136  2  4 11:48 時空の水/

test-dst//平沢 進/時空の水:
total 15344
-rw-------  1 foo  staff  4542192  4  4  2008 01 ハルディン・ホテル.m4p
-rw-------  1 foo  staff  3311160  4  4  2008 10 金星.m4p

% xattr -x test-dst/Makana/Different\ Game/01\ Away.m4p
# => 何もかえってこない。

% xattr -x test-dst/平沢\ 進/時空の水/01\ ハルディン・ホテル.m4p
# => 何もかえってこない。


[2010-02-16]
s/errr_str/err_str/g
s/warm_str/warn_str/g
s/やめる/Cancel/g
[2010-02-18]
s/folk/fork/g

0 件のコメント: