applescript 使用简单的 "whose" 查询挂起 finder

applescript hangs finder with simple "whose" query

我尝试编写一个非常简单的 Automator 服务,以 select 所有与当前突出显示的文件具有相同扩展名的文件。它调用了一个我这样写的 Applescript 脚本(实际上是受这里找到的一些代码的启发):

on run {input, parameters}
    try
        set anItem to first item of input
        tell (info for anItem) to set theExt to name extension
        tell application "Finder"
            set parentFolder to (parent of anItem as alias)
            select (every document file of parentFolder whose (name extension is theExt))
        end tell
    end try
end run

它 运行 到行 'select...' 为止都很好,具有适当的变量 parentFolder 和 theExt。然后我得到一个沙滩球。 Finder 长时间停留在 100%,我必须杀死它并重新启动。

编辑: 我刚刚意识到,如果文件夹包含几十个元素,实际上 运行 没问题。它挂起(甚至允许 运行 小时 )在一个包含大约 10000 个元素的大文件夹上(很明显,你在处理大量文件时会编写那种代码...... .).

使用 where 子句过滤项目时,Finder 非常慢。

这是使用 System Events 的替代方法,速度要快得多

on run {input, parameters}
    try
        set anItem to first item of input
        tell application "System Events"
            set theExt to name extension of anItem
            set parentFolder to path of container of anItem
            set itemsToSelect to (path of files of folder parentFolder whose name extension is theExt)
        end tell
        tell application "Finder"
            open parentFolder
            select itemsToSelect
        end tell
    end try
end run

注意:info for 已被弃用很长时间。最好直接从 System Events 获取信息。