使用 AppleScript 获取完整的目录内容 [英] Get full directory contents with AppleScript

查看:19
本文介绍了使用 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 更改已经生效,而我从未听说过它们.我在数十个项目中使用了我的第一个提供的方法,主要是因为它是 Apple 最初提供的代码,并且使用它完全没有错误(即使在撰写本文时)也从未触发我需要更新任何内容.

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 是公平的竞争,我为对 mmcgrail 的恶意投反对票表示歉意,我将用投赞成票代替它.明确地说,我从不认为 mmcgrail 给出的答案是错误的.这是一种很棒的单行便捷方法,但根据我已经给出的评论,我一直远离这种方法.但相反,我对他的反对票及其陈述的背景感到生气.归根结底,它只是代码,我认为我们都出于同样的原因:找到更好的方式来做我们所做的事情.看来我现在有一些自己的更新要颁布.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