获取与AppleScript的完整目录内容 [英] Get full directory contents with AppleScript
问题描述
我需要得到一个文件夹的全部(可见)的内容和它的子文件夹列表。这可能吗?
I need to get the entire (visible) contents of a folder and its subfolders as a list. Is this possible?
推荐答案
我敢肯定有一个shell命令可以做到这一点快,但这里是纯AppleScript的一种方式,让你完全控制格式化哪些信息想显示。
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.
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的应用程序。
On a side note, there are also a number file listing applications that can do this faster than Applescript.
更新:作为讨论的结果,这里是再次的功能,但使用更新的API这个时候。这很可能会使用一些清理,但它的作品编目我的桌面得心应手足够(这是一个很深很深的文件夹对我来说):
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):
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的变化颁布和我的从来没有一次的听说过他们。我用我的第一次提供的方法几十个项目,主要是因为它是code最初由苹果公司提供的,并使用此(即使在写这篇文章的时间)完全没有错误,从来没有触发需要我进行任何更新。
I must be subscribed to the wrong mailing lists or missing one because these API changes were enacted and I never once heard 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.
的Turnabout是公平竞争,以及我对恶意downvote到mmcgrail道歉,而我与给予好评替换它。只是要清楚,我从来没有想过通过mmcgrail给出的答案是错的。这是一个伟大的单行方便的方法只有一个我从我的每已经给意见敬而远之。而是,这是他下来表决,我采取了进攻与规定的范围内。最后,它只是code,我觉得我们都在这里出于同样的原因:找做什么我们做的更好的方法。看来现在我有我自己的一些更新颁布。
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.
干杯
这篇关于获取与AppleScript的完整目录内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!