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

如何用AppleScript点击LG OnScreen Control无名称菜单项

使用AppleScript操作LG OnScreen Control切换显示器布局

需求背景

使用LG OnScreen Control应用切换不同显示器布局时,该应用的布局菜单项无文字名称,仅通过图标展示,需要编写AppleScript实现快速点击常用的第1个或第9个布局选项。
菜单项展示图

初始实现(仅打开布局菜单)

最开始仅完成了打开「Screen Split」布局菜单的代码:

ignoring application responses
    tell application "System Events" to tell process "OnScreen Control"
        click menu bar item 1 of menu bar 2
    end tell
end ignoring
delay 0.1
do shell script "killall System\\ Events"

tell application "System Events" to tell process "OnScreen Control"
    tell menu bar item 1 of menu bar 2
        click menu item "Screen Split" of menu 1
    end tell
end tell

初步可行方案(模拟按键选择)

通过多次模拟按下向下方向键+回车的方式实现选择,对应代码如下:

ignoring application responses
    tell application "System Events" to tell process "OnScreen Control"
        click menu bar item 1 of menu bar 2
    end tell
end ignoring
delay 0.1
do shell script "killall System\\ Events"

tell application "System Events" to tell process "OnScreen Control"
    tell menu bar item 1 of menu bar 2
        click menu item "Screen Split" of menu 1
        key code 125
        key code 125
        key code 125
        key code 125
        key code 125
        key code 125
        key code 125
        key code 125
        key code 76
    end tell
end tell

该方案可以实现需求,但效率较低,依赖固定次数的按键模拟,稳定性较差。

最终高效解决方案

无需模拟按键,哪怕子项没有名称也可以直接通过序号定位点击:

ignoring application responses
    tell application "System Events" to tell process "OnScreen Control"
        click menu bar item 1 of menu bar 2
    end tell
end ignoring
delay 0.1
do shell script "killall System\\ Events"

tell application "System Events" to tell process "OnScreen Control"
    tell menu bar item 1 of menu bar 2
        click menu item "Screen Split" of menu 1
        -- 要选择第几个布局就把下面的数字改成对应序号即可,比如选第1个就改为1
        click menu item 9 of menu 1 of menu item "Screen Split" of menu 1
    end tell
end tell

该方案直接通过菜单项的索引定位目标,执行效率更高,也不受菜单焦点变化影响,稳定性更强。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 01:06:04