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.
The displays are controlled by the /Library/Preferences/com.apple.windowserver.plist
preference file:
DisplayMainOnInternal
key.DisplaySets
key contains the list of the display sets. The first set is the one used (fact to check).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:
/Library/Preferences/com.apple.windowserver.plist
(in case of)DisplaySets
and IOFlags
keys) by using the defaults
commandkillall -KILL SystemUIServer
The tool I wrote, displayplacer, does this.
Configure your screens how you like, drag the "white bar" to your primary screen in the macOS system settings, and then execute displayplacer list
. It will output the command to run to put your screens in their current configuration. The screen with origin:(0,0)
is the main display with the "white bar". Run this terminal command through a script, Automator, BetterTouchTool, etc.
Example profile 1 puts the white bar on the menu bar on the left monitor.
displayplacer "id:<leftScreenId> res:1920x1080 scaling:on origin:(0,0) degree:0" "id:<rightScreenId> res:1920x1080 scaling:on origin:(1920,0) degree:0"
Example profile 1 puts the white bar on the menu bar on the right monitor.
displayplacer "id:<leftScreenId> res:1920x1080 scaling:on origin:(1920,0) degree:0" "id:<rightScreenId> res:1920x1080 scaling:on origin:(0,0) degree:0"
Also available via Homebrew brew tap jakehilborn/jakehilborn && brew install displayplacer
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With