1
votes

see my image

I want to get the value of the menu item "Set as system proxy" (whether if it has been checked).

The problem is if I want to get that value I have to click that menu first and find the menu item. But I want it to be done in the background since I want it to be executed every 5 seconds.

My code goes like this

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

(^^^^ This opens that menu over and over again) and when I delete the line "click (menu bar itm 1 of menu bar 2)", which is

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

the script cannot be done, error "System Events got an error: Can’t get menu 1 of menu bar item 1 of menu bar 2 of process "ClashX". Invalid index." number -1719 from menu 1 of menu bar item 1 of menu bar 2 of process "ClashX"

1
You aren't going to be able to do anything like that in the background, since the UI needs to be active in order to script it.red_menace

1 Answers

0
votes

If you want to know whether or not the Set as system proxy menu item is checked for ClashX, you can check the value of the proxyPortAutoSet key in its preferences plist file, e.g.:

In Terminal:

defaults read com.west2online.ClashX 'proxyPortAutoSet'

Returns 1 when checked, and 0 when not checked.

If you want to use it in AppleScript, use a do shell script command, e.g.:

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

if menuItemIsChecked then
    
    # Your code goes here.
    
    
end if