How can I swap the primary display automatically in a dual-monitor setup?
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:



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



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