改进脚本的第一件事就是不要使用 shell 脚本。Shell 脚本就像一个手提钻,但您所做的只需要最小的手持锤。
这是您的原始脚本:
tell application "Finder"
activate
set jobNum to text returned of (display dialog "Enter a job number:" default answer "[JobNumber]-[Description]")
set folderpath to POSIX path of (choose folder with prompt "Select client folder")
do shell script "/bin/mkdir -p " & quoted form of folderpath & "/" & quoted form of (jobNum) & "/" & quoted form of (jobNum & "-Links")
do shell script "/bin/mkdir -p " & quoted form of folderpath & "/" & quoted form of (jobNum) & "/" & quoted form of (jobNum & "-PDFs")
do shell script "/bin/mkdir -p " & quoted form of folderpath & "/" & quoted form of (jobNum) & "/" & quoted form of (jobNum & "-Supplied")
do shell script "/bin/mkdir -p " & quoted form of folderpath & "/" & quoted form of (jobNum) & "/" & quoted form of (jobNum & "-Versions")
end tell
与更易于阅读、编写和维护的“脱壳”版本相比:
tell application "Finder"
activate
set jobNum to text returned of (display dialog "Enter a job number:" default answer "[JobNumber]-[Description]")
set theClientFolder to (choose folder with prompt "Select client folder")
set theSubFolderNames to {"Links", "PDFs", "Supplied", "Versions"}
repeat with theSubFolderName in theSubFolderNames
make new folder at theClientFolder with properties {name:jobNum & "-" & theSubFolderName}
end repeat
end tell
请注意,变量“folderpath”已更改为“theClientFolder”,因为选择文件夹不返回路径,它返回所选文件夹的别名对象。告诉 Finder 创建新文件夹的命令是“在 [您希望创建文件夹的别名] 处创建新文件夹”,因此您无需将该别名转换为在其中创建新文件夹的路径。
另请注意,文件夹的名称已被声明为列表中的变量,这使它们更易于阅读或以后更改。即使是不了解 AppleScript 的人也可以打开此脚本并将“PDFs”文件夹的名称更改为“Documents”或类似的名称。
我可以建议的另一个改进是使您从用户那里收集输入的方式更加复杂。除了客户文件夹之外,您似乎还要求提供 2 项信息——工作编号和工作描述——所以使用 2 个对话框,因为这对用户来说更容易并且不易出错,因为你可以给出 2 个实际示例你希望他们输入。您还可以通过几种方式改善这些对话框的外观。并且您可以添加一些错误处理——如果用户取消您正在呈现的对话框,则会产生错误。您可以通过检查用户是否单击“确定”来优雅地处理它们。
这是您的脚本的一个版本,其中对用户输入进行了一些清理:
tell application "Finder"
activate
display dialog "Enter a job number:" default answer "100" buttons {"Cancel", "OK"} default button "OK" with title (the name as text) with icon note giving up after 180
if the button returned of the result is equal to "OK" then
set theJobNumber to the text returned of the result
display dialog "Enter a job description:" default answer "Photos" buttons {"Cancel", "OK"} default button "OK" with title (the name as text) with icon note giving up after 180
if the button returned of the result is equal to "OK" then
set theJobDescription to the text returned of the result
try
choose folder with prompt "Select client folder:" default location (the path to the desktop folder as alias) without multiple selections allowed, invisibles and showing package contents
set theClientFolder to the result
on error
set theClientFolder to ""
end try
if theClientFolder is not equal to "" then
set theSubFolderNames to {"Links", "PDFs", "Supplied", "Versions"}
repeat with theSubFolderName in theSubFolderNames
make new folder at theClientFolder with properties {name:theJobNumber & "-" & theJobDescription & "-" & theSubFolderName}
end repeat
end if
end if
end if
end tell
这将为您提供顶部显示“Finder”的对话框,并包含 Finder 图标,该图标将在 180 秒后放弃(以防止出现超时错误),并且如果用户在任何时候按下“取消”,它将只优雅地死掉而不会产生任何错误。您可以调整输入对话框的默认答案,以便它们帮助用户输入正确的输入,例如,如果工作编号是 4 位数字,则在此处输入 4 位数字。选择文件夹提示将从显示桌面开始 - 如果有存储所有客户端文件夹的磁盘或文件夹,您可以将其更改为另一个位置:
choose folder with prompt "Select client folder:" default location (disk "Clients" as alias) …
需要明确的是,shell 脚本非常棒,但我建议您只使用“执行 shell 脚本”来获得 UNIX 层的独特功能,例如 PHP 函数、Perl 正则表达式等。Mac 层中已经为您提供的任何内容都将更容易、更安全地使用,并且在 AppleScript 中更易于阅读、编写和维护。尤其是非常基本的事情,比如创建文件夹。