Question

I'm trying to write a small Objective-C command line tool that adjusts the rotation of a given display.

Sadly, I can not find any reference in Apple's documentation that tells how to change the rotation (aside from getting it). I think it has to be done with using CGDisplayModeRef but I can't find out how. Any help would be appreciated!

Était-ce utile?

La solution

Extracted from https://github.com/CdLbB/fb-rotate - it seems IOKit can do the job although the behavior seems a bit odd to me. In a nutshell:

io_service_t service = CGDisplayIOServicePort(myDisplayID); //IOKit Handle (get myDisplayID yourself)
IOOptionBits options = (0x00000400 | (kIOScaleRotate90)  << 16) //Some undocumented hex'ing
IOServiceRequestProbe(service, options); //Set rotation to display

Don't forget to link against IOKit.

Autres conseils

Hacked together a good enough solution using applescript that works on OS El Capitan (10.11.6) so YMMV. It's easy to kick it off with Quicksilver directly or via a Quicksilver trigger and takes about the amount of time it takes to physically rotate and adjust a monitor. Hope this helps someone!

This applescript switches the target display "DELL U2414H" to portrait mode with 270 degrees rotation. Note that the string "DELL U2414H" is from the window title representing said display under System Preferences > Displays. Not sure what would happen if it's run when there are two displays of the same make and model – feel free to report findings. Easy potential improvements would be to make it check state and toggle between the two most frequent orientations, but scarcity in time and documentation meant it was good enough for my use case as is. ¯\_(ツ)_/¯

tell application "System Preferences"
    activate
end tell

tell application "System Events"
    delay 1
    click UI element "Displays" of scroll area 1 of window "System Preferences" of application process "System Preferences"

    delay 1
    click window "DELL U2414H" of application process "System Preferences"

    delay 1
    click pop up button 2 of tab group 1 of window "DELL U2414H" of application process "System Preferences"

    -- select 270° for portrait, for landscape replace 4 with "Standard" (with quotes)
    delay 1
    click menu item 4 of menu 1 of pop up button 2 of tab group 1 of window "DELL U2414H" of application process "System Preferences"

    -- click the "Confirm" button, not necessary for "Standard" orientation
    delay 5
    click UI element "Confirm" of sheet 1 of window "DELL U2414H" of application process "System Preferences"
end tell

tell application "System Preferences"
    quit
end tell
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top