MacBook – How to swap the primary display automatically in a dual-monitor setup

applescriptdisplaymacbook prothunderbolt

I have a MacBook Pro and two Thunderbolt displays at work. I work with the MacBook lid-closed because it's overkill and I don't have enough desk space anyway.

Every morning I show up to work and plug in a Thunderbolt cable and power cord, but my primary screen (the one in front) and secondary (one off to the side) are swapped. I have to this stupid song and dance in the Displays / Arrangement system preference and it's getting annoying:

enter image description here
enter image description here

Is there a way to do this automatically? With a setting? An AppleScript? Is there a reason that the screens are always flipped?

Best Answer

Update: I think I got it.

There's a utility called cscreen which lets you control the display settings. Running it shows all displays:

$ cscreen
DisplayID  Index     Depth     Width     Height  Refresh
 4249fef       1        32      2560       1440     0
 4248344       2        32      2560       1440     0
use -h to display all usage options

Sure enough, you can force one of them to be primary (with menu bar):

$ cscreen -i 4248344 -p

You can script setting the second screen as the primary like this:

$ cscreen -l | perl -lane 'print $F[0] if $F[1] == 2' | xargs -I id cscreen -i id -p

Now, to bind to a key, I first created an AppleScript script, SwapScreen.scpt:

swapscreen

Then I used QuickSilver to bind the script to a F13:

quicksilver

Now, when I plug in my MacBook, if the screens are messed up, I simply hit F13. Sweet!