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"
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
Related
I can open the bluetooth dropdown menu item with this code but I don't know how to actually click on any of the items in the menu.
tell application "System Events" to tell process "ControlCenter"
click menu bar item "Bluetooth" of menu bar 1
end tell
What would be a command to click on something in the open bluetooth menu?
This following AppleScript code should accomplish what you are trying to achieve. Just replace the "Mac Pro" part of the code with the name of the item you want to click.
tell application "System Events" to tell process "ControlCenter"
click menu bar item "Bluetooth" of menu bar 1
repeat until exists of checkbox 1 of scroll area 1 of window 1
delay 0.1
end repeat
click checkbox "Mac Pro" of scroll area 1 of window 1
key code 53 -- Press escape key
end tell
This following AppleScript code will return the names of the checkboxes so you can easily know your options to use in the first code.
tell application "System Events" to tell process "ControlCenter"
click menu bar item "Bluetooth" of menu bar 1
repeat until exists of checkbox 1 of scroll area 1 of window 1
delay 0.1
end repeat
set checkBoxNames to name of checkboxes of scroll area 1 of window 1
end tell
I have an app called Fenêtre, when looking process name using top command it gives the name Fene?~Btre H.
I would like to click the item called 'a.py' under its menubar item as shown in figure.
My attempt:
attempt 1
tell application "System Events" to tell process "Fenêtre"
tell menu bar item 1 of menu bar 1
click
click menu item "Show all" of menu 1
end tell
end tell
Error:
$ osascript a.applescript
a.applescript:121:157: execution error: System Events got an error: Can’t get menu item "Show all" of menu 1 of menu bar item 1 of menu bar 1 of process "Fenêtre". (-1728)
Note that, when I run only first and last line of attemp1 it runs good, when I add middle lines it fails to run.
attempt 2
ignoring application responses
tell application "System Events" to tell process "Fenêtre"
click menu bar item 1 of menu bar 2
end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "Fenêtre"
tell menu bar item 1 of menu bar 2
click menu item "a.py" of menu 1
-- click menu item 1 of menu 1 -- another try
end tell
end tell
Updates (Still get errors)
tell application "System Events" to tell process "Fenêtre"
get entire contents of menu bar 2
end tell
This gives:
{menu bar item 1 of menu bar 2 of application process "Fenêtre" of application "System Events"}
References:
Applescript: on clicking Menu Bar item via gui script
applescript click menu bar option
https://superuser.com/questions/587815/can-applescript-osascript-be-used-to-click-menu-extra-menu-items
Applescript to show Apple menu bar items
Is AppleScript UI Scripting very slow in general, or is it my script, or something else?
Clicking an applications menu bar item with AppleScript
Thanks a lot.
Use a bundle identifier instead of the app name:
tell application "System Events"
tell (first application process whose bundle identifier is "BUNDLE_IDENTIFIER_HERE")
tell menu bar item 1 of menu bar 1
click
click menu item "Show all" of menu 1
end tell
end tell
end tell
I have made an AppleScript to interact with a menu bar item (NordVPN). Basically, it clicks the item, selects connect or disconnect, and that's it.
In developing this, I followed some advice in a response found here
It worked a couple of times, but now it just hangs and keeps "Running". Nothing is happening. I am wondering if the ignore responses is an issue? This was done to prevent a 5 second delay between clicks. Or could the two tries cause issue? I'm trying to ensure the script runs, whether there's "Connect" or "Disconnect".
Any advice is helpful. If someone has a suggestion for a better way to do this, I'll appreciate it. Thanks
Here's the code:
ignoring application responses
tell application "System Events" to tell process "NordVPN IKE"
click menu bar item 1 of menu bar 2
end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "NordVPN IKE"
tell menu bar item 1 of menu bar 2
try
click menu item "Connect" of menu 1
end try
try
click menu item "Disconnect" of menu 1
end try
end tell
end tell
EDIT: And now it's working again. It seems to work some of the time... But I cannot figure out why it stops working other times.
EDIT 2: It appears the issue arises when the Mac goes to sleep. When I wake it back up and try to run the script, it hangs. But if I manually click the menu bar item and then run the script, it'll work.
This works for me using the latest version of macOS high Sierra. Maybe this code will work a little better for you.
set disconnectExists to false
set connectExists to false
ignoring application responses
tell application "System Events"
launch application "NordVPN IKE"
delay 1
click menu bar item 1 of menu bar 2 of application process "NordVPN IKE"
end tell
end ignoring
do shell script "killall System\\ Events"
tell application "System Events"
repeat until disconnectExists or connectExists is true
set disconnectExists to menu item "Disconnect" of menu 1 of menu bar item 1 of menu bar 2 ¬
of application process "NordVPN IKE" exists
set connectExists to menu item "Connect" of menu 1 of menu bar item 1 of menu bar 2 ¬
of application process "NordVPN IKE" exists
end repeat
try
if connectExists is true then
delay 0.2
click menu item "Connect" of menu 1 of menu bar item 1 of menu bar 2 of ¬
application process "NordVPN IKE"
else if disconnectExists is true then
delay 0.2
click menu item "Disconnect" of menu 1 of menu bar item 1 of menu bar 2 of ¬
application process "NordVPN IKE"
end if
end try
end tell
This is an extension of a previous question
Applescript: on clicking Menu Bar item via gui script
On the highlighted menu item below for the f.lux menu bar, if you click it, there will be a checkmark indicating that the "Disable for an hour" feature has been enabled. What I'm trying to do is write a GUI AppleScript for f.lux where the user can decide to toggle the check mark by typing a keyword in Alfred followed by a 1 or a 0 where 1 serves to enable the "Disable for an hour" and 0, serves to keep it unchecked.
This is a screen shot of the menu bar for f.lux
I am however having a hard time figuring out what attribute to adjust for the "Disable for an hour" menu item in order to toggle the checkmark. Here is the code, but I get an unexpected token error when compiling it via applescript editor. So far what I'm trying to do is target the "menu item mark character" attribute indicated by the arrow in screenshot above, but I'm not sure if this is the right approach to toggle the "Disable for an hour" item. Can someone please give me advice?
on alfred_script(q)
set myOption to q as integer
ignoring application responses
tell application "System Events" to tell process "Flux"
click menu bar item 1 of menu bar 2
end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "Flux"
tell menu bar item 1 of menu bar 2
if myOption is 1 then
set ✓ to value of attribute "AXMenuItemMarkChar" of menu item "Disable for an hour" of menu 1
else if myOption is 0 then
set nil to value of attribute "AxMenuItemMarkChar" of menu item "Disable for an hour" of menu 1
end if
end tell
end tell
end alfred_script
This seems to work:
tell application "System Events" to tell process "Flux"
tell menu bar item 1 of menu bar 2
set v to (value of attribute "AXMenuItemMarkChar" of menu item "Disable for an hour" of menu 1) as string
if (v = "" and myOption = 1) or (v is not equal to "" and myOption = 0) then
click menu item "Disable for an hour" of menu 1
else
key code 53
end if
end tell
end tell
I'm trying to make an applescript for an application called F.lux that clicks the menu item "Disable for an Hour" as indicated in the screenshot below:
The element path is indicated in the screenshot below:
Here is my code thus far:
tell application "System Events"
tell process "Flux"
click (menu bar item 1 of menu bar 2)
click menu item "Disable for an hour" of menu 1 of menu bar item 1 of
menu bar 2
end tell
end tell
Everything compiles fine, however I keep getting the error message below when I attempt to run the script:
error "System Events got an error: Can’t get menu 1 of menu bar item 1 of menu bar 2 of process "Flux". Invalid index." number -1719 from menu 1 of menu bar item 1 of menu bar 2 of process "Flux"
Can someone pinpoint where I'm going wrong with this?
This worked for me, but there is a delay of about 5 seconds after the first click command.
tell application "System Events" to tell process "Flux"
tell menu bar item 1 of menu bar 2
click
click menu item "Disable for an hour" of menu 1
end tell
end tell
One workaround is to use ignoring application responses and terminate System Events after the click command:
ignoring application responses
tell application "System Events" to tell process "Flux"
click menu bar item 1 of menu bar 2
end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "Flux"
tell menu bar item 1 of menu bar 2
click menu item "Disable for an hour" of menu 1
end tell
end tell