I have an AppleScript that helps process a Numbers worksheet. The script has to use “System Events” UI elements to do some of its work. The script runs okay when executed from Script Editor, but I would like to run it directly from Numbers using the Script Menu. When I run it from the Script Menu, it does nothing, because UI elements are not enabled.
The following mini script illustrates the problem:
tell application "System Events"
display dialog "UI elements enabled: " & (UI elements enabled) as text
end tell
When I run this little script from Script Editor, it says “UI elements enabled: true”; but when I run it from Script Menu, it says “UI elements enabled: false”. I would like to be able to set UI elements to true, but the UI elements enabled property is read only.
How can I enable UI elements in an AppleScript that I run from the Script Menu?
Model: MacBook Air
AppleScript: 2.8.1
Browser: Safari 601.5.17
Operating System: Mac OS X (10.10)
I figured out a solution to this problem, but I still have a mystery. I got the solution from the following item in Apple support: https://support.apple.com/en-us/HT202802
However, this item says that I should get an alert: “not allowed assistive access”. I never got that alert, but I went directly to System Preferences → Security & Privacy → Privacy → Accessibility. There, I was able to allow SystemUIServer to control my computer. After turning that on, my script runs from the Script Menu in Numbers.
The mystery is, I then un-checked SystemUIServer in the “Allow the apps below to control your computer”. Now my script tells me that UI events are NOT enabled, but my script is still working.
Here is my script. It is designed to fill-down a formula from a selected cell or cells to the bottom of the table. In this demo version, it first displays a dialog saying whether UI elements are enabled.
tell application "System Events"
display dialog "UI elements enabled: " & (UI elements enabled) as text
end tell
tell application "Numbers"
activate
tell active sheet of front document
set selectedTable to (the first table whose class of selection range is range)
my fillToBottom(selectedTable, selection range of selectedTable)
end tell
end tell
on fillToBottom(theTable, startRange)
tell application "Numbers"
activate
tell theTable
set theColumns to columns of startRange
set col1 to address of first column of startRange
set coln to address of last column of startRange
set row1 to address of last row of startRange
set rown to row count
set myAddress to my colLabel(col1) & row1 & ":" & my colLabel(coln) & row1
set selection range to range myAddress
delay 0.2
tell application "System Events" to keystroke "c" using command down
set myAddress to my colLabel(col1) & row1 & ":" & my colLabel(coln) & rown
set selection range to range myAddress
--delay 0.2
tell application "System Events" to keystroke "v" using command down
end tell
end tell
end fillToBottom
on colLabel(n)
set m to (n - 1)
set digits to {(m mod 26) + 1}
set m to m div 26
repeat while m > 0
set d to (m mod 26)
set m to m div 26
copy d to end of digits
end repeat
set myString to ""
repeat with d in digits
set myString to (item d of "ABCDEFGHIJKLMNOPQRSTUVWXYZ") & myString
end repeat
return myString
end colLabel
If I remember well, we can’t rule access to Assistive devices by a script as we did before.
Now, there is no link between the authorization given thru the prefpane and the status of the checkbox in the pane Assistive Devices.
Your script may be simplified as :
tell application "Numbers"
activate
tell active sheet of front document
set selectedTable to (the first table whose class of selection range is range)
my fillToBottom(selectedTable, selection range of selectedTable)
end tell
end tell
on fillToBottom(theTable, startRange)
tell application "Numbers"
activate
tell theTable
set theColumns to columns of startRange
set col1 to address of first column of startRange
set coln to address of last column of startRange
set row1 to address of last row of startRange
set rown to row count
set myAddress to (name of cell row1 of column col1) & ":" & name of cell row1 of column coln
set selection range to range myAddress
delay 0.2
tell application "System Events" to keystroke "c" using {command down}
set myAddress to (name of cell row1 of column col1) & ":" & name of cell rown of column coln
set selection range to range myAddress
--delay 0.2
tell application "System Events" to keystroke "v" using {command down}
end tell
end tell
end fillToBottom
Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) vendredi 29 avril 2016 09:53:52