这是我的基本 AppleScript...
我有一个固定的文件路径,后面跟着1个变量文件夹,后面跟着一个可变的部分文件夹名称(即变量文件夹以“12345”开头)。 我未能解决的是如何在 1 条指令中打开目标文件夹(因为它是一个变量)。所以我必须打开封闭的文件夹,然后执行“获取名称开头的文件夹”。这是我的脚本非常慢的地方。目标文件夹包含几千个文件夹,因此搜索“开头为”的文件夹不太实用。
set fixedFolderPath to ":Volumes:FixedFolder1:FixedFolder2:FixedFolder3:"
set varFolderName to "VariableFolder4:"
set finalFolderPath to fixedFolderPath & varFolderName
set partialName to "VariableNumber12345"
tell application "Finder"
open finalFolderPath as alias
set finalFolder to (get folders of front window whose name starts with partialName)
set target of window 1 to finalFolder as alias
end tell
几乎任何事情都需要搜索文件夹名称,并且调用应用程序和涉及的各种 Apple 事件会产生一些开销。对于此类内容,shell 脚本(包括其开销)通常性能更高,对于 Spotlight 可能无法索引的外部卷来说更是如此。 POSIX 路径也可以直接使用,无需与 Finder 发生冲突。
以下示例使用 find 执行不区分大小写的搜索并返回找到的第一个目录:
set fixedFolderPath to "/Volumes/FixedFolder1/FixedFolder2/FixedFolder3/"
set varFolderName to "VariableFolder4/"
set finalFolderPath to fixedFolderPath & varFolderName
set partialName to "VariableNumber12345"
set found to (do shell script "/usr/bin/find " & quoted form of finalFolderPath & " -type d -maxdepth 1 -iname " & quoted form of (partialName & "*") & " -print -quit") -- name pattern matches everything after partialName
if found is not "" then
tell application "Finder" to reveal found as POSIX file
else
(display alert "Directory not found" message "A folder name starting with " & quoted form of partialName & " was not found.")
end if