<p>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.</p>

[{'answer_id': 70273, 'author': 'easeout', 'author_id': 10906, 'author_profile': 'https://Stackoverflow.com/users/10906', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n\n<p>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.</p>\n'}, {'answer_id': 1894987, 'author': 'benzado', 'author_id': 10947, 'author_profile': 'https://Stackoverflow.com/users/10947', 'pm_score': 1, 'selected': False, 'text': '<p>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.</p>\n\n<p>Here\'s <a href="http://www.mactech.com/articles/mactech/Vol.21/21.06/UserInterfaceScripting/index.html" rel="nofollow noreferrer">a pretty good overview by MacTech</a>.</p>\n'}, {'answer_id': 1906298, 'author': 'Laurent Etiemble', 'author_id': 121278, 'author_profile': 'https://Stackoverflow.com/users/121278', 'pm_score': 2, 'selected': False, 'text': '<p>The displays are controlled by the <code>/Library/Preferences/com.apple.windowserver.plist</code> preference file:</p>\n\n<ol>\n<li>A flag controls whether the main display is the onboard screen the <code>DisplayMainOnInternal</code> key.</li>\n<li>The <code>DisplaySets</code> key contains the list of the display sets. The first set is the one used (fact to check).</li>\n<li>In the set, each item contains the screen properties. The <code>IOFlags</code> key seems to indicate if the display is the main one (value of 7) or not (value of 3).</li>\n</ol>\n\n<p>Before going Apple Script, you may change the display configuration by hand, and save a copy of the <code>/Library/Preferences/com.apple.windowserver.plist</code> file to study it.</p>\n\n<p><strong><em>Note that the following procedure has not been tested !!!</em></strong></p>\n\n<p>With AppleScript, the keys in the plist file are changed individually, in order to change the main display:</p>\n\n<ol>\n<li>Make a backup of the <code>/Library/Preferences/com.apple.windowserver.plist</code> (in case of)</li>\n<li>Alter the display set the select the main display (<code>DisplaySets</code> and <code>IOFlags</code> keys) by using the <code>defaults</code> command</li>\n<li>Restart the Window Server: <code>killall -KILL SystemUIServer</code></li>\n</ol>\n'}, {'answer_id': 1911234, 'author': 'apaderno', 'author_id': 225647, 'author_profile': 'https://Stackoverflow.com/users/225647', 'pm_score': 0, 'selected': False, 'text': '<p>Using AppleScript, you can invoke <code>default</code> to write the setting to change the main monitor.</p>\n'}, {'answer_id': 56201151, 'author': 'Jake Hilborn', 'author_id': 1330546, 'author_profile': 'https://Stackoverflow.com/users/1330546', 'pm_score': 3, 'selected': False, 'text': '<p>The tool I wrote, <a href="https://github.com/jakehilborn/displayplacer" rel="noreferrer">displayplacer</a>, does this.</p>\n\n<p>Configure your screens how you like, drag the "white bar" to your primary screen in the macOS system settings, and then execute <code>displayplacer list</code>. It will output the command to run to put your screens in their current configuration. The screen with <code>origin:(0,0)</code> is the main display with the "white bar". Run this terminal command through a script, Automator, BetterTouchTool, etc.</p>\n\n<p>Example profile 1 puts the white bar on the menu bar on the left monitor.\n<code>displayplacer "id:&lt;leftScreenId&gt; res:1920x1080 scaling:on origin:(0,0) degree:0" "id:&lt;rightScreenId&gt; res:1920x1080 scaling:on origin:(1920,0) degree:0"</code></p>\n\n<p>Example profile 1 puts the white bar on the menu bar on the right monitor.\n<code>displayplacer "id:&lt;leftScreenId&gt; res:1920x1080 scaling:on origin:(1920,0) degree:0" "id:&lt;rightScreenId&gt; res:1920x1080 scaling:on origin:(0,0) degree:0"</code></p>\n\n<p>Also available via Homebrew <code>brew tap jakehilborn/jakehilborn &amp;&amp; brew install displayplacer</code></p>\n'}]
['https://Stackoverflow.com/questions/68447', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6311/']