26

I'm trying to interact with a print dialog via Applescript.

What I want to emulate is the user setting a value on a specific dropdown.

Say I have:

tell application "System Events"
  tell process "Preview"
    set value of pop up button XXX to YYY
  end tell
end tell

How should I know the name XXX? For example, how would I distinguish between the "printer" and "presets" dropdowns?

I've opened the Accessibility Inspector, but from the information displayed there, I cannot spot a reference to the name or some other unique identifier for the specified dropdown.

I've seen many people using numbers to refer to the different dropdown, but I'm not sure this is good practice. What happens if Apple decide to swap the order of two dropdowns at some point?

Any help would be appreciated.

UPDATE:

Using 10.6.8 and inspector as suggested below, I get the following:

enter image description here

2
  • I found the UI elements command from here helpful as well: n8henrie.com/2013/03/a-strategy-for-ui-scripting-in-applescript
    – phs
    Commented Sep 11, 2013 at 5:46
  • Here's a link to another page in Ask Different that's linked to the current page, called: AppleScript – How can I get UI elements' names, attributes, properties, classes programmatically without “guessing” via Accessibility Inspector? (This here question was the reason why I wanted to learn some basic facts on "How-to"…) Commented Oct 30, 2018 at 8:52

5 Answers 5

20

UPDATE. this will work in 10.7.x but 10.6 has les element info.

The Buttons (drop downs ) in the Print Sheet have Description to describe the function.

In Accessibility inspector; you see this when hovering the mouse over the element (button). you can lock the Accessibility inspector's view with cmd+F7.

The Description will be listed as AXDescription

enter image description here

In the cases for the Printers its is Printers for Presets it is Presets

If you know the AXDescription you can avoid the numbers using something like this. But this is not the only way. Just one example.

activate application "Preview"
tell application "System Events"
    tell process "Preview"
        click ((pop up buttons of sheet 1 of window 1) whose description is "Printers")
    end tell
end tell

For the above to work in this example the Print Sheet must be visible along with 'Show Details'

The button/drop down has a menu. So you can select or click it by referring to the menu items of the menu of the button.

Either by number or using its title/AXTitle.

activate application "Preview"
    tell application "System Events"
        tell process "Preview"
            click ((pop up buttons of sheet 1 of window 1) whose description is "Presets")

click menu item "Last Used Settings" of menu of ((pop up buttons of sheet 1 of window 1) whose description is "Presets")
        end tell
    end tell

You can shorten repetitive code by using a variable for the button and calling that. When doing it like my example below;

   activate application "Preview"
tell application "System Events"
    tell process "Preview"
        set Presets_button to item 1 of ((pop up buttons of sheet 1 of window 1) whose description is "Presets")

        click Presets_button
        click menu item "Last Used Settings" of menu of Presets_button
    end tell
end tell
0
5

Use one of the scripts linked in this Mac OS X hint to find the appropriate form of address: Finding Control and Menu Items for use in AppleScript User Interface Scripting

IIRC, you'll need to click (cmd is in in System Events) the popup menu button before you can select a menu item from its menu.

1
  • Link is broken.
    – Merchako
    Commented Nov 10, 2023 at 0:58
4

For those who wonder, it seems that one location where to find the Accessibility Inspector in 10.7 is:

/Applications/Xcode.app/Contents/Applications

1

I recently write up my workflow for UI Scripting in AppleScript.

In short, the key component that made things much, much easier for me was learning that the UI Elements command in AppleScript returns a list of the terms that AppleScript Editor wants to see to call the UI Elements. Interface this with the terms seen from Accessibility Inspector (which are often slightly different) to build a working script.

Example:

tell application "System Events" to tell process "Safari"
    set frontmost to true
    delay 1
    
    UI elements 
end tell
0

Based on @n8henrie's blog post as linked in their answer, here's a full example:

Let's find the path for the "New Tab" button in Safari. Starting from the application name:

tell application "System Events" to tell process "Safari"
    set frontmost to true
    delay 1
    
    UI elements 
end tell

Result:

{window "Start Page" of application process "Safari" of application "System Events", menu bar 1 of application process "Safari" of application "System Events"}

Let's choose a window and see what elements it has:

tell application "System Events" to tell process "Safari"
    set frontmost to true
    delay 1
    
    tell window "Start Page"
        UI elements
    end tell
end tell

splitter group 1 of window "Start Page" of application process "Safari" of application "System Events", toolbar 1 of window "Start Page" of application process "Safari" of application "System Events", button 1 of window "Start Page" of application process "Safari" of application "System Events", button 2 of window "Start Page" of application process "Safari" of application "System Events", button 3 of window "Start Page" of application process "Safari" of application "System Events"}

Toolbar sounds promising, let's see what's in there:

tell application "System Events" to tell process "Safari"
    set frontmost to true
    delay 1
    
    tell toolbar 1 of window "Start Page"
        UI elements
    end tell
end tell

{group 1 of toolbar 1 of window "Start Page" of application process "Safari" of application "System Events", group 2 of toolbar 1 of window "Start Page" of application process "Safari" of application "System Events", group 3 of toolbar 1 of window "Start Page" of application process "Safari" of application "System Events", button 1 of toolbar 1 of window "Start Page" of application process "Safari" of application "System Events", button 2 of toolbar 1 of window "Start Page" of application process "Safari" of application "System Events", button 3 of toolbar 1 of window "Start Page" of application process "Safari" of application "System Events"}

Ah, we found some buttons. Ann after a little trial and error, we end up with this:

tell application "System Events" to tell process "Safari"
    set frontmost to true
    delay 1
    
    click button 2 of toolbar 1 of window "Start Page" -- open new tab
end tell

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .