How to simulate a click in another application’s menu? - swift

For instance, what is the pure Swift equivalent of the following AppleScript:
tell application "System Events" to tell process "Finder"
click menu item "New Finder Window" of menu 1 of menu bar item "File" of menu bar 1
end tell

Related

Applescript not clicking menu item

I'm trying to add shortcuts to my Notes app for typing in superscript and subscript. The script below does not give any errors and correctly seems to find/click the 'Superscript' submenu item, but when returning to the notes app this option is not activated/selected. If I uncomment the line below it, that functionality (opening 'Notes Help') works perfectly.
tell application "System Events"
tell process "Notes"
click menu item "Superscript" of menu 1 of menu item "Baseline" of menu 1 of menu item "Font" of menu "Format" of menu bar 1
-- click menu item "Notes Help" of menu "Help" of menu bar 1
end tell
end tell
What am I doing wrong?
AppleScript code is usually all about personal style, but I just wrote this up real quick from probing the menubar. Tested and working properly.
tell application "Notes" to activate -- needed because you say "but when returning to the notes app" meaning you're not there already
-- tell System Events to Click, not tell system events to tell process to click... process doesn't understand click.
tell application "System Events"
click menu item "Superscript" of menu "Baseline" of menu item "Baseline" of menu "Font" of menu item "Font" of menu "Format" of menu bar item "Format" of menu bar 1 of application process "Notes" of application "System Events"
end tell

On Numbers (Mac), how can I export (save) a chart using Python or Apple Script

I have a numbers chart with mulitple plot in a sheet.
And I would like to save that chart in jpeg/pdf using python or applescript.
With AppleScript:
activate application "Numbers"
tell application "System Events"
tell process "Numbers"
click menu item "PDF…" of menu 1 of menu item "Export To" of menu 1 of menu bar item "File" of menu bar 1
end tell
end tell
Note that "…" is one character (ellipsis). If you're trying to use three dots "..." it will not work. The script only navigates you to the 'Export Your Spreadsheet' dialog. Do you want the script to set the file name, saving location and so on?

If check box is already selected, then quit application

I created a script to be activated by using the dictation command "Enable Sharing" which successfully opens up System preferences/ Sharing/ then it will click the checkbox automatically whether it is enabled or disabled already.
I would like to have two dictation commands, one for enable sharing and one for disable sharing. How can I add a conditional that when I say "Enable Sharing" it will run through the script and if the sharing checkbox is already selected, just quit system preferences instead of clicking that checkbox if it is already selected which actually would deselect that checkbox?
activate application "System Preferences"
delay 1
tell application "System Events"
tell process "System Preferences"
click button "Sharing" of scroll area 1 of window "System Preferences"
end tell
end tell
activate application "System Preferences"
delay 1
tell application "System Events"
tell process "System Preferences"
click checkbox 1 of row 2 of table 1 of scroll area 1 of group 1 of window "Sharing"
end tell
end tell
tell application "System Preferences"
quit
end tell
Here, I fixed it for you.
activate application "System Preferences"
delay 1
tell application "System Events"
tell process "System Preferences"
click button "Sharing" of scroll area 1 of window "System Preferences"
end tell
end tell
activate application "System Preferences"
delay 1
tell application "System Events"
tell process "System Preferences"
set theCheckbox to checkbox 1 of row 2 of table 1 of scroll area 1 of group 1 of window "Sharing"
tell theCheckbox
if false then click theCheckbox
end tell
end tell
end tell
tell application "System Preferences"
quit
end tell
Eureka!!! After trying all sorts of different combinations.. This is the script that actually worked as I needed it to..
activate application "System Preferences"
delay 1
tell application "System Events"
tell process "System Preferences"
click button "Sharing" of scroll area 1 of window "System Preferences"
end tell
end tell
activate application "System Preferences"
delay 1
tell application "System Events"
tell process "System Preferences"
tell checkbox 1 of row 2 of table 1 of scroll area 1 of group 1 of window "Sharing" to if value is 0 then click
delay 1
end tell
end tell
tell application "System Preferences"
quit
end tell

