Skip to content

Instantly share code, notes, and snippets.

@v1c77
Last active April 14, 2024 04:25
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save v1c77/96affa87de94045d29cdc5f9cb8c1847 to your computer and use it in GitHub Desktop.
Save v1c77/96affa87de94045d29cdc5f9cb8c1847 to your computer and use it in GitHub Desktop.
rotate display using applescript, tested on Mac os High Sierra(10.13)
--
-- Created by: v1c77
-- Created on: 2018/7/21
--
-- DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
-- Version 2, December 2004
-- Copyright (C) 2020 v1c77
-- Everyone is permitted to copy and distribute verbatim or modified
-- copies of this license document, and changing it is allowed as long
-- as the name is changed.
-- DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
-- TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
-- 0. You just DO WHAT THE FUCK YOU WANT TO.
use AppleScript version "2.7" -- Yosemite (10.10) or later
use scripting additions
tell application "System Preferences"
quit
delay 0.5
launch
activate
set current pane to pane "com.apple.preference.displays"
--reveal pane id "com.apple.preference.displays"
end tell
tell application "System Events"
tell application process "System Preferences"
tell window "DELL U2415" -- change the window name for different display
delay 1
click radio button "Display" of tab group 1
set theGroup to tab group 1
tell pop up button "Rotation:" of theGroup
set theStat to value of it
click
tell menu 1
if theStat = "Standard" then
click menu item 4 -- 270 dig
else
click menu item 1 -- standard
end if
end tell
end tell
end tell
end tell
end tell
tell application "System Preferences"
quit
end tell
@hexbus
Copy link

hexbus commented Mar 5, 2020

Can you please put an open source license on this? Like GPL2? I’d like to use this at work, but they won’t let me until it has a license. Thanks!!!

@sleeptil3
Copy link

This is FANTASTIC! Thanks so much for making this.

@v1c77
Copy link
Author

v1c77 commented Apr 14, 2021

Can you please put an open source license on this? Like GPL2? I’d like to use this at work, but they won’t let me until it has a license. Thanks!!!

done.

@oluckyman
Copy link

Updated version for new UI in macOS Monterey 12

use scripting additions

tell application "System Preferences"
	quit
	delay 0.5
	launch
	activate
	set current pane to pane "com.apple.preference.displays"
	--reveal pane id "com.apple.preference.displays"
end tell

delay 2

tell application "System Events"
	tell application process "System Preferences"
		tell window "Displays"
			click button "Display Settings…"
			delay 0.5
			tell sheet 1
				select row 2 of outline 1 of scroll area 1 -- the row with external monitor
				tell pop up button "Rotation:"
					set theRotation to value of it
					click
					tell menu "Rotation:"
						if theRotation = "Standard" then
							click menu item "90°" -- your preferred rotation 
						else
							click menu item "Standard"
						end if
					end tell
				end tell
				delay 1
				if exists of button "Confirm" of sheet 1 then
					click button "Confirm" of sheet 1
				end if
				click button "Done"
			end tell 
			-- Arrange displays here somehow 🤷‍♂️
		end tell 
	end tell
end tell

delay 0.5

tell application "System Preferences"
	quit
end tell

I didn't figure out how to rearrange displays with Apple Script. So I commented the last 3 lines for me. I do arrangement manually and then quit the System Preferences 😔

@sleeptil3
Copy link

@oluckyman that's fantastic! thanks for doing this. It works really well dropping it into the new Shortcuts app as an AppleScript action. Then you can put it in a nice place like the Menu Bar for access

Screen Shot 2022-01-19 at 9 19 17 AM
!

@oluckyman
Copy link

@sleeptil3 thanks for the followup! I was not aware that you can pin a shortcut to the Menu Bar. Nice!

@oluckyman
Copy link

My final version of the script includes arrangement logic with help of displayplacer.
In the script above replace

-- Arrange displays here somehow 🤷‍♂️

with

-- Arrange monitors
delay 0.1
if theRotation = "Standard" then
  -- vertical arrangement
  do shell script "/opt/homebrew/bin/displayplacer \"id:8B54EED0-06DA-6D6D-0857-D6964E3302DB res:1080x1920 scaling:on origin:(180,-1920) degree:90\""
else
  -- horizontal arrangement
  do shell script "/opt/homebrew/bin/displayplacer \"id:8B54EED0-06DA-6D6D-0857-D6964E3302DB res:1920x1080 scaling:on origin:(-240,-1080) degree:0\""
end if

See displayplacer Instructions how to get the script string for your desired arrangement in vertical and horizontal setup.

@Axeln78
Copy link

Axeln78 commented Mar 24, 2022

Thanks a lot @oluckyman! The addition of the display placer makes it stellar ⭐

@codeanpeace
Copy link

Thanks @oluckyman ! I re-posted a snippet of your gist on this StackOverflow question and credited you.

@doowylloh88
Copy link

@sleeptil3 How did you get the Apple Script to work as a shortcut. I get a "Shortcuts is not allowed assistive access" error. Note: I gave it access under the privacy settings pane. I also tried creating an AppleScript app. It pops up the same error. The script itself runs fine. Thanks

@therohitdas
Copy link

Thank you very much for the script @v1c77, @oluckyman and @codeanpeace.

Does anyone know how to make it work on macOS Ventura Version 13.0.1?
Arrangement logic still works, but the script runs into this error after the system preference opens:

error "System Settings got an error: AppleEvent handler failed." number -10000

@marcelliino
Copy link

Update for MacOS Ventura new System Settings

tell application "System Events"
	
	key code 107 using {option down}
	
	delay 1
	tell window "Displays" of application process "System Settings"
		tell pop up button 0 of group 4 of scroll area 2 of group 0 of group 2 of splitter group 0 of group 0
			
			set currentRotation to value of it
			
			click
			tell menu 1
				if currentRotation = "Standard" then
					click menu item 4 -- 270°
				else
					click menu item 1 -- standard
				end if
			end tell
			
		end tell
		
		delay 1
		if exists of sheet 1 then
			click button 1 of group 0 of sheet 1
		end if
		
	end tell
	
end tell

delay 1

tell application "System Settings"
	quit
end tell

@therohitdas
Copy link

Thank you very much @marcelliino
This script works if the external monitor is chosen as the main display.

@oluckyman
Copy link

Installed macOS Ventura recently. Glad to see that there is updated script already. Thank you @marcelliino!

I wanted to automate external monitor selection, but was not able to click on buttons representing displays. Maybe it's a bug, or my AppleScript skills.
For now added a 5sec delay, to have time to select the proper monitor.
Here is the mix of mine and @marcelliino scripts:

tell application "System Events"
	
	key code 107 using {option down}
	
	delay 1
	tell window "Displays" of application process "System Settings"
		(* 
			The script below works only when the external monitor is selected.
			I was not able to select it programmatically:
				Displays are represented as buttons in `scroll area 1`,
				but nothing happens when they receive `click` command
			So, ask user for help with this and click on the proper monitor
		*)
		
		-- Give user 5 sec to click on the monitor to rotate
		set autoClose to 5
		set continueButton to "Continue (in " & autoClose & " sec)"
		set stopButton to "Stop"
		display dialog "Select monitor to rotate" giving up after autoClose buttons {stopButton, continueButton} default button continueButton cancel button stopButton
		
		-- This only works when the external monitor is selected (by default or by user)
		tell pop up button 0 of group 4 of scroll area 2 of group 0 of group 2 of splitter group 0 of group 0
			set theRotation to value of it
			click
			tell menu 1
				if theRotation = "Standard" then
					click menu item "90°"
				else
					click menu item "Standard"
				end if
			end tell
		end tell
		
		-- Confirm new display setting
		delay 1
		if exists of sheet 1 then
			click button 1 of group 0 of sheet 1
		end if
		
		-- Arrange monitors
		delay 0.1
		if theRotation = "Standard" then
			-- vertical arrangement
			do shell script "/opt/homebrew/bin/displayplacer \"id:8B54EED0-06DA-6D6D-0857-D6964E3302DB res:1692x3008 scaling:on origin:(0,-3008) degree:90\""
		else
			-- horizontal arrangement
			do shell script "/opt/homebrew/bin/displayplacer \"id:8B54EED0-06DA-6D6D-0857-D6964E3302DB res:2560x1440 scaling:on origin:(-427,-1440) degree:0\""
		end if
	end tell
end tell

delay 0.5

tell application "System Settings"
	quit
end tell

@alissoneloi
Copy link

On my Mac air M2 (Ventura 13.3.1a)

error "System Events got an error: Can’t get application process "System Settings"." number -1728 from application process "System Settings"

@felipemldias
Copy link

Hi @alissoneloi, did you find and alternative to bypass the error message? I've found that if I activate it twice, it opens without any issues

@lucasmalaguti
Copy link

tried on Sonoma with no success

@arootroatch
Copy link

I just upgraded to Ventura and am also getting the error System events got an error: Can't get window "Displays" of application process "System Settings"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment