如何使用 AppleScript 检查菜单项是否被选中

How to use AppleScript check if a menu item checked

see my image

我想获取“设为系统代理”菜单项的值(是否勾选)

问题是,如果我想获得该值,我必须先单击该菜单并找到菜单项。但我希望它在后台完成,因为我希望它每 5 秒执行一次。

我的代码是这样的

tell application "System Events"
    tell process "ClashX"
        click (menu bar itm 1 of menu bar 2)
        get value of attribute "AXMenuItemMarkChar" of menu item "Set as system proxy" of menu 1 of menu bar item 1 of menu bar 2
    end tell
end tell

(^^^^ 这会一遍又一遍地打开那个菜单) 当我删除“单击(菜单栏 2 的菜单栏 itm 1)”行时,即

tell application "System Events"
    tell process "ClashX"
        get value of attribute "AXMenuItemMarkChar" of menu item "Set as system proxy" of menu 1 of menu bar item 1 of menu bar 2
    end tell
end tell

脚本无法完成,错误“系统事件发生错误:无法获取进程“ClashX”的菜单栏 2 的菜单栏项目 1 的菜单 1。无效索引。”进程“ClashX”的菜单栏 2 的菜单栏 1 的菜单 1 中的编号 -1719

如果您想知道设置为系统代理 菜单项是否被选中ClashX,你可以在其首选项 plist 文件,例如:

终端:

defaults read com.west2online.ClashX 'proxyPortAutoSet'

Returns 1 选中,0 选中。

如果您想在 AppleScript 中使用它,请使用 do shell script 命令,例如:

set menuItemIsChecked to ¬
    (do shell script ¬
        "defaults read com.west2online.CrashX 'proxyPortAutoSet'") ¬
        as boolean

if menuItemIsChecked then
    
    # Your code goes here.
    
    
end if