使用AppleScript获取完整的目录内容

时间:2010-01-19 21:32:29

标签: list applescript directory subdirectory

我需要将文件夹及其子文件夹的整个(可见)内容作为列表。这可能吗?

3 个答案:

答案 0 :(得分:10)

这比需要的工作更多。我知道这是一篇很老的帖子,但我希望你们两个都能看到这样做有多容易

  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

由于这篇文章后面的评论,我请一组商业专家来看待这个问题,并以公正的方式评估我们的答案,我得到的答案是thsis

“你们两个房子的痘怎么样?: - )

是的,整个内容完全符合您的要求 - 但它很容易窒息 大文件夹,永远需要(如果你在10.6,那就是一天)。没关系 小东西,比如从你的文件夹中提取一种文件 知道只会​​包含少量文件。

递归方法也运行良好 - 但它使用“列表文件夹”,和 它的字典列表说它已被弃用,我们不应该使用它 再说一遍。“

所以我听说我错了!这两种解决方案都是有效的,但在其使用中存在“痘”或漏洞。我的applogies给菲利普。如果他决定编辑他的答案(因为那是改变我投票的唯一方法),我会很高兴回来并给他+1

答案 1 :(得分:8)

我确信有一个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

另外,还有一个列出应用程序的数字文件可以比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最初提供的代码,使用它完全没有错误(即使在写这篇文章的时候)从来没有引起我更新任何东西的需要。

Turnabout是公平竞争,我为恶意击败mmcgrail道歉,我用upvote代替它。为了清楚起见,我从未想过mmcgrail给出的答案是错误的。这是一个很棒的单行便利方法,但是根据我已经给出的评论,我已经远离了。但更确切地说,这是他的投票和我所说的背景。最后,它只是代码,我认为我们都出于同样的原因:找到一种更好的方式来做我们的工作。看来我现在有了一些我自己的更新来制定。

干杯

答案 2 :(得分: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