applescript click menu bar option

I want to create an AppleScript to click a menu but I can't what ever the script I using, here is the menu I want :
ELEMENT :
Role : menu item
Title: "sign in As..."
Description :
Help:
Application: SytemUIServer
ELEMENT PATCH (starting at leaf element):
menu Item "Sign in As..." (menu item 12)
menu (menu 1)
menu extra (menu bar item 2)
menu bar (menu bar 1)
application "SystemUIServer"
So I did a few script, the last one was
ignoring application responses
tell application "System Events" to tell process "SystemUIServer"
click menu bar item 2 of menu bar 1
end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "SystemUIServer"
tell menu item 12 of menu 1 of menu bar item 2 of menu bar 1
click
end tell
end tell
Also I just realise that the position can change (some time the item I want to click is menu item 12 sometime its 10 etc.)
In your question, you didn't specify name of the menu bar item and name of the app which owns the menu bar item. That's the problem.
First, SystemUIServer only run menu bar items/icons native to OS X. To see the icons it runs, do these three lines separately in Script Editor.
1)
tell application "System Events" to tell process "SystemUIServer" ¬
to number of menu bars
2)
tell application "System Events" to tell process "SystemUIServer" ¬
to value of attribute "AXDescription" of menu bar items of menu bar 1
3)
tell application "System Events" to tell process "SystemUIServer" ¬
to title of menu bar items of menu bar 2
The results should look something like:
1) 2
2) {"Wi-Fi, four of four bars, with WiFiName.", "Battery: Charged ", "Clock"}
3) {"Notification Center", missing value}
Third-party apps, plus Spotlight, manage its own menu bar items/icons. Spotlight for example:
tell application "System Events" to tell process "Spotlight" ¬
to title of menu bar items of menu bar 1
This gives you: {"Spotlight"}
If you have Caffeine:
tell application "System Events" to tell process "Caffeine" ¬
to title of menu bar items of menu bar 1
You get: {missing value}, 'cause its programer didn't bother naming the item.
So if this is third-party menu bar item you are trying to script, it's not in SystemUIServer. If you only refer to the menu bar item with position instead of its name, you cannot reliably click it every time.
McUsr inserted this line, in order to save the edit that had to be of minimum 6 characters.

Connect to bluetooth device (iPhone) via command line on MacOSX

