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

AppleScript:如何通过数字ID定位并点击目标菜单?

AppleScript使用长数字ID点击菜单项的问题

我是AppleScript新手,已编写脚本的前半部分成功显示菜单(附图为鼠标悬停时UI Browser的结果)。但始终找不到使用长数字ID而非名称点击菜单的语法,尝试的代码执行失败。请问是否可使用Relative Specifier定位该特定菜单项?

UI Browser悬停结果图

尝试的失败代码:

tell application "System Events"
tell process "Remote Desktop Scanner"
    tell menu bar 2
        click menu bar item 1
        click menu item 2 of "menu -9223372036854775808" (this bit failed)
    end tell
end tell
end tell

问题修正与解决方案

你代码的核心问题是引用菜单的方式错误,不能直接将ID作为字符串使用。要通过menu元素的id属性定位目标菜单,同时也可以用相对定位的方式更稳定地操作。

1. 使用长数字ID的正确语法

修正后的代码会直接指定菜单的ID属性,而非字符串:

tell application "System Events"
    tell process "Remote Desktop Scanner"
        tell menu bar 2
            click menu bar item 1
            -- 通过ID定位菜单后点击对应菜单项
            tell menu id -9223372036854775808
                click menu item 2
            end tell
        end tell
    end tell
end tell

2. 使用Relative Specifier的相对定位方案

这种方式不需要硬编码菜单ID,而是通过刚点击的菜单栏项直接关联其弹出的菜单,稳定性更强:

tell application "System Events"
    tell process "Remote Desktop Scanner"
        tell menu bar 2
            set targetItem to menu bar item 1
            click targetItem
            -- 直接引用目标菜单栏项弹出的菜单
            click menu item 2 of menu of targetItem
        end tell
    end tell
end tell

相对定位的优势在于:无需依赖固定的菜单ID,避免因应用更新导致ID变化的问题,脚本兼容性更好。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 02:37:07