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

AppleScript中else if语法错误求助:Logic Pro X脚本调试

Logic Pro X AppleScript语法错误修复

问题代码

原脚本因语法结构错误触发报错,原代码如下:

activate application "Logic Pro X"
tell application "System Events"
    tell process "Logic Pro"
    
        set tracks_window to title of first window whose title contains "- Tracks"
        
        if value of static text 1 of group 3 of list 1 of group 2 of window tracks_window is "Track:" and value of UI element 1 of group 3 of list 1 of group 2 of window tracks_window is 0 then click UI element 1 of group 3 of list 1 of group 2 of window tracks_window

        else if value of static text 1 of group 2 of list 1 of group 2 of window tracks_window is "Track:" and value of UI element 1 of group 2 of list 1 of group 2 of window tracks_window is 0 then click UI element 1 of group 2 of list 1 of group 2 of window tracks_window

        end if

    end tell
end tell

错误原因

AppleScript语法规则:当if语句的then后直接跟单行执行代码时,该if会被判定为单行独立条件结构,无法后续衔接else if分支。原脚本中第一个if的执行语句与then写在同一行,导致编译器认为第一个if已结束,后续else if找不到对应的父if,从而抛出语法错误。

修复后的代码

将if分支改为多行结构,同时提取重复元素路径为变量简化代码:

activate application "Logic Pro X"
tell application "System Events"
    tell process "Logic Pro"
        set tracks_window to title of first window whose title contains "- Tracks"
        set target_group_container to list 1 of group 2 of window tracks_window
        
        -- 第一个条件分支
        if (value of static text 1 of group 3 of target_group_container is "Track:") and (value of UI element 1 of group 3 of target_group_container is 0) then
            click UI element 1 of group 3 of target_group_container
        -- 第二个条件分支
        else if (value of static text 1 of group 2 of target_group_container is "Track:") and (value of UI element 1 of group 2 of target_group_container is 0) then
            click UI element 1 of group 2 of target_group_container
        end if
    end tell
end tell

额外优化说明

  • 将重复的UI元素路径list 1 of group 2 of window tracks_window提取为变量target_group_container,减少代码冗余,提升可读性和维护性。
  • 给条件表达式添加括号,明确逻辑优先级,避免潜在的语法歧义。

内容的提问来源于stack exchange,提问作者WMB

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 23:05:30