You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让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

关键修改说明

  1. 合并命令:将cd和python3 build.py用&&连接成单条命令,通过do script发送到同一个Terminal窗口,确保脚本在仓库目录下执行
  2. 指定窗口执行:添加in window 1明确命令发送到已激活的Terminal窗口,避免创建新窗口
  3. 统一路径变量:将仓库路径定义为变量,减少重复代码,便于后续修改

内容的提问来源于stack exchange,提问作者Enric Delgado Claramunt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 09:23:16