フォルダとそのサブフォルダの(表示されている)コンテンツ全体をリストとして取得する必要があります。これは可能ですか?
3 に答える
それは必要以上の作業です。これは古い投稿ですが、これがいかに簡単かを両方に見てもらいたいと思います。
tell application "Finder"
set file_list to entire contents of (choose folder with prompt "Please select directory.")
end tell
ファイル名のリストが必要な場合は、これを行うことができます
tell application "Finder"
set file_list to name of every file of entire contents of (choose folder with prompt "Please select directory.")
end tell
この投稿に続くコメントのために、私はビジネスの専門家のグループにこの質問を見て、偏りのない方法で私たちの答えを評価するように頼みました、そして私が得た答えはこれでした
「あなたの両方の家のポックスはどうですか?:-)
はい、コンテンツ全体があなたの言うとおりに機能しますが、大きなフォルダでは簡単に詰まり、永遠にかかります(10.6の場合は1日かかります)。少数のファイルしか含まれないことがわかっているフォルダから1種類のすべてのファイルを抽出するなど、小さなことでも問題ありません。
再帰的方法もうまく機能しますが、「リストフォルダ」を使用しており、その辞書リストには非推奨であり、これ以上使用しないように記載されています。」
だから私は間違っていたと宣言して聞きます!これらのソリューションはどちらも有効ですが、使用に「pox」または穴があります。フィリップに謝罪します。彼が彼の答えを編集することを決定した場合(それが私の投票を変更する唯一の方法であるため)私は戻って彼に+1を与えることを嬉しく思います
これをより速く行うことができるシェルコマンドがあると確信していますが、これは、表示したい情報のフォーマットを完全に制御できる純粋なApplescriptの1つの方法です。
property kFileList : {}
tell application "Finder"
set source_folder to choose folder with prompt "Please select directory."
my createList(source_folder)
end tell
on createList(item_list)
set the the_items to list folder item_list without invisibles
set item_list to item_list as string
repeat with i from 1 to number of items in the the_items
set the_item to item i of the the_items
set the_item to (item_list & the_item) as alias
set this_info to info for the_item
set file_name to name of this_info
set end of kFileList to file_name
if folder of this_info is true then
my createList(the_item)
end if
end repeat
end createList
ちなみに、これをApplescriptよりも高速に実行できるアプリケーションを一覧表示するファイルも多数あります。
更新:この議論の結果として、ここに再び関数がありますが、今回は更新されたAPIを使用しています。これはおそらくクリーンアップを使用する可能性がありますが、デスクトップのカタログ化は十分に手軽に機能します(そしてそれは私にとっては深く深いフォルダーです):
property kFileList : {}
tell application "Finder"
set source_folder to choose folder with prompt "Please select directory."
my createList(source_folder)
end tell
return kFileList
on createList(mSource_folder)
set item_list to ""
tell application "System Events"
set item_list to get the name of every disk item of mSource_folder
end tell
set item_count to (get count of items in item_list)
repeat with i from 1 to item_count
set the_properties to ""
set the_item to item i of the item_list
set the_item to ((mSource_folder & the_item) as string) as alias
tell application "System Events"
set file_info to get info for the_item
end tell
if visible of file_info is true then
set file_name to displayed name of file_info
set end of kFileList to file_name
if folder of file_info is true then
my createList(the_item)
end if
end if
end repeat
end createList
これらのAPIの変更が制定され、一度も聞いたことがないため、間違ったメーリングリストに登録するか、メーリングリストが欠落している必要があります。私は最初に提供した方法を数十のプロジェクトで使用しました。これは主に、Appleが最初に提供したコードであり、これを使用したエラーが完全にないため(この記事の執筆時点でも)、何かを更新する必要が生じたことはありません。 。
ターンアバウトはフェアプレーであり、mmcgrailへの意地悪な反対票をお詫びします。私はそれを賛成票に置き換えます。明確にするために、私はmmcgrailによって与えられた答えが間違っているとは思っていませんでした。これは1行の便利な方法ですが、すでに述べたコメントに従って、私は避けてきました。むしろ、私が腹を立てたのは彼の反対票とその表明された文脈でした。結局のところ、それは単なるコードであり、私たち全員が同じ理由でここにいると思います。私たちがしていることを行うためのより良い方法を見つけることです。私は今、制定するために私自身のいくつかの更新を持っているようです。
乾杯
うわーこれはかなり遅いですが、私はチェックし、それは動作します。
tell application "Finder" to set folder_root to (choose folder with prompt "Please select directory.")
set fl to {}
dump_folder(folder_root)
on dump_folder(f)
global fl
tell application "System Events" to set end of fl to (get the POSIX path of f)
tell application "Finder" to set nfl to (the items of the contents of f)
repeat with nf in nfl
dump_folder(nf as alias)
end repeat
end dump_folder
fl