Applescript脚本优化:用choose file name替代多步交互
优化AppleScript:用
choose file name简化用户输入流程 嘿,很高兴看到你在打磨自己的AppleScript!你的想法完全可行——用choose file name确实能把原来的4次用户交互合并成2次,既简化了操作流程,又让脚本更简洁。我帮你重构了脚本,保留了所有原有功能,同时解决了创建文件夹并保留名称变量的问题,还优化了一些重复代码。
优化后的脚本
set logoIcon to alias ((path to me as string) & "Contents:Resources:Ourlogo.icns") set bundleResources to (path to me as string) & "Contents:Resources:" set defaultLocation to (path to desktop) as alias -- 第一步:选择作业文件夹的名称和位置 set jobFilePrompt to "命名并选择今日作业的保存位置:" set defaultJobPath to defaultLocation as string & "Job-2018-00-00" set chosenJobPath to choose file name with prompt jobFilePrompt default name "Job-2018-00-00" default location defaultLocation with icon logoIcon set newJobname to name of (info for chosenJobPath) set newJobParentFolder to container of (info for chosenJobPath) as alias tell application "Finder" -- 创建作业主文件夹 set newJobDirectory to make new folder at newJobParentFolder with properties {name:newJobname} -- 复制并重命名作业模板 set getJobBrief to file "job_brief_template.rtf" of (bundleResources as alias) duplicate getJobBrief to newJobDirectory set name of document file "job_brief_template.rtf" of newJobDirectory to newJobname & ".rtf" -- 批量创建标准作业文件夹(用循环减少重复代码) set jobSubfolders to {"Raw Folder", "Mark Ups", "Resources", "JPGS", "No Retouching"} repeat with folderName in jobSubfolders make new folder at newJobDirectory with properties {name:folderName} end repeat end tell -- 第二步:选择Capture One会话的名称和位置(默认使用作业名) set seshFilePrompt to "命名并选择今日Capture Session的保存位置:" set defaultSeshPath to defaultLocation as string & newJobname set chosenSeshPath to choose file name with prompt seshFilePrompt default name newJobname default location defaultLocation with icon logoIcon set seshName to name of (info for chosenSeshPath) set newSeshParentFolder to container of (info for chosenSeshPath) as alias tell application "Finder" -- 创建会话主文件夹 set newSeshDirectory to make new folder at newSeshParentFolder with properties {name:seshName} -- 批量创建标准会话文件夹 set seshSubfolders to {"Capture", "Selects", "Output", "Trash"} repeat with folderName in seshSubfolders make new folder at newSeshDirectory with properties {name:folderName} end repeat -- 复制并重命名空白会话文件 set getSeshDoc to file "session.cosessiondb" of (bundleResources as alias) duplicate getSeshDoc to newSeshDirectory set name of document file "session.cosessiondb" of newSeshDirectory to seshName & ".cosessiondb" -- 复制会话名称到剪贴板,并准备打开会话 set the clipboard to seshName set openSesh to newSeshDirectory & seshName & ".cosessiondb" as string end tell -- 调用Capture One打开会话 tell application id "C1PR" open file openSesh end tell
关键改进点
- 用
choose file name合并输入:把原来的「输入名称+选择文件夹」合并成一次操作,用户可以直接在对话框里输入名称,同时选择保存位置,更贴合macOS的交互习惯。 - 提取名称和父文件夹:通过
info for chosenJobPath获取选中路径的name(用户输入的名称)和container(父文件夹),再用这些信息创建对应的目录。 - 批量创建文件夹:用循环处理重复的文件夹创建代码,让脚本更简洁,后续修改文件夹列表也更方便。
- 默认值复用:会话名称默认使用作业名,减少用户重复输入,同时保留修改的空间。
- 完善会话打开逻辑:直接调用Capture One的ID打开会话,完成整个流程闭环。
choose file name使用技巧
- 获取用户输入的名称:
name of (info for chosenPath)可以直接提取用户在对话框中输入的文件名(也就是我们需要的文件夹名称)。 - 获取保存位置:
container of (info for chosenPath)会返回用户选择的父文件夹,我们可以基于这个路径创建新的文件夹。 - 设置默认值:通过
default name和default location参数,可以预设默认的名称和位置,提升用户操作效率。 - 处理虚拟路径:
choose file name返回的是一个虚拟的文件路径(文件还未创建),所以我们需要先创建对应的文件夹,再处理文件的复制和重命名。 - 自定义图标:
with icon参数可以指定对话框的图标,保持和原有脚本的视觉一致性。
内容的提问来源于stack exchange,提问作者deefroud
相关产品推荐
相关产品推荐

