Add Display via AppleScript

Hi all, I hope you can assist. I'm trying to create a macro that adds my iPad as an additional display and move and resize some windows once its added. I'd like to, using Applescript, select the iPad displayed on this screenshot:

I'm having trouble with the Applescript though. Using UI Browser from Pfiddlesoft, I was able to create the Applescript below which works, but it only works if the System Preferences Pane is already open and the Displays option has been selected. How can I get the Applescript to work regardless of the status of the System Preferences pane - ie if it's closed or if it is in a different state?

tell application "System Preferences" to activate

delay 1

tell application "System Events"
   tell process "System Preferences"
      delay 1
      click window "Displays"
      tell pop up button "Add Display" of window "Displays"
         click
         delay 1
         click menu item "Jaco's iPad" of menu 1
      end tell
   end tell
end tell

Hey Jaco,

You'll have to do something like this:

tell application "System Preferences"
   if not running then run
   activate
   reveal anchor "displaysArrangementTab" of pane id "com.apple.preference.displays"
end tell

-Chris

1 Like