Using my MacBook Pro, which normally sits to the left of my Thunderbolt Display, and my Thunderbolt Display, the following will show how to use hsscreens and AppleScript to toggle the two arrangements programmatically based on how it's currently arranged. The end result is an AppleScript application that when run simply toggles back and forth between the two arrangements. This app can be run in all the normal ways any other application can be run.
In the image below, you see the normal and flipped arrangements of the Displays and Menu bar showing the difference relationships between my MacBook Pro and Thunderbolt Display.
This AppleScript application, along with the hsscreens
binary executable file, programmatically toggles between the two arrangements shown in the image above, shifting the MacBook Pro's Display to the right or left of the Thunderbolt Display maintaining the Menu bar on the MacBook Pro's Display.
To code this, I needed to determine what the current arrangement was and then act accordingly. To do this I used hmscreens -info
in a Terminal twice, once while in each arrangement.
Looking at the outputted information I saw a piece of info that I could grep
for, in order to the set a flag to toggle against.
Output of hmscreens -info
in a normal arrangement of the Displays:
$ hmscreens -info
Screen ID: 68822100
Size: {1440, 900}
Global Position: {{0, 0}, {1440, 900}}
Color Space: NSCalibratedRGBColorSpace
BitsPerPixel: 32
Resolution(dpi): {144, 144}
Refresh Rate: 0
Uses Quartz Extreme: YES
Screen ID: 68695180
Size: {2560, 1440}
Global Position: {{1440, -540}, {4000, 900}}
Color Space: NSCalibratedRGBColorSpace
BitsPerPixel: 32
Resolution(dpi): {72, 72}
Refresh Rate: 0
Uses Quartz Extreme: YES
$
Output of hmscreens -info
in a flipped arrangement of the Displays:
$ hmscreens -info
Screen ID: 68822100
Size: {1440, 900}
Global Position: {{0, 0}, {1440, 900}}
Color Space: NSCalibratedRGBColorSpace
BitsPerPixel: 32
Resolution(dpi): {144, 144}
Refresh Rate: 0
Uses Quartz Extreme: YES
Screen ID: 68695180
Size: {2560, 1440}
Global Position: {{-2560, -540}, {0, 900}}
Color Space: NSCalibratedRGBColorSpace
BitsPerPixel: 32
Resolution(dpi): {72, 72}
Refresh Rate: 0
Uses Quartz Extreme: YES
$
If was obvious, to me, I needed to use info from Global Position:
to determine what the current arrangement of the Displays were. I immediately saw -2560
as unique between the output and a way of determining the current arrangement. If I grep
ed for -2560
and found it then I was in the flipped arrangement, not the normal arrangement. So this is what I used to determine the current arrangement and thus toggle between the two arrangements. (Note: -2560
was not the only thing unique, it's just what I focused on first and felt an easy way to parse the information to set a toggle flag with.)
Save the AppleScript code below as an application and place a copy of the hmscreens
binary executable file within the <appname>.app/Contents/Resources
folder. This way the app, e.g. /Applications/Toggle Displays Arrangements.app
is self-contained and not dependent on an external location of the hmscreens
binary executable file.
Obviously you'll need to determine what to grep
for or use a different method to set the toggle flag and modify the code below accordingly to suite your particular needs (if other then this use case or a different use case).
AppleScript code:
set hms to (path to me as text) & "Contents:Resources:hmscreens"
set screenIDs to paragraphs of (do shell script quoted form of POSIX path of hms & " -screenIDs")
if (count of screenIDs) is greater than 1 then
set normalOrentation to "1"
try
set normalOrentation to do shell script quoted form of POSIX path of hms & " -info | grep '\\-2560'"
end try
if normalOrentation is "1" then
set othersPosition to "left"
else
set othersPosition to "right"
end if
set secondScreen to item 1 of screenIDs
do shell script quoted form of POSIX path of hms & " -setMainID " & secondScreen & " -othersStartingPosition " & othersPosition
else
tell me
activate
display dialog "Only one screen is attached to your computer!" buttons {"OK"} default button 1 with icon note
end tell
end if
The image below shows syntactical highlighting of the AppleScript code.
if normalOrentation ...
statement. That just needed to cover info for the toggle flag. The replicated code has been moved out and set below it.