I'm trying to figure out a way to connect to my iPhone via Bluetooth with a shell script. I'm currently using an applescript which essentially does this through UIElements, but I'm wondering if this can be done with a command line utility, a.l.a. blueutil but with the ability to connect to the device not just turn on/off bluetooth? Thanks for the consideration.
-Afshin
This answer is very similar to #Wolph's answer; however after fighting other issues this is what I came up with. I like it better for two reasons:
# It will not disconnect the device if it is already connected
# Nice output from osascript
Just save it in a file and call osascript path/to/file.applescript
This was working on OSX Mavericks 10.9.2 as of 11-Apr-2014, although you may need to grant access to whatever method you use to run this in the security preferences panel. See this Apple KB: http://support.apple.com/kb/HT5914
All you should have to do is change the "LG HBS730" string to match the name of your device and you should be set. The returns are there so you get nice output from osascript.
activate application "SystemUIServer"
tell application "System Events"
tell process "SystemUIServer"
-- Working CONNECT Script. Goes through the following:
-- Clicks on Bluetooth Menu (OSX Top Menu Bar)
-- => Clicks on LG HBS730 Item
-- => Clicks on Connect Item
set btMenu to (menu bar item 1 of menu bar 1 where description is "bluetooth")
tell btMenu
click
tell (menu item "LG HBS730" of menu 1)
click
if exists menu item "Connect" of menu 1
click menu item "Connect" of menu 1
return "Connecting..."
else
click btMenu -- Close main BT drop down if Connect wasn't present
return "Connect menu was not found, are you already connected?"
end if
end tell
end tell
end tell
end tell
I had to change a bit to get Andrew Burns's answer to work for me in Yosemite; his code keeps giving me
connect-mouse.scpt:509:514: execution error: System Events got an error: Can’t get menu 1 of menu bar item 3 of menu bar 1 of application process "SystemUIServer". Invalid index. (-1719)
Seems like selecting the menu bar item that way lets you click on it, but not get to the menu, for some inscrutable applescript reason. This very similar code works for me:
tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
click bt
tell (first menu item whose title is "The Device Name") of menu of bt
click
tell menu 1
if exists menu item "Connect"
click menu item "Connect"
return "Connecting..."
else
click bt -- close main dropdown to clean up after ourselves
return "No connect button; is it already connected?"
end if
end tell
end tell
end tell
I don't know the difference between (first menu bar item whose description is "bluetooth") of menu bar 1 and (menu bar item 1 of menu bar 1 where description is "bluetooth"), but....
Updating this for High Sierra 10.13.2, based on the answers provided by Andrew Burns and dougal.
set DeviceName to "LG HBS730"
tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
click bt
if exists menu item DeviceName of menu of bt then
tell (first menu item whose title is DeviceName) of menu of bt
click
tell menu 1
if exists menu item "Connect" then
click menu item "Connect"
return "Connecting..."
else
key code 53 -- hit Escape to close BT menu
return "No connect button; is it already connected?"
end if
end tell
end tell
else
key code 53 -- hit Escape to close BT menu
return "Cannot find that device, check the name"
end if
end tell
Changes:
Clicking on the Bluetooth icon no longer closes the menu. Solved by hitting the Escape key, per this answer and confirmed on this page
Checking to see if the device is listed in Bluetooth menu, to detect incorrect names. (I spent quite a while debugging, only to realize ' isn't the same as ’...)
Hope this helps others, not trying to steal karma!
After messing about a bit with Applescript I wrote this little bit of Applescript to connect:
Note that the "Show Bluetooth in menu bar" checkbox needs to be on for this as it's effectively just using that menu.
tell application "System Events"
tell process "SystemUIServer"
tell (menu bar item 1 of menu bar 1 whose description is "bluetooth")
click
-- You can use your phone name as well over here if you have multiple devices
-- tell (menu item "YOUR_PHONE_NAME_HERE" of menu 1)
tell (menu item 1 of menu 1)
click
tell (menu item 1 of menu 1)
click
end tell
end tell
end tell
end tell
end tell
This tool1 allowed me to connect to bluetooth headphones from command line.
As far as I know you can only turn on/off bluetooth and check the status in command line (using blueutil or manipulating with launchctl). But you can use your applescript routines via osascript in shell.
Dougal's answered worked for me.
You can also use Automator to create a service that can be run from anywhere and assign it a Keyboard Shortcut in Keyboard Shortcut Preferences for an even faster workflow.
I have multiple bluetooth audio device(s). So Andrew's script is very helpful. Here's my modification to his script. I'm not a programmer/IT person, just learned some bits from the internet.
set btchoice to BT_Choice()
on BT_Choice()
display dialog "Choose the device of your choice" with title "Selecting Device" buttons {"Bluedio", "Reconnect S-TS", "Anker A7721"} default button "Reconnect S-TS"
set Ndialogresult to the result
set DNameSel to button returned of Ndialogresult
display dialog "Whether to Connect or Disconnect the Device" with title "Handling Bluetooth" buttons {"Connect", "Disconnect", "Cancel"} default button "Connect"
set Bdialogresult to the result
set Bbuttonsel to button returned of Bdialogresult
activate application "SystemUIServer"
tell application "System Events"
tell process "SystemUIServer"
set btMenu to (menu bar item 1 of menu bar 1 where description is "bluetooth")
tell btMenu
click
tell (menu item DNameSel of menu 1)
click
if exists menu item Bbuttonsel of menu 1 then
click menu item Bbuttonsel of menu 1
return "Connecting..."
else
click btMenu -- Close main BT drop down if Connect wasn't present
return "Connect menu was not found, are you already connected?"
end if
end tell
end tell
end tell
end tell
end BT_Choice