Apple script is not allowed to send keystrokes (Sonoma)

This all worked fine in the previous MacOSs, but with every upgrade, my script breaks.

Now with Sonoma, all the system setting have moved (or been removed or replaced) I get and error "Apple script is not allowed to send keystrokes."


Anyone know where I can grant Apple Script access to keystrokes?

iMac (2017 – 2020)

Posted on Sep 29, 2023 8:44 AM

Reply
Question marked as Best reply

Posted on Oct 2, 2023 4:03 AM

I have similar issue. I use automation for login to VDE over vmware horizon client.

I use simple app for pasting name and password to login window.


After upgrade to Sonoma I am getting same error message. I tried to remove and add app to accessibility but without success :(


any help?

10 replies
Question marked as Best reply

Oct 2, 2023 4:03 AM in response to VikingOSX

I have similar issue. I use automation for login to VDE over vmware horizon client.

I use simple app for pasting name and password to login window.


After upgrade to Sonoma I am getting same error message. I tried to remove and add app to accessibility but without success :(


any help?

Sep 29, 2023 11:09 AM in response to VikingOSX

  1. Created the script in Apple Script.
  2. Created an action in Apple Shortcuts.


Running from Shortcuts, I get File can't be imported.

But If I click on the shortcut and select "Edit" and run it from there, it opens but it isn't correct. I set the page orientation to landscape, but it wants to print in portrait.


If I run it directly from Apple Script, I get the error that it is not allowed to send keystrokes.


The errors don't seem to be consistent.


Here is the script - It worked flawlessly before Sonoma.


-- Script Editor User Guide for Mac - Apple Support


tell application "Numbers"

open ("/Users/silvasp/Downloads/Export.csv") as POSIX file

activate

end tell


tell application "Numbers"

tell document 1

delete (row 1 of table 1 of active sheet)

delete (row 1 of table 1 of active sheet)

delete (row 1 of table 1 of active sheet)

end tell

end tell


tell application "Numbers"

tell document 1

delete (column 1 of table 1 of active sheet)

delete (column 2 of table 1 of active sheet)

delete (columns 7 through 7 of table 1 of active sheet)

end tell

end tell


tell application "Numbers"

tell document 1

set format of (column 3 of table 1 of active sheet) to currency

set format of (column 4 of table 1 of active sheet) to currency

set format of (column 5 of table 1 of active sheet) to currency

end tell

end tell


tell application "Numbers"

tell document 1

set alignment of row 1 of table 1 of active sheet to center

tell application "System Events" to keystroke "b" using {command down}

set background color of row 1 of table 1 of active sheet to {35767, 35767, 35767}

set the width of (column 3 of table 1 of active sheet) to 65

set the width of (column 4 of table 1 of active sheet) to 65

set the width of (column 5 of table 1 of active sheet) to 65

end tell

tell application "System Events" to keystroke "p" using {command down}

delay 1

tell application "System Events" to keystroke "p" using {command down}

end tell

tell application "System Events"

tell application process "Numbers"

set frontmost to true

tell menu bar 1 to tell menu bar item "File" to tell menu 1 to tell menu item 23

click

end tell

tell window "Export"

tell scroll area 2 to tell button 2

-- page orientation: button 1 to portrait; button 2 to landscape

click

end tell

tell button 5

click

end tell

end tell

end tell

end tell


Sep 29, 2023 11:58 AM in response to silvasp

When you get the dialogs about not allowed to send keystrokes are these Cancel OK button pairs and do you click OK. If you do, it should set that flag in System Settings : Privacy & Security : Accessibility for Automator, Script Editor, and Shortcuts. If it doesn't, you can add [+] these applications there.


You also want to check that Privacy & Security panel under Automation and ensure that if you have given your workflow a name, that its entry has System Events enabled for it. Here is mine for Automator:



As far as your AppleScript goes, you really only need the following, and not multiple Numbers tell blocks:


tell application "Numbers"
activate
tell document 1

... all Numbers related AppleScript code

end tell
end tell



Review the Numbers AppleScript dictionary to see if you really need that GUI scripting with System Events and process Numbers. Any future update to Numbers could break your GUI scripting as Apple has this penchance for rearranging internal application layout organization.

May 14, 2024 7:37 AM in response to silvasp

I'm using macOS Sonoma. The new system controls have changed the organization and usage of the term "Accessibility." It now serves as a root directory for traditional accessibility features (e.g., hard of hearing, vision) and as a subfolder within "Privacy & Security." This dual usage was unexpected, as Accessibility was previously only a root category encompassing both groups of features.


To clarify, Accessibility now appears both as a root folder and as a subfolder within Privacy & Security. The confusion arises because other subfolders in Privacy & Security only report the current access state without allowing modifications. If I had known to look for the Accessibility subfolder, I would have found how to update AppleScript permissions.


Finally, I expect that in the next OS version, Accessibility as a subfolder will be removed, and the ability to control access will be integrated into specialized subfolders for each permission type (e.g., Automation). Not a major beef, but I wish Apple would document these design changes and their roadmap more clearly... the time wasted on silly mistakes on my part, could be more easily avoided.

Apple script is not allowed to send keystrokes (Sonoma)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.