Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the main display via AppleScript?

Tags:

applescript

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.

like image 286
Michael Tsai Avatar asked Sep 16 '08 01:09

Michael Tsai


3 Answers

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
like image 112
Laurent Etiemble Avatar answered Nov 01 '22 00:11

Laurent Etiemble


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

like image 6
Jake Hilborn Avatar answered Nov 01 '22 01:11

Jake Hilborn


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.

like image 1
benzado Avatar answered Oct 31 '22 23:10

benzado