如何从AppleScript向Photoshop Droplet传递文件路径列表
解决Photoshop Droplet仅处理单个文件的AppleScript问题
核心问题分析
- 文件列表传递错误:你的脚本中直接将字符串列表传给
document file,Finder无法正确解析整个列表,只会处理第一个元素。 - 异步执行漏处理:AppleScript调用Finder打开Droplet是异步操作,脚本会继续执行而不等待Droplet完成处理,导致仅部分文件被处理。
最优解决方案:使用Shell命令强制同步处理
通过do shell script调用open -W命令,既可以正确传递多文件路径,又能强制等待Droplet处理完成后再继续脚本执行。
修正后的脚本
property toPsDroplet : "/Volumes/VIDEO-REC/Scans/Scan_Preview.app" -- PS Droplet的POSIX路径 on open (theFiles) set posixFilePaths to {} repeat with aFile in theFiles -- 替换为你的重命名、移动逻辑,生成处理后的文件路径 set newFilePath to aFile -- 临时占位,需替换为实际新路径 -- 将文件路径转为POSIX格式并包裹引号,避免空格解析错误 set end of posixFilePaths to quoted form of POSIX path of newFilePath end repeat -- 构建Shell命令:-W参数等待Droplet退出后再返回 set shellCommand to "open -W -a " & quoted form of toPsDroplet & " " & (posixFilePaths as string) do shell script shellCommand end open
关键细节
quoted form的作用:自动处理路径中的空格、特殊字符,避免Shell命令解析出错。open -W的作用:强制等待目标应用(Droplet)完全退出后才继续执行脚本,确保所有文件都被处理。- 无需Finder介入:直接通过Shell调用Droplet,比Finder更可靠,避免对象解析问题。
备选方案:通过Finder传递文件列表
如果必须使用Finder,需正确构建文件对象列表并监控Photoshop进程等待处理完成:
property toPsDroplet : "/Volumes/VIDEO-REC/Scans/Scan_Preview.app" on open (theFiles) set targetFiles to {} repeat with aFile in theFiles set newFilePath to aFile -- 替换为实际处理后的路径 set end of targetFiles to newFilePath as alias end repeat tell application "Finder" set dropletAlias to POSIX file toPsDroplet as alias open targetFiles using dropletAlias -- 等待Photoshop退出(Droplet运行时会启动Photoshop,处理完自动退出) repeat while application "Adobe Photoshop" is running delay 5 -- 每5秒检查一次 end repeat end tell end open
注意:此方案依赖Photoshop处理完后自动退出,如果Photoshop原本就在运行,需调整逻辑判断处理状态。
内容的提问来源于stack exchange,提问作者Cesare Asaro
相关产品推荐
相关产品推荐

