如何让Apple Script在指定仓库目录正确运行Python构建脚本?
解决AppleScript打开多个终端并执行Python脚本的问题
问题根源
你当前脚本的核心问题是混淆了do script和do shell script的作用:
do script是向Terminal窗口发送命令,在窗口的环境中执行do shell script是系统独立启动进程执行命令,和Terminal窗口无关
这就导致两个独立的执行流:一个Terminal窗口切换到仓库目录,另一个系统进程在用户根目录(~)执行Python脚本,脚本无法访问仓库内的相对路径文件。
修复后的脚本
to run set dialogText to "Do you want to build Effekseer first?" display dialog dialogText buttons {"Skip", "Yes"} default button "Skip" if result = {button returned:"Yes"} then set repoPath to "~/Documents/_Repos/Effekseer" tell application "Terminal" if not (exists window 1) then reopen activate -- 合并切换目录与执行脚本的命令,确保在同一Terminal窗口的正确目录下运行 do script "cd " & repoPath & " && python3 build.py" in window 1 end tell -- 等待构建完成 set isBusy to true repeat until isBusy is false tell application "Terminal" tell window 1 set isBusy to busy as boolean end tell end tell delay 1 end repeat -- 启动程序 do shell script repoPath & "/Dev/Release/Effekseer" else if result = {button returned:"Skip"} then do shell script "~/Documents/_Repos/Effekseer/Dev/Release/Effekseer" end if end run
关键修改说明
- 合并命令:将
cd和python3 build.py用&&连接成单条命令,通过do script发送到同一个Terminal窗口,确保脚本在仓库目录下执行 - 指定窗口执行:添加
in window 1明确命令发送到已激活的Terminal窗口,避免创建新窗口 - 统一路径变量:将仓库路径定义为变量,减少重复代码,便于后续修改
内容的提问来源于stack exchange,提问作者Enric Delgado Claramunt
相关产品推荐
相关产品推荐

