2013年5月30日木曜日

【AppleScript】 フォルダ内のファイル(HFSパス/URL)を取得

インフルエンザな暇人なので久しぶりにプログラミングに着手しました。
いつものリハビリハンドラです。

フォルダ内のファイルのHFSパス、URLを取得します。
使い方は、ファイル一覧を出力したいとき、更にEvernoteとかに貼り付けようとしたらURLもあると便利だな、という感じです。

やりようによって、下層フォルダも巡回するので、うまく取り出せれば面白いですね。
複雑で混乱しそうなので、いまのところ1階層しか読み込んでませんが。

ちょっと番外ではありますが、shellのlsを使うというのもあります。
単純に一覧化するだけなら、こちらのほうが便利。URLはなくなりますが。

あと、treeという罫線でツリー化するshellコマンドもあります。

fol
├file.a
├file.b
├file.c
└file.d

みたいなやつですね。
たしかMacPortsでインストールできたと思います。
見た目のバランス的に、これもアリかもしれません。
うまく合わせて綺麗に表示したいですね。


tell application "Finder" to set someFolder to (choose folder with multiple selections allowed)
set fileList to {}
repeat with thePath in someFolder
set theFile to my DiveFolder(thePath as alias, fileList)
end repeat

on DiveFolder(thePath, alist)
tell application "Finder"
set theFiles to every file of thePath
repeat with a in theFiles
set b to (container of a as text) & name of a
set c to URL of a
set end of alist to {b, c}
end repeat
(* コメントアウトで下層フォルダも巡回
try
set theFolder to every folder of thePath
end try
repeat with aFolder in theFolder
DiveFolder(aFolder, alist) of me
end repeat
*)
end tell
return alist
end DiveFolder

2 件のコメント: