Vash
Vash

Reputation: 190

How do I setup Displays to change via keyboard shortcut or "Hotkey'?

I work with 2 monitors on macOS. Under System Preferences >> Displays >> Appearances, I can change the display setup so that monitors are either side by side, one on top of the other, etc.

I often need to change the setup from side by side to one below the other, and it's painful to have to go into System Preferences and change it manually every time.

How do I go about setting it up so it does it automatically when I press some "hotkey" or keyboard shortcut?

I have access to Alfred for Mac so can code in custom workflows which should help achieve this.

Any ideas would be much appreciated!

Upvotes: 1

Views: 456

Answers (1)

Ted Wrigley
Ted Wrigley

Reputation: 3194

Here's a script that calls a python script to switch between display sets. It's only set up to change the relative position of the second display, but it can easily be expanded to work with three or more displays.

Note that you don't have to be precise about the new position. Quartz will automatically adjust things to give the nearest configuration in which the display spaces touch seamlessly.

set displaySet to first item of ¬
    (choose from list {"On left", "On right", "On top", "On bottom"} ¬
        with prompt "Choose position for second display.")

(* assuming 1280 x 800 pixel displays *)
if displaySet is "On top" then
    changeSecondDisplayOrigin(0, -800)
else if displaySet is "On bottom" then
    changeSecondDisplayOrigin(0, 800)
else if displaySet is "On left" then
    changeSecondDisplayOrigin(-1280, 0)
else
    changeSecondDisplayOrigin(1280, 0)
end if


on changeSecondDisplayOrigin(x, y)
    do shell script "
/usr/bin/python <<END

import objc
import Quartz

from Quartz.CoreGraphics import CGGetActiveDisplayList
from Quartz.CoreGraphics import CGBeginDisplayConfiguration
from Quartz.CoreGraphics import CGConfigureDisplayOrigin
from Quartz.CoreGraphics import CGCompleteDisplayConfiguration

(activeErr, activeDisplays, displayCount) = Quartz.CGGetActiveDisplayList(2, None, None);
secondDisplay = activeDisplays[1];

(configErr, displayConfigRef) = CGBeginDisplayConfiguration(None);
moveErr = CGConfigureDisplayOrigin(displayConfigRef, secondDisplay, " & x & ", " & y & ");
completeErr = CGCompleteDisplayConfiguration(displayConfigRef, 2);

END"
end changeSecondDisplayOrigin

Upvotes: 1

Related Questions