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

List 使用AppleScript获取完整目录内容,list,applescript,directory,subdirectory,List,Applescript,Directory,Subdirectory,我需要以列表的形式获取文件夹及其子文件夹的全部(可见)内容。这可能吗?我确信有一个shell命令可以更快地完成这项工作,但在纯Applescript中有一种方法可以让您完全控制要显示的信息的格式 property kFileList : {} tell application "Finder" set source_folder to choose folder with prompt "Please select directory." my createList(sourc

我需要以列表的形式获取文件夹及其子文件夹的全部(可见)内容。这可能吗?

我确信有一个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。这可能需要一些清理,但它可以很方便地对我的桌面进行编目(这对我来说是一个很深的文件夹):

我必须订阅错误的邮件列表或缺少一个,因为这些API更改是制定的,我从来没有听说过。我在几十个项目中使用了我的第一个提供的方法,主要是因为它最初是由苹果提供的代码,而且使用这种方法时完全没有错误(即使在撰写本文时),我从来没有需要更新任何东西

转变是公平的,我对恶意否决mmcgrail表示歉意,我将以向上投票取而代之。说清楚一点,我从没想过mmcgrail给出的答案是错误的。这是一个很好的一行方便的方法,但根据我已经给出的评论,我一直远离它。但是,恰恰是他投了反对票,以及他所陈述的背景让我感到愤怒。最后,这只是代码,我想我们来到这里的原因都是一样的:找到更好的方法来做我们所做的事情。看来我现在有自己的一些更新要执行


干杯

这是远远超出需要的工作。我知道这是一篇老文章,但我想让你们两个看看这有多容易

  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上的话,需要一天时间) 一些小事情,比如从你需要的文件夹中提取一种类型的所有文件 “知道”将只包含少量文件

递归方法也可以很好地工作——但它使用“列表文件夹”,并且 它的字典列表上说它已被弃用,我们不应该使用它 再也没有了。”


所以我听说我错了!这两种解决方案都是有效的,但在使用中存在“痘”或漏洞。我向菲利普致意。如果他决定编辑他的答案(因为这是改变我投票的唯一方法),我会很高兴回来给他+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

对,因为有大量的{文档文件“FileName”文件夹“SubFolder”文件夹“folder”文件夹“username”文件夹“Users”应用程序“Finder”启动盘的“Users”,ad nauseum}你有什么要处理的呢?你有列表,并根据需要引用项目,即在文件列表中别名项目,你做的工作比需要的多,这就是为什么我给了你一个否决票,我没有给你,不管在什么地方做你要做的事同样,你的kFileList只返回一个文件名列表,而不引用文件来自何处,那么接下来如何通过文件夹返回以确定它们来自何处呢?这是一个“六合一,五六合一”的场景。通过预先获取文件信息属性,很容易创建一个自定义列表,而不是获取别名列表,然后在事后提取所需信息。更少的代码行并不总是最好的答案,也不是最坏的答案。在否决之前请三思。@恶意的否决:是的。是的。根据我上面的观点,你的和我的一样没有必要。我很少投反对票(一年中有六次,包括这里),而你似乎在3个月内5岁就开始了。我只是对答案中真正令人震惊的错误投了反对票,但你们似乎在我脾气暴躁的一天抓住了我。下一次,再一次,为了“想让我们都知道这是多么容易”,在否决投票和牺牲宝贵的分数之前要三思而后行。如果你只是不喜欢一个答案,那就别管它了。它会写一些类似“无法获取…的每个磁盘项”的内容
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