Access Application Menu Item with AppleScript I am trying to create a global hotkey for a function of an app by building an AppleScript in Automator.app and then assign a hotkey to it in Service. But the AppleScript doesn't work.
Specifically, I'm trying to click the Set as system proxy item of ClashX.app.

and the item path is showed as below:

My AppleScript (sorry IDK the corr. syntax highlight):
on run {input, parameters}
    
    tell application &quot;ClashX&quot; 
        tell process &quot;ClashX&quot;
            click menu item &quot;Set as system proxy&quot; of menu 1 of menu bar item 1 of menu bar 2
        end tell
    end tell
end run

and it comes out
Syntax Error: Expected end of line but found “&quot;”.

Note: the process name seems to be &quot;ClashX&quot;, but I'm newbie to OS, so I post it here FYI.
The following example AppleScript code is how you'd click that target menu item:
tell application &quot;System Events&quot; to tell application process &quot;ClashX&quot;
    click menu bar item 1 of menu bar 2
    click menu item &quot;Set as system proxy&quot; of menu 1 of menu bar item 1 of menu bar 2
end tell

That said though, sometimes there can be a delay of several seconds, or more, between the first click and the second click. If you find that to be the case, then try the following example AppleScript code instead:
ignoring application responses
    tell application &quot;System Events&quot;
        click menu bar item 1 of menu bar 2 of application process &quot;ClashX&quot;
    end tell
end ignoring
do shell script &quot;killall 'System Events'&quot;
ignoring application responses
    tell application &quot;System Events&quot;
        click menu item &quot;Set as system proxy&quot; of menu 1 of menu bar item 1 of menu bar 2 of application process &quot;ClashX&quot;
    end tell
end ignoring

However, assigning a global keyboard shortcut for an Automator Service/Quick Action can be difficult at best because finding a keyboard shortcut that will not step on an already assigned keyboard shortcut in every application that you could trigger the Automator Service/Quick Action in will not be easy.
Additionally, every application you do successfully trigger the keyboard shortcut in will have to be added to System Preferences &gt; Security &amp; Privacy &gt; Privacy &gt; Accessibility, and that can be such a pain. Also, you actually may not want a given application to have accessibility privileges for security reasons.
To work around the accessibility privileges issues in a use case such as this I avoid Automator and just use an AppleScript script triggered by a keyboard shortcut using e.g. FastScripts, or a similar type of application. Using FastScripts I find that I typically only have to give it accessibility privileges and not the application that was frontmost when the keyboard shortcut was triggered.
This still leaves the global keyboard shortcut issue though and you may end up having to use four or five keys and the non-modifier key will most likely have to be a symbol.

Note: I am not affiliated with the developer of FastScripts, just a satisfied user of the product.