You can use a command line utility named cscreen to do that. You can wrap it in a AppleSctipt script or in Automator. You can download the cscreenIntel.dmg from the link provided. In a terminal use cscreen -h
to output the internal help information.
I have a MBP and a Thunderbolt Display so in a Terminal I want to have screencscreen
list my displays
$ cscreen -l
DisplayID Index Depth Width Height Refresh
5ac6fa7f 1 32 1440 900 0
3247b0f 2 32 2560 1440 0
use -h to display all usage options
$
So to set the Thunderbolt Display to have the menu bar I run the following command:
cscreen -s 2 -p
I can then toggle it back using the same command as the Index shifts.
In AppleScript: do shell script "/<path_to>/cscreen -s 2 -p"
In Automator add Run Shell Script and add command: /<path_to>/cscreen -s 2 -p
Note: This is a simple toggle if you only have two displays however if you have more then two you'll need to write some logic flow into the script.
You can also use, cscreen -i <DisplayID> -p
, e.g. cscreen -i 3247b0f -p
to code with the DisplayID
which doesn't change.
This can also just be used in a Terminal as a singular command or incorporated into a shell script without using AppleScript and or Automator.