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

Logic Pro X无状态GUI按钮的AppleScript颜色检测与MIDI触发问询

Logic Pro X无状态参数按钮的AppleScript颜色检测解决方案思路

问题背景

Logic Pro X中部分GUI按钮没有状态参数(value = nil),无法通过常规方式判断激活状态。我需要编写AppleScript检测按钮颜色(激活态为蓝色,未激活态为灰色),以此为触发条件向外部控制器发送MIDI消息,但目前卡在颜色检测与按钮元素的映射代码编写环节,寻求解决思路。

现有代码

tell application "Logic Pro X" to activate
delay 1
tell application "System Events"
    set projectContent to entire contents of first application process whose its frontmost is true
    set project to item 2 of projectContent -- 第一个UI元素为窗口
    tell process "Logic Pro X"
        tell project -- 替换为你的项目名称才能让脚本正常运行
(*
            以下代码通过工具栏的固定特征定位智能控制面板:
            先尝试查找带'Arpeggiator'复选框的面板,失败则用快捷键打开面板后查找'Compare'按钮。
            双层whose查询逻辑为:"找到第一个UI元素是工具栏,且对应控件描述匹配的组",
            这种方式不受其他面板展开/折叠状态影响,能稳定定位目标区域
*)
            try
                set smartControl_panel to first group whose its (first UI element whose role description is "toolbar")'s last checkbox's description is "Arpeggiator"
            on error
                keystroke "b" -- 智能控制面板未打开时,用LPX快捷键唤起
                delay 2
                set smartControl_panel to first group whose its (first UI element whose role description is "toolbar")'s first button's description is "Compare"
            end try
            tell smartControl_panel
                tell first group
                    log (get description of every UI element) -- 记录所有可见UI元素的描述信息
                    set compare_button to first UI element whose role description is "button" and description is "Compare"
                    log (get position of compare_button) -- 记录Compare按钮的当前位置
                    set x to (item 1 of position) + 10
                    set y to (item 2 of position) + 10
                    set compare_state to {x, y}

(* 我的核心问题是实现持续运行的按钮颜色检测回调:
                    -- set buttonCheck(compare_state, x, y)
*)
                if buttonCheck is true then
                    (*
                    可选择用SendMIDI发送SysEx消息,或执行点击按钮等操作:
                    -- click the Compare_button to toggle its state
                    也可添加MIDI音符消息,见代码中EOX(0xF7)之后的内容:
                    *)
                    do shell script "/usr/local/bin/sendmidi" & " dev " & gPort & " raw hex F0 00 00 66 10 12 00 43 6f 6d 70 61 72 65 F7" -- 激活状态对应的MIDI消息
                    do shell script "/usr/local/bin/sendmidi" & " dev " & gPort & " raw hex 90 2D 7F" -- MIDI音符开启消息
                else
                    do shell script "/usr/local/bin/sendmidi" & " dev " & gPort & " raw hex F0 00 00 66 10 12 00 43 6f 6d 70 61 72 65 F7" -- 未激活状态对应的MIDI消息
                    do shell script "/usr/local/bin/sendmidi" & " dev " & gPort & " raw hex 90 2D 00" -- MIDI音符关闭消息
                end if
            end tell
        end tell
    end tell
end tell
end tell

核心卡点解决思路

当前需重点实现buttonCheck函数,完成按钮颜色检测与持续回调,以下是可行方向:

  • 屏幕取色实现状态判断:
    AppleScript无原生取色API,可通过两种方式实现:

    1. 借助命令行工具:用screencapture截取按钮对应1x1像素区域,搭配ImageMagick的convert命令读取颜色值,对比蓝色/灰色的RGB范围判断状态;
    2. 用AppleScriptObjC调用Quartz框架:编写桥接代码直接获取屏幕像素颜色,无需外部工具,效率更高。
  • 持续检测的循环逻辑:
    用repeat循环包裹检测代码,添加delay控制检测频率(如每0.5秒检测一次),同时加入Logic Pro X前台判断,避免后台误触发。

  • 按钮全局坐标修正:
    当前获取的是窗口内相对坐标,需结合窗口全局坐标计算按钮的屏幕绝对坐标,避免窗口移动后取色位置偏移:

    set winPos to position of project -- 获取窗口全局坐标
    set btnRelPos to position of compare_button -- 获取按钮相对窗口的坐标
    set globalX to (item 1 of winPos) + (item 1 of btnRelPos) + 10
    set globalY to (item 2 of winPos) + (item 2 of btnRelPos) + 10
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 20:34:57