Is it possible to open a recent file with an AppleScript?
I tried the following script but it doesn't work.
on run
try
tell application "System Events"
set theApp to (get name of first process whose frontmost is true)
click menu bar item "Archivio" of menu bar 1 of process theApp
click menu item "Apri recente" of menu "Archivio" of menu bar item "Archivio" of menu bar 1 of process theApp
click menu item " test" of menu "Apri recente" of menu item "Apri recente" of menu "Archivio" of menu bar item "Archivio" of menu bar 1 of process theApp
end tell
end try
end run
The error is
--> error number -1728 from «class menI» " test" of «class menE» "Apri recente" of «class menI» "Apri recente" of «class menE» "Archivio" of «class mbri» "Archivio" of «class mbar» 1 of «class prcs» "AppleScript Editor"
It seems to be a bug when the "Apri recente" submenu is open.
But this is not necessary, you can do it in one click.
tell application "System Events"
tell (get first process whose frontmost is true)
click menu item " test" of menu "Apri recente" of menu item "Apri recente" of menu "Archivio" of menu bar item "Archivio" of menu bar 1
end tell
end tell
Related
I want to implement a alfred workflow to control my AirPods Pro to switch between "Transparency Mode" and "ANC Mode". How can I write an apple script to simulate click on "audio" menu bar to switch noise-canceling. Or there is a better solution?
I also had this problem, so I solved it in this way (with the error handling if the AirPods are not connected + popups):
tell application "System Events"
tell process "SystemUIServer"
click (menu bar item 1 of menu bar 1 whose description contains "volume")
try
click menu item "nestim AirPods Pro" of menu 1 of result
if value of attribute "AXMenuItemMarkChar" of menu item "Transparency" of menu 1 of result is "✓" then
click menu item "Noise Cancellation" of menu 1 of result
display notification "Noise Cancellation active" with title "Noise control:"
return "Noise Cancellation active"
else
click menu item "Transparency" of menu 1 of result
display notification "Transparency mode active" with title "Noise control:"
return "Transparency mode active"
end if
on error
tell application "System Events"
key code 53
display notification "Something went wrong" with title "Noise control:" sound name "Submarine"
return "Something went wrong"
end tell
end try
end tell
end tell
I found a simple apple script solution after trying.
tell application "System Events"
tell process "SystemUIServer"
click (menu bar item 1 of menu bar 1 whose description contains "volume")
click menu item "your AirPods name" of menu 1 of result
click menu item "noise control mode" of menu 1 of result
end tell
end tell
Change the your AirPods name to your AirPods name and change the noise control mode to which you want to (like Off, Noise Cancellation, or Transparency, or to your language as 关闭,降噪,通透模式 in Chinese).
Inspired by anton-uspehov's answer. I updated the script to automatic connect AirPods when it is not connected.
tell application "System Events"
tell process "SystemUIServer"
click (menu bar item 1 of menu bar 1 whose description contains "volume")
try
click menu item "your AirPods name" of menu 1 of result
click menu item "noise control mode" of menu 1 of result
on error
key code 53
click (menu bar item 1 of menu bar 1 whose description contains "bluetooth")
click menu item "your AirPods name" of menu 1 of result
click menu item "Connect" of menu 1 of result
end try
end tell
end tell
Or if your want to auto switch between Noise Cancellation and Transparency
tell application "System Events"
tell process "SystemUIServer"
click (menu bar item 1 of menu bar 1 whose description contains "volume")
try
click menu item "your AirPods name" of menu 1 of result
if value of attribute "AXMenuItemMarkChar" of menu item "Transparency" of menu 1 of result is "✓" then
click menu item "Noise Cancellation" of menu 1 of result
else
click menu item "Transparency" of menu 1 of result
end if
on error
key code 53
click (menu bar item 1 of menu bar 1 whose description contains "bluetooth")
click menu item "your AirPods name" of menu 1 of result
click menu item "Connect" of menu 1 of result
end try
end tell
end tell
For macos Big Sur (10.14) users, use the following script
set AirPodsName to "Your AirPods name"
tell application "System Events"
tell application process "ControlCenter"
set volMenu to menu bar item "volume" of menu bar 1
tell volMenu to click
set btCheckbox to checkbox 1 of scroll area 1 of group 1 of window "ControlCenter" whose title contains AirPodsName
set btCheckboxValue to value of btCheckbox
tell btCheckbox to click
delay 0.1
set checkbox_anc to checkbox 1 of scroll area 1 of group 1 of window "ControlCenter" whose title contains "Noise Cancellation"
if exists checkbox_anc then
if value of checkbox_anc is 1 then
set checkbox_transparent to checkbox 1 of scroll area 1 of group 1 of window "ControlCenter" whose title contains "Transparency"
tell checkbox_transparent to click
else
tell checkbox_anc to click
end if
end if
tell volMenu to click
end tell
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 can start a "Find" operation with the following applescript:
activate application "Firefox"
tell application "System Events"
tell process "Firefox"
click menu item "Find" of menu 1 of menu bar item "Edit" of menu bar 1
end tell
end tell
How can I then input a string to the find search?
activate application "Firefox"
tell application "System Events"
tell process "Firefox"
click menu item "Find" of menu 1 of menu bar item "Edit" of menu bar 1
end tell
keystroke myString
keystroke return
end tell
I have on Applescript to start and another one to stop Dictation. I tried to run them together and it only starts Dictation but does not stop it. I tried the key code way but this script is to be used with VoiceOver and that method does not work. Is there a way to have this script check if start/stop is ready and to act accordingly?
tell application "System Events"
tell (get first process whose frontmost is true)
tell menu bar 1
tell menu bar item "Edit"
tell menu 1
click menu item "Start Dictation"
end tell
end tell
end tell
end tell
end tell
tell application "System Events"
tell (get first process whose frontmost is true)
tell menu bar 1
tell menu bar item "Edit"
tell menu 1
click menu item "Stop Dictation"
end tell
end tell
end tell
end tell
end tell
You can use the exists command
tell application "System Events"
tell (get first process whose frontmost is true)
tell menu 1 of menu bar item "Edit" of menu bar 1
if exists of menu item "Start Dictation" then
click menu item "Start Dictation"
else if exists of menu item "Stop Dictation" then
click menu item "Stop Dictation"
end if
end tell
end tell
end tell
--
Or you can use a word in the name of the menu, like this:
tell application "System Events"
tell (get first process whose frontmost is true)
tell menu 1 of menu bar item "Edit" of menu bar 1
click (first menu item whose name contains " Dictation") -- start or stop
end tell
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