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.
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.
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.
The displays are controlled by the /Library/Preferences/com.apple.windowserver.plist
preference file:
- A flag controls whether the main display is the onboard screen the
DisplayMainOnInternal
key. - The
DisplaySets
key contains the list of the display sets. The first set is the one used (fact to check). - 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:
- Make a backup of the
/Library/Preferences/com.apple.windowserver.plist
(in case of) - Alter the display set the select the main display (
DisplaySets
andIOFlags
keys) by using thedefaults
command - Restart the Window Server:
killall -KILL SystemUIServer
Using AppleScript, you can invoke default
to write the setting to change the main monitor.