Applescript to click on a specific icon in the Mac Menu Bar - macos

Sometimes I use PdaNet to tether using my iPhone. The desktop client for OSX is not as rich as the one for windows. One of the chief differences is, that the OSX does not allow to automatically connect to iPhone as soon as the latter is plugged in.
Would you know of a way using Applescript to click on the PdaNet icon on the Menu Bar and then select and click the 'Connect' option on it ?
Here is what the 'PdaNetMac' application's menu bar icon looks like:
I have looked at the following questions but am an applescript newbie and am not sure how to search for PdaNet's icon on the menu bar:
Click menu item on Mac OSX Lion using AppleScript
Applescript: on clicking Menu Bar item via gui script
Accessing dock icon right-click menu items with AppleScript
I have confirmed that 'Enable Access for assistive devices' is enabled.
Based on the second question above, Here is my current attempt at doing this:
ignoring application responses
tell application "System Events" to tell process "PdaNet"
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 "PdaNet"
tell menu bar item 1 of menu bar 2
click menu item "Connect" of menu 1
end tell
end tell
Interestingly, the above script works for me fine when I change PdaNet to Flux.
Thanks!!

You were very close !!
I just downloaded the PdaNet application to test this, and the only edit I had to make to your script was change PdaNet to 'PdaNetMac` ( I was thinking that this is the Process Name and so used the process name displayed in Activity Monitor).
So this works for me:
ignoring application responses
tell application "System Events" to tell process "PdaNetMac"
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 "PdaNetMac"
tell menu bar item 1 of menu bar 2
click menu item "Connect" of menu 1
end tell
end tell
Hope this works for you too !!
(Very useful script, btw. Cheers !)

Related

Apple Script Error: Can't continue click

I'm trying to open a messaging application (it does not have an Apple Script Dictionary (command + shift + o)), click on text, and type into the text box, and hit send.
Pop up: Script Error - Telegram got an error: Can't continue click after the application becomes active.
Result Tab: error "Telegram got an error: Can’t continue click." number -1708
P.S., The messaging application is Telegram.
Apple Script:
tell application "Telegram"
activate
delay 1
click on text "chat name"
keystroke "some text"
//assuming this works because text box is the first responder when the chat opens.
click on text "Send"
end tell
If an application lacks an AppleScript dictionary, any command except the standard commands launch, activate, open, reopen and quit will throw an error.
The solution is GUI scripting: The built-in application System Events is the bridge to send mouse clicks and keyboard events to the target application.
I don't know the application Telegram at all, so this code might fail, but it might also be a starting point
activate application "Telegram"
tell application "System Events"
tell process "Telegram"
tell window 1
keystroke "some text"
click button "Send"
end tell
end tell
end tell
You have two choices for a 3rd party app that lacks an AppleScript dictionary.
Option 1:
Use System Events as described above to perform an action on an element, e.g. click a button, keystroke text into a field, etc. The trick is to identify the element in syntax that is recognized by Applescript. Besides UIElementInspector mentioned above, which can be confusing and occasionally wrong/incomplete, you can also run the following commands in a separate Applescript Editor. For example, to get all UI elements for the active window (window 1) in Telegram:
tell application "System Events" to tell application process "Telegram" to tell window 1
UI elements
end tell
To get all UI elements for the main menu bar in Telegram:
tell application "System Events" to tell application process "Telegram" to tell menu bar 1
UI elements
end tell
In each case the Result pane will display a comma delimited list of all available UI elements in that window or menu bar. Moreover, the syntax as listed is guaranteed to be recognizable by Applescript. Just identify the correct element and tell System Events to tell it what to do.
For example if you want to click the Menu item "Format" In TextEdit first run the following:
tell application "System Events" to tell application process "TextEdit" to tell menu bar 1
UI elements
end tell
Among the results in the Result pane will be the following:
menu bar item "Format" of menu bar 1 of application process "TextEdit" of application "System Events"
Convert that to Applescript, run the script and it will click the "Format" Menu:
tell application "TextEdit" to activate --you need TexEdit up and running to click its menu bar
tell application "System Events" to click menu bar item "Format" of menu bar 1 of application process "TextEdit"
For submenus, etc. you just iterate the process asking for UI elements for the submenu. GUI scripting is iterative and empirical.
Option 2:
Download the free Terminal/Command Line app cliclick which allows you to click on any point in the screen. The screen coordinates you want to click can be manually identified with your cursor by holding down command + shift + 4.

Applescript to click on a specific application in the Mac Status Bar

I have an OS X application that runs as a status bar app. I am trying to do a test automation. Is there a way to click the status bar app using Apple Script?
The script given below works only on native apps.
tell application "System Events" to tell process "SystemUIServer" to click menu bar item 1 of menu bar 1
I am looking for a similar functionality on custom apps.
Not all Menu Bar Extras (or status icons) can be accessed through UI Scripting. You have to experiment with whether they can be called through a unique description or other value.
Play with code like this to see if you can figure it out:
tell application "System Events" to tell process "SystemUIServer"
tell (menu bar item 1 of menu bar 1 where description is "clock")
click
click menu item "Open Date & Time Preferences…" of menu 1
end tell
end tell
If you can't find it there, you can try menu bar 2 for Status items.

AppleScript: Menu bar item click (but no AXDescription)

I'd like to AppleScript to click a menu bar icon (not a system process) and then click on one of the menu items. I'm using OS X 10.10.
I've read that I can use the Accessibility Inspector to find the AXDescription of the item, however, I can't find it anywhere in the inspector (in fact none of the items I inspected seemed to have this property).
I also tried the method described here but get an error on line 10:
ignoring application responses
tell application "System Events" to tell process "Webcam Settings"
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 "Webcam Settings"
tell menu bar item 1 of menu bar 2
click menu item "Show Webcam Settings Panel" of menu 1
end tell
end tell
System Events got an error: Can’t get menu 1 of menu bar item 1 of menu bar 2 of process "Webcam Settings". Invalid index.
There are some menu bar apps which you can click and show menu with applescript regardless of if they appear in dock.
But I am afraid you can’t access the menubar of “Webcam Settings” with applescript.
Because there isn’t key named “isAccessbiliyFocused” in the attributes.
Menu bar apps you can show their menu with applescript have that key and its value is “Yes”.
I am not quite sure about my answer but as far as I investigate this issue, my answer is probably right.

Applescript: hiding manipulation of gui when running a gui applescript

I'm working on an applescript that clicks menu items in an application menu bar. I'd like to run this script without it displaying the whole process of clicking the target menu item. For example, here's some code below to click the disable for an hour menu item in the menu bar for the F.lux application
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
How do I go about preventing the display of the gui manipulation when running the this script?
You cannot do this. For AppleScript GUI scripting to work the UI elements must be front and key as they would be for the human.

Applescript to de-authorize a computer for iTunes

I have a computer (Mac) that is authorized to play music through iTunes which I want to disable. But, I only have ssh access to the machine. Is there an applescript I can run remotely (eg via the terminal) that I can use to de-authorize the machine?
I know that I can unauthorize all the machines I have authorized, but I would prefer to use this solution if it is possible.
I don't see any properties to authorize/deauthorize in iTunes dictionary but I just played around with GUI scripting and came up with a solution. So, the target Mac will have to have GUI Scripting enabled for the script below to work.
tell application "System Events"
tell process "iTunes"
click menu item "Deauthorize This Computer…" of menu 1 of menu bar item "Store" of menu bar 1
delay 1
set frontmost to true
click menu 1 of menu bar item "Store" of menu bar 1
set value of text field 1 of window "Deauthorize This Computer" to "password"
click button "Deauthorize" of window "Deauthorize This Computer"
end tell
end tell
You could leave that AppleScipt on your target Mac and then just use the open command to launch it. Or you could copy the above AppleScript and paste it into a shell script and use the HEREDOC method with osascript.
The full example of this looks like this:
osascript<<END
tell application "System Events"
tell process "iTunes"
click menu item "Deauthorize This Computer…" of menu 1 of menu bar item "Store" of menu bar 1
delay 1
set frontmost to true
click menu 1 of menu bar item "Store" of menu bar 1
set value of text field 1 of window "Deauthorize This Computer" to "password"
click button "Deauthorize" of window "Deauthorize This Computer"
end tell
end tell
END
The above method works well with Apple Remote Desktop's Send Unix feature as well.
Also, note that the password is included in this script which I don't recommend
but it is needed on the deauthorize window. If you put the password in the script make sure to secure that script so no one gets ahold of your Apple password.

Resources