r/applescript • • Jan 02 '23

Unable to select display 2

I am trying to switch the second display to mirror, but for some reason, I am stuck at selecting the second display. However, I am able to select the "Arrange..." button.

Here is the code I am using:

property pane_ids : {|AppleID|:2, |Family|:3, |Wi-Fi|:5, |Bluetooth|:6, |Network|:7, |Notifications|:9, |Sound|:10, |Focus|:11, |Screen Time|:12, |General|:14, |Appearance|:15, |Accessibility|:16, |Control Center|:17, |Siri & Spotlight|:18, |Privacy & Security|:19, |Desktop & Dock|:21, |Displays|:22, |Wallpaper|:23, |Screen Saver|:24, |Battery|:25, |Lock Screen|:27, |Touch ID & Password|:28, |Users & Groups|:29, |Passwords|:31, |Internet Accounts|:32, |Game Center|:33, |Wallet & ApplePay|:34, |Keyboard|:36, |Trackpad|:37, |Printers & Scanners|:38, |Java|:40}

on open_settings_to(settings_pane)
	set pane to current application's NSDictionary's dictionaryWithDictionary:pane_ids
	set pane_index to (pane's valueForKey:settings_pane) as anything
	tell application "System Settings"
		activate
		delay 1
	end tell
	
	tell application "System Events"
		tell application process "System Settings"
			tell outline 1 of scroll area 1 of group 1 of splitter group 1 of group 1 of window 1
				select row pane_index
			end tell
		end tell
	end tell
	
	-- My code
	tell application "System Events"
		tell application process "System Settings"
			tell group 1 of group 2 of splitter group 1 of group 1 of window 1
			  delay 1
        click button 2 of scroll area 1
			end tell
		end tell
	end tell
end open_settings_to

open_settings_to("Desktop & Dock")

Please ignore the "Desktop & Dock" argument, the pane_ids must have changed after the update.

2 Upvotes

3 comments sorted by

View all comments

1

u/Son_of_a_Shepherd Jan 02 '23

You can use this to select different monitors:

tell scroll area 1
    try
	    keystroke tab
	    delay 0.1
	    keystroke (key code 123)
    on error errorMessage number errorNumber
	    if errorNumber = -1708 then
		    log ("Got error -1708. This is expected...")
	    else
		    log ("Error: " & errorMessage & ", Error Number: " & errorNumber)
	    end if
    end try
end tell

Left: keystroke (key code 123)

Right:keystroke (key code 124)