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

Ale*_*ers 8 directory applescript list subdirectory

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

mcg*_*ilm 10

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

  tell application "Finder"
     set file_list to entire contents of (choose folder with prompt "Please select directory.")
   end tell 
Run Code Online (Sandbox Code Playgroud)

如果你想要一个文件名列表,那么你可以这样做

  tell application "Finder"
    set file_list to name of every file of entire contents of (choose folder with prompt "Please select directory.")
   end tell
Run Code Online (Sandbox Code Playgroud)

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

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

是的,整个内容完全按照你说的做 - 但它很容易在大文件夹上窒息,并且需要永远(如果你在10.6,则为一天).对于小事情来说没关系,比如从一个你知道只包含少量文件的文件夹中提取一种文件.

递归方法也运行良好 - 但它使用"列表文件夹",它的字典列表说它已被弃用,我们不应再使用它了."

所以我通过声明我错了!这两种解决方案都是有效的,但在其使用中存在"痘"或漏洞.我的applogies给菲利普.如果他决定编辑他的答案(因为那是改变我投票的唯一方法),我将很乐意回来并给他+1


Phi*_*gan 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
Run Code Online (Sandbox Code Playgroud)

另外,还有一个列出应用程序的数字文件可以比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
Run Code Online (Sandbox Code Playgroud)

我必须订阅错误的邮件列表或丢失一个,因为这些API更改已经颁布,我从未听说过它们.我在几十个项目中使用了我的第一个提供的方法,主要是因为它是Apple最初提供的代码,使用它完全没有错误(即使在写这篇文章的时候)从来没有引起我更新任何东西的需要.

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

干杯