Change Display Orientation with KM?

I have a 5K iMac running Catalina.

On either side of the iMac, there is a dell 4K display 27".

The left Dell 4K is in Portrait mode. The others are in Standard landscape mode.

Sometimes when waking up from sleep, the left 4K will be turned into standard landscape mode and the right, in portrait mode.

Needless to say, this is a complete pain to undo since you have to move your mouse in that screen orientation to change the display settings via the system preferences...

I am aware and use SwitchRez but Catalina even with SIP turned off, sometimes does not work and was hoping for a KM solution.

thx

I don't know of a way to do this with Keyboard Maestro, but there is a free command-line tool:

display_manager: An open-source Python library which can modify your Mac's display settings manually or automatically.

It has a rotate feature which is supposed to work with external monitors as well.

You could assign a keyboard shortcut in Keyboard Maestro to the appropriate display_manager.py command. I believe the proper command would be:

/usr/local/bin/display_manager.py rotate 90 0

Unfortunately I do not have an external monitor to test it. The final 0 (zero) refers to the number of the monitor (starting at zero). I would assume zero would be the left-most monitor, but if not, try 1 or 2 such as:

/usr/local/bin/display_manager.py rotate 90 2

1 Like

Thank you!

I will try it today and report back soon.

It's kinda of wonky. numbers for displays off. but I can work around but changing the 3rd display downgraded the Rez on the first display. I will fiddle with it some more though.

Maybe there is some call in it to the OS that I could duplicate with a swift tool.

I'm guessing that there is, but that's way outside my sphere of knowledge unfortunately.

Found this. will take me a while to try out though, seems like it would work if I call this and save this as a command line swift app:

Just found this:

https://www.jibapps.com/apps/displays/

https://www.jibapps.com/support/

Why rotation is not supported?
Apple does not provide any perfect way to rotate screen in macOS. Therefore, some screens may support rotation, others may not. Most MacBook don’t support screen rotation, thus this function will not work in Displays.

I am hoping that apple's Pro Display screen (and the fact they show off screen orientation changes often) will push apple to make this a standard OS call.

On macOS 10.15.3 I can rotate my external HP display with this:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "System Preferences"
  reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell

tell application "System Events"
  tell process "System Preferences"
    tell window "HP LP2475w"
      tell tab group 1
        tell pop up button "Rotation:"
          click
          delay 1
          click menu item "90°" of menu 1
        end tell
      end tell
      repeat until exists sheet 1
        delay 0.5
      end repeat
      -- click button "Revert" of sheet 1
      click button "Confirm" of sheet 1
    end tell
  end tell
end tell

Replace the HP LP2475w your actual window title and the 90° with your desired rotation (90°, 180°, 270°, Standard)

Thank you. there is something to this. I am making progress as at least it is working to an initial rotate. I need to add some logic to get it to rotate back. Or not. may save and use this only when I know screen x needs to rotate y....

How do you get the current display rotation?

I can get it for a given display ID with…

import CoreGraphics

print (CGDisplayRotation(CGDirectDisplayID(1007271426))) // --> 0.0 or 90.0 etc.

…but this only works in an Xcode playground, not as script or compiled.

Any idea why? Or another way? (besides UI scripting)

interesting. I will get back to this and try. if it works one option will be a swift command line app maybe

Edit: making progress. will report back

1 Like

I ended up going with AppleScript and KM.

I added the macro to my menubar KM item and it worked great this morning.

Here is the code in case anyone needs:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "System Preferences"
reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell

tell application "System Events"
tell process "System Preferences"

  tell window "Right"
  	tell tab group 1
  		tell pop up button "Rotation:"
  			click
  			delay 1
  			click menu item "Standard" of menu 1
  		end tell
  	end tell
  	repeat until exists sheet 1
  		delay 0.5
  	end repeat
  	-- click button "Revert" of sheet 1
  	click button "Confirm" of sheet 1
  	
  end tell

end tell
end tell

1 Like