A GUI script fails – Accessibility API involved

I created an Automator service that’s intended to be run from the Services menu of Finder. It allows me to switch to another bootable partition. The AppleScript part of the Automator workflow put in “Run AppleScript” is as follows (in this case we’re switching to the bootable partition “macOS High Sierra”) and is compatible with Lion 10.7.5. Considering the stricter security in High Sierra I also have a version of this snippet that allows hands-free clicking the padlock and filling in the password field before selecting a partition in the Startup Disk pane.
Since I’m writing this post in Lion I only cite the versions of this snippet that do not require clicking on the padlock so I omitted the part that follows clicking on “Startup Disk” without nullifying the subject of this post.

tell application "System Events"
tell application process "Finder" to set frontmost to true
	tell application "System Preferences" to activate
	
	tell application process "System Preferences"
		set frontmost to true
		repeat until window "System Preferences" exists
			delay 0.1
		end repeat
		
		
		tell window "System Preferences"
			tell scroll area 1 to tell the first item of (every UI element whose title contains "Startup" and role is "AXButton") to perform action "AXPress"
		end tell
		
end tell		
end tell

The script is longer but for now that will do.

Before it I placed 2 Automator actions: “Ask to confirm” and “Quit all applications”.
The problem is that it quits applications and starts executing the script but halts before clicking on “Startup Disk” if I run it outside Automator in every macOS I use. It doesn’t the other way. Why?

In Mavericks and later I have SystemUiServer, System Events, Automator Launcher and Automator Runner in the list of apps allowed to control the computer as seen in the Accessibility pane of System Preferences. That seems not to have an effect. Every time this fails I see a message in Console that tells smth like “Accessibility API wasn’t allowed to access blah-blah-blah”.

Model: MacBook Pro
AppleScript: 2.2.1
Browser: Safari 534.57.7
Operating System: macOS 10.7

This should make the job, do not change the name of button. It contains invisible “\n” character (“Startup\nDisk”):


tell application "System Preferences" to activate

tell application "System Events" to tell application process "System Preferences"
	repeat until window "System Preferences" exists
		delay 0.1
	end repeat
	click button ("Startup" & return & "Disk") of scroll area 1 of window "System Preferences"
end tell

Tested on Mojave

That’s not what I meant. The script works flawlessly, however as soon as I run it as a service it stops half-way after opening the Sysprefs window. However, I was able to make it work by omitting “Quit applications” action.

Model: MacBook Pro
AppleScript: 2.3.2
Browser: Safari 9537.86.7
Operating System: macOS 10.9

Most likely, the problem was quitting the “System Events” along with other applications. Try to keep “quitting applications” and run this:


tell application "System Events" to activate
tell application "System Preferences" to activate

tell application "System Events" to tell application process "System Preferences"
	repeat until window "System Preferences" exists
		delay 0.1
	end repeat
	click button ("Startup" & return & "Disk") of scroll area 1 of window "System Preferences"
end tell

And how is written your “quit applications” code? You can avoid quitting the “System Events” with if name of application process is not “System Events” then quit it

“Quit all applications” is not a code, it’s an Automator action, just like “Ask confirmation”. They constitute a stack that has the following config:

“Ask confirmation”
||
“Quit all applications” (has an option to exclude some apps from quitting).
||
“Run AppleScript” – contains the script

I added “System Events” and both “Automator Launcher” and “Automator Runner” to the list of excluded apps in the second action. Still no dice.

Model: MacBook Pro
AppleScript: 2.3.2
Browser: Safari 9537.86.7
Operating System: macOS 10.9

I got the solution. No need exclude any application from quitting. The problem was other. The name of button should be exactly as here:


on run {input, parameters}
	tell application "System Preferences" to activate
	tell application "System Events" to tell application process "System Preferences"
		repeat until window "System Preferences" exists
			delay 0.1
		end repeat
		click button ("Startup" & "
" & "Disk") of scroll area 1 of window "System Preferences"
	end tell
	return input
end run

NOTE: code line click button… should be 2-line as here. Do not change, since button name has invisible character “\n”. 2-line is its AppleScript representation.

Safer to write that as:

"Startup" & linefeed & "Disk"

Yes it looks better, is safer and works fine, I checked. By the way, I googled here about the letter “\n” and on some site I read that in AppleScript this is return. Now it’s clear to me that I received the wrong information. Thanks for the correction:


on run {input, parameters}
	tell application "System Preferences" to activate
	tell application "System Events" to tell application process "System Preferences"
		repeat until window "System Preferences" exists
			delay 0.1
		end repeat
		click button ("Startup" & linefeed & "Disk") of scroll area 1 of window "System Preferences"
	end tell
	return input
end run

To get rid of localization problem, you may use:

# Grab localized strings
set theBundle to (path to library folder from system domain as text) & "PreferencePanes:StartupDisk.prefPane:" as alias
set Startup_loc to localized string "CFBundleName" from table "InfoPlist" in bundle theBundle --> "Démarrage"
set NSPrefPaneIconLabel_loc to localized string "NSPrefPaneIconLabel" from table "InfoPlist" in bundle theBundle (*"Disque de
démarrage"*)

set theBundle to path to application "System Preferences"
set SystemPreferences_loc to localized string "CFBundleName" from table "InfoPlist" in bundle theBundle --> "Préférences Système"
(*
# Scheme 1
tell application "System Preferences" to activate
tell application "System Events" to tell application process "System Preferences"
	set frontmost to true
	repeat until window SystemPreferences_loc exists
		delay 0.1
	end repeat
	click button NSPrefPaneIconLabel_loc of scroll area 1 of window SystemPreferences_loc
end tell
*)

# Scheme 2
tell application "System Preferences" to activate
tell application "System Events" to tell process "System Preferences"
	set frontmost to true
	tell menu bar 1 to tell menu bar item 4 to tell menu 1
		repeat until exists menu item Startup_loc
			delay 0.1
		end repeat
		click menu item Startup_loc
	end tell
	
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 14 décembre 2019 12:20:37