Friday, August 14, 2020

#1 2019-12-12 03:18:05 pm

scrutinizer82
Member
Registered: 2015-08-04
Posts: 41

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.

Applescript:

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

Last edited by scrutinizer82 (2019-12-13 02:09:14 pm)


Currently running Mac OS X 10.7.5, OS X 10.9.5, macOS 10.14.6

Offline

 

#2 2019-12-13 04:19:47 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1265

Re: A GUI script fails – Accessibility API involved

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

Applescript:


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

Last edited by KniazidisR (2019-12-13 06:24:28 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#3 2019-12-13 06:06:28 am

scrutinizer82
Member
Registered: 2015-08-04
Posts: 41

Re: A GUI script fails – Accessibility API involved

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


Currently running Mac OS X 10.7.5, OS X 10.9.5, macOS 10.14.6

Offline

 

#4 2019-12-13 06:31:40 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1265

Re: A GUI script fails – Accessibility API involved

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

Applescript:


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

Last edited by KniazidisR (2019-12-13 06:37:17 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#5 2019-12-13 06:47:36 pm

scrutinizer82
Member
Registered: 2015-08-04
Posts: 41

Re: A GUI script fails – Accessibility API involved

"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

Last edited by scrutinizer82 (2019-12-13 06:52:10 pm)


Currently running Mac OS X 10.7.5, OS X 10.9.5, macOS 10.14.6

Offline

 

#6 2019-12-13 11:55:08 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 1265

Re: A GUI script fails – Accessibility API involved

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

Applescript:


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.

Last edited by KniazidisR (2019-12-14 12:11:10 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#7 2019-12-14 12:25:21 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6440

Re: A GUI script fails – Accessibility API involved

Safer to write that as:

Applescript:

"Startup" & linefeed & "Disk"


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#8 2019-12-14 12:48:14 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1265

Re: A GUI script fails – Accessibility API involved

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:

Applescript:


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

Last edited by KniazidisR (2019-12-14 12:48:50 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#9 2019-12-14 05:20:41 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4568

Re: A GUI script fails – Accessibility API involved

To get rid of localization problem, you may use:

Applescript:

# 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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)