list 使用 AppleScript 获取完整目录内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2097263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get full directory contents with AppleScript
提问by Alexsander Akers
I need to get the entire (visible) contents of a folder and its subfolders as a list. Is this possible?
我需要将文件夹及其子文件夹的整个(可见)内容作为列表获取。这可能吗?
采纳答案by Philip Regan
I'm sure there is a shell command that can do this faster, but here is one way in pure Applescript that gives you total control over formatting what info you would like displayed.
我确定有一个 shell 命令可以更快地完成此操作,但这是纯 Applescript 中的一种方法,可让您完全控制格式化您想要显示的信息。
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
On a side note, there are also a number file listing applications that can do this faster than Applescript.
附带说明一下,还有一些文件列表应用程序可以比 Applescript 更快地完成此操作。
UPDATE:As a result of this discussion, here is the function again, but this time using the updated API. This could probably could use some cleaning up, but it works cataloging my Desktop handily enough (and that's a deep, deep folder for me):
更新:作为讨论的结果,这里又是这个函数,但这次使用更新的 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
I must be subscribed to the wrong mailing lists or missing one because these API changes were enacted and I never onceheard about them. I've used my first-offered method in dozens of projects largely because it was the code originally offered by Apple, and the complete lack of errors using this (even at the time of this writing) never triggered a need for me to update anything.
我一定订阅了错误的邮件列表或丢失了一个,因为这些 API 更改已生效,而我从未听说过它们。我在几十个项目中使用了我的第一个提供的方法,主要是因为它是 Apple 最初提供的代码,并且使用它完全没有错误(即使在撰写本文时)也从未触发我需要更新任何内容.
Turnabout is fair play, and my apologies for the spiteful downvote to mmcgrail, and I am replacing it with an upvote. Just to be clear, I never thought the answer given by mmcgrail was wrong. It's a great one-line convenience method but one I've stayed away from per my comments already given. But rather, it was his down vote and its stated context that I took offense with. In the end, it's just code, and I think we're all here for the same reason: to find a better way of doing what we do. It seems I now have some updates of my own to enact.
Turnabout 是公平的竞争,我为对 mmcgrail 的恶意投反对票表示歉意,我将用投赞成票代替它。明确地说,我从不认为 mmcgrail 给出的答案是错误的。这是一种很棒的单行便捷方法,但根据我已经给出的评论,我一直远离这种方法。但相反,我对他的反对票及其陈述的背景感到生气。归根结底,它只是代码,我认为我们都出于同样的原因:找到更好的方式来做我们所做的事情。看来我现在有一些自己的更新要制定。
Cheers
干杯
回答by mcgrailm
that is way more work than needed. I know this is an old post but I want you both to see how easy this can be
这是比需要更多的工作。我知道这是一篇旧帖子,但我希望你们俩都知道这有多容易
tell application "Finder"
set file_list to entire contents of (choose folder with prompt "Please select directory.")
end tell
if you want a list of file names then you could do this
如果你想要一个文件名列表,那么你可以这样做
tell application "Finder"
set file_list to name of every file of entire contents of (choose folder with prompt "Please select directory.")
end tell
Due to the comments that follow this post I asked a group of experts in business to look at this question and evaluate our answer in an unbiased manner and the answer I got was thsis
由于这篇文章后面的评论,我请了一群商业专家来看看这个问题,并以公正的方式评估我们的答案,我得到的答案是这个
"How about a pox on both your houses? :-)
“你家两处都长痘怎么样?:-)
Yes, entire contents does exactly what you say -- but it easily chokes on large folders, and takes forever (and a day if you're on 10.6). It's OK for small things, like extracting all the files of one kind out of a folder you know will only contain a small number of files.
是的,整个内容完全按照你说的做——但它很容易被大文件夹阻塞,并且需要永远(如果你使用的是 10.6 则需要一天)。对于小事情来说是可以的,比如从你知道只包含少量文件的文件夹中提取一种类型的所有文件。
The recursive method also works well -- but it's using "list folder", and the dictionary listing for it says it's deprecated and we shouldn't use it any more."
递归方法也很有效——但它使用的是“列表文件夹”,它的字典列表说它已被弃用,我们不应再使用它。”
So I hear by declare that I was wrong! both of these solutions are valid but have "pox" or holes in their uses. My applogies to to Philip. Should he decide to edit his answer (because thats the only way to change my vote)I will be happy to return and give him +1
所以我听到声明我错了!这两种解决方案都是有效的,但在它们的使用中存在“痘痘”或漏洞。我向菲利普致意。如果他决定编辑他的答案(因为这是改变我投票的唯一方法)我会很乐意回来给他+1
回答by u4864450
Wow this is quite late but I checked and it works.
哇,这已经很晚了,但我检查了一下,它有效。
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