Skip to content

Instantly share code, notes, and snippets.

@v1c77
Last active June 20, 2024 18:43
Show Gist options
  • 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
@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"

@ipstone
Copy link

ipstone commented Jun 20, 2024

any update for Sonoma ? thx

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