Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you programmatically press a toolbar button with AppleScript?

I hope this isn't too obvious, but I'd like to press one of the toolbar buttons within an application by means of AppleScript.

Background: The button doesn't have any menu item or keyboard shortcut. Thus, I can't activate it by any of those methods; I need to find an AppleScript way of actually 'pressing' the button.

like image 941
Henrik Avatar asked Feb 26 '23 20:02

Henrik


2 Answers

I am going to expand a little on what @hced said.

I agree that click button 3 of tool bar 1 of window 1 works in many cases. However from OS to OS thic can be a moving target. My example is iTunes. In iTunes 10.5.8 the airplay button is 17 where in 10.7.5 it is 25. However, in both instances it is called "AirPlay"!

So I suggest using the developer kit, and getting this code from Apple: http://developer.apple.com/library/mac/#samplecode/UIElementInspector/Introduction/Intro.html

Run the application and whatever you mouse over will get you all the info you need to make this work in your AppleScripts:

click (every button whose description is "AirPlay") of window "iTunes" of application process "iTunes"

In this case there should only be one button with this description, but you can use the tool to discover whether this is the case. If so, limit the scope!

Hope this helps!! Bo

like image 52
roberthuttinger Avatar answered Mar 02 '23 13:03

roberthuttinger


Ah, figured out that one myself, too. Seems I'm on a self-commenting spree here.

This example worked for me, referencing a specific toolbar button by means of its description attribute:

tell application "System Events"
    tell process "OmniFocus"
        click (every button whose description is "Contexts") of tool bar 1 of window 1
    end tell
end tell

I'll leave it up for any commenters with an opinion about referencing every button whose descripton is "Contexts" (as per my example), to express those down below. But I presume there can't be multiple buttons with the same description, right?

like image 25
Henrik Avatar answered Mar 02 '23 13:03

Henrik