tags:

views:

795

answers:

4

From the Displays pane in System Preferences, I can manually change the main monitor by dragging the menu bar from one display to the other. I'd like to automate this and make it part of an AppleScript.

A: 

Much like you can tell System Events.app to sleep your Mac, you can tell Image Events.app to mess with your displays. The Image Events application provides a "displays" collection. Each display has a "profile" with lots of goodies. However, everything I just mentioned is read-only, so I don't have a good way to do it from within script.

You might have better luck in Automator – Hit record, run System Preferences, go to Displays, drag the menu bar to the other screen, and hit stop. I bet something will work.

Kevin Conner
Recording with Automator doesn't seem to be able to record opening System Preferences (easy to work around) or dragging the menu bar.
Michael Tsai
+1  A: 

You should see if you can do it via AppleScript's User Interface Scripting. It allows you to manipulate an application's GUI elements; useful when the app doesn't support scripting directly. I'd test it myself but I don't have any extra displays lying around.

Here's a pretty good overview by MacTech.

benzado
It doesn't look to me like the relevant controls are accessible via GUI scripting. I suppose I could generate mouse clicks and drags, but the coordinates would be different for different Macs and display arrangements.
Michael Tsai
+1  A: 

The displays are controlled by the /Library/Preferences/com.apple.windowserver.plist preference file:

  1. A flag controls whether the main display is the onboard screen the DisplayMainOnInternal key.
  2. The DisplaySets key contains the list of the display sets. The first set is the one used (fact to check).
  3. In the set, each item contains the screen properties. The IOFlags key seems to indicate if the display is the main one (value of 7) or not (value of 3).

Before going Apple Script, you may change the display configuration by hand, and save a copy of the /Library/Preferences/com.apple.windowserver.plist file to study it.

Note that the following procedure has not been tested !!!

With AppleScript, the keys in the plist file are changed individually, in order to change the main display:

  1. Make a backup of the /Library/Preferences/com.apple.windowserver.plist (in case of)
  2. Alter the display set the select the main display (DisplaySets and IOFlags keys) by using the defaults command
  3. Restart the Window Server: killall -KILL SystemUIServer
Laurent Etiemble
A: 

Using AppleScript, you can invoke default to write the setting to change the main monitor.

kiamlaluno