pistacchio
pistacchio

Reputation: 58873

Applescript getting elements

This SO answer pointed me a bit further in scripting the change of screen resolution on the new MacBook Retina. I'm stuck here:

enter image description here

I can arrive to this pane with this script:

tell application "System Preferences"
    activate
    set the current pane to pane id "com.apple.preference.displays"
    reveal anchor "displaysDisplayTab" of current pane
    get elements of current pane
    tell application "System Events"

    end tell
    --get the name of every anchor of current pane
end tell

But how to know how to select bits if this pane contents? How to refer to the "Scale" radio button, for instance, and select one of the 5 possible resolutions? Thanks

Upvotes: 2

Views: 8882

Answers (2)

Jeremy
Jeremy

Reputation: 13

Image to tell you what radio button number you need for property: _MS on line 7

use sys : application "System Events"
use prefs : application "System Preferences"

property prefsUI : a reference to process "System Preferences"
property _W : a reference to windows of prefsUI
property _DD : a reference to radio button "Default for display" of radio group 1 of tab group 1 of _W
property _SD : a reference to radio button "Scaled" of radio group 1 of tab group 1 of _W
property _MS : a reference to radio button 4 of radio group 1 of group 2 of tab group 1 of _W

property pane : "com.apple.preference.displays"
property anchor : "displaysDisplayTab"
property tab : anchor (my anchor) of pane id (my pane)

set defaultDisplay to null
if tab ≠ (reveal tab) then return null

tell _DD to if exists then set defaultDisplay to its value as boolean

if defaultDisplay is false then
    click _DD
else
    click _SD
    click _MS
end if

quit prefs

The script alternates between Default Resolution and "Most Space" Resolution depending on which is currently active.

Upvotes: 1

Protector one
Protector one

Reputation: 7261

I found this piece of AppleScript that apparently retrieves every UI element available in a Window:

tell application "System Events"
tell process "Process Name"
set visible to true
return every UI element of front window
return name of every UI element of front window
end tell
end tell

(Haven't tested myself yet. Found here.)

Upvotes: 8

Related Questions