Script Debugger & UI elements enabled bug?

I can run this script (below) in Script Editor and it runs just fine. But if I run it in SD the ‘UI elements enabled’ returns false. So I add Script Debugger to the Accessibility list and make sure the checkbox next to it is checked. Running it again still give the dialog about UI elements enabled being false, but additionally, if I go look at the Accessibility list, the checkbox next to ScriptDebugger is unchecked now.

Any help is appreciated,

Sam Griffith

tell application "System Preferences"
	activate
	set perform_changes to false
	set current pane to pane "com.apple.preference.displays"
	delay 1
	tell application "System Events"
		set GUIScriptingEnabled to UI elements enabled
		if GUIScriptingEnabled then
			tell process "System Preferences"
				set fourkwindow to windows contains "ASUS MG28U"
				if fourkwindow is not false then
					tell window "ASUS MG28U"
						-- tab groups are a special kind of radio button
						click the first radio button of the first tab group
						tell the first pop up button of the first tab group
							click
							delay 1
							tell menu 1
								click menu item 1
								set perform_changes to true
							end tell
						end tell
						delay 3
						if (perform_changes is true) then
							tell the first sheet
								click first button
							end tell
						end if
					
					end tell
				else
					display dialog "No ASUS 4K monitor to set rotation on" giving up after 4
				end if
			end tell
		else
			display dialog "GUIScripting is not enabled" giving up after 2
		end if
	end tell
	quit
end tell

If I take out the UI elements enabled setter, since that is gone in 10.10 and after, then I get this bug:
System Events got an error: Script Debugger is not allowed assistive access.

This however does not uncheck the Script Debugger app in the Assistive list of apps.

So in summary, the app is in the list and checked as allowed but I get this error even with that set up correctly.

Thanks,

Sam Griffith

I’m unclear about what you’re asking, but your code has some problems.

First, don’t nest it all in a “System Preferences” tell block – end that block after the delay command. Second, this line:

set fourkwindow to windows contains “ASUS MG28U”

makes no sense. I suspect you mean:

  set fourkwindow to name of windows contains "ASUS MG28U"

Thanks for input. That line of code does work in script editor.

Like I mentioned, the code works when I run it in Script Editor and does exactly what I want, which is to open up System Preferences, then open the Displays preference, then finds the window with the name “ASUS MG28U” and changes it’s rotation, then clicks the confirm button of the dialog that pops up.

What doesn’t work is running it in Script Debugger. It complains about

“System Events got an error: Script Debugger is not allowed assistive access.”

And it does this even though I’ve added Script Debugger as a app that is allowed to control the machine in Security and Privacy.

It should work like it does with Script Editor.

I used Script Debugger to see why the line:

set fourkwindow to windows contains “ASUS MG28U”

works and what it returns based on your feedback.

NOTE: I got rid of the UI elements enabled stuff as it no longer works after 10.10

Full code


tell application "System Preferences"
	activate
	set perform_changes to false
	set current pane to pane "com.apple.preference.displays"
	delay 1
	tell application "System Events"
		tell process "System Preferences"
			set fourkwindow to windows contains "ASUS MG28U"
			if fourkwindow is not false then
				tell window "ASUS MG28U"
					-- tab groups are a special kind of radio button
					click the first radio button of the first tab group
					tell the first pop up button of the first tab group
						click
						delay 1
						tell menu 1
							click menu item 1
							set perform_changes to true
						end tell
					end tell
					delay 3
					if (perform_changes is true) then
						tell the first sheet
							click first button
						end tell
					end if
				
				end tell
			else
				display dialog "No ASUS 4K monitor to set rotation on" giving up after 4
			end if
		end tell
	end tell
	quit
end tell

##Replies Output from Running code in Script Editor (on laptop)
NOTE: I’m running this on my laptop away from my external monitor that I wrote this script for (to rotate it to portrait or landscape mode), so there is only one window returned in the output below to do the contains check on. Normally there would be two or more.


tell application "System Preferences"
	activate
	set current pane to pane "com.apple.preference.displays"
end tell
tell application "System Events"
	get every window of process "System Preferences"
		--> {window "Built-in Retina Display" of application process "System Preferences"}
	display dialog "No ASUS 4K monitor to set rotation on" giving up after 4
		--> {button returned:"OK", gave up:false}
end tell
tell application "System Preferences"
	quit
end tell

Observation

As ou can see even without the code saying ‘name of windows contains “ASUS MG28U”’ that the return list has the window with it’s name string. (In this case ‘Built-in Retina Display’) and so the contains check works there.

I also took out that 2nd

tell application "System Preferences" 

block. That made it run. So I still find it confusing that if I tell the same app again that that would cause Script Debugger to say that it isn’t allowed assistive access. But I’m guessing that has something to do with reentrant code or something.

Anyway, thanks for the help and I hope my little bit of code that shows that even if you don’t say ‘name of windows contains “…”’ that you still get a list with window names was informative also.

Yes, that’s so. But that list does not contain the string “ASUS MG28U” – it potentially contains a reference to the window itself. So the expression:

set fourkwindow to windows contains “ASUS MG28U”

will always return false, because the list does not consist of strings.