Sonoma won't allow my script to use GUI scripting

I am trying to get an AppleScript (shown below) for toggling the System Settings “Use F1, F2, etc. keys as standard function keys” to work on macOS Sonoma 14.7.1. It works when run in Script Editor, but when I save it as a run-only, locally signed app, I get error:

Toggle Fn keys is not allowed assistive access.
System Events got an error: Toggle Fn keys is not allowed assistive access.
(-25211)

I have granted it access in Privacy & Security > Accessibility, but still get the error. I’ve tried every suggestion I can find about various ways of adding it to Accessibility, removing, locking and unlocking, rebooting. deleting app and re-exporting, all to no avail.

What’s the trick to get Sonoma to recognize that an AppleScript applet is allowed to control the computer?

The script is:

set System_Settings_was_not_running to application "System Settings" is not running

tell application "System Events"
	tell application "System Settings"
		activate
		reveal anchor "FunctionKeys" of pane id "com.apple.Keyboard-Settings.extension"
	end tell
	tell application process "System Settings"
		repeat until sheet 1 of window "Keyboard" exists
		end repeat
		-- May require this to give pane focus?
		click UI element 1 of UI element 1 of row 12 of outline 1 of scroll area 1 of group 1 of splitter group 1 of group 1 of sheet 1 of window "Keyboard"
		-- Click "Use F1, F2, etc. keys as standard function keys"
		click checkbox "Use F1, F2, etc. keys as standard function keys" of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window "Keyboard"
		-- Click Done button
		click UI element 2 of group 2 of splitter group 1 of group 1 of sheet 1 of window "Keyboard"
	end tell
end tell

if System_Settings_was_not_running then
	delay 1
	tell application "System Settings" to quit
end if

This may not solve your problem, but you could try it:
That first tell application "System Events" could be after the initial end tell block.
The first time you target System Settings, you are sending it commands in its own dictionary (activate and reveal anchor). You don’t need that to be wrapped with System Events. That might not be causing the problem you see, but it could be.
Then,

Krioni is correct. Move the nested tell application outside like so

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions


set System_Settings_was_not_running to application "System Settings" is not running

tell application "System Settings"
	activate
	reveal anchor "FunctionKeys" of pane id "com.apple.Keyboard-Settings.extension"
end tell
tell application "System Events"
	tell application process "System Settings"
		repeat until sheet 1 of window "Keyboard" exists
			delay 0.2
		end repeat
		tell group 2 of splitter group 1 of group 1 of sheet 1 of window "Keyboard"
			-- Click "Use F1, F2, etc. keys as standard function keys"
			click checkbox 1 of group 1 of scroll area 1
			-- Click Done button
			click button 1
		end tell
	end tell
end tell

if System_Settings_was_not_running then
	delay 1
	tell application "System Settings" to quit
end if

On Ventura, the checkbox doesn’t have a title/name so I had to change it to a number.
Also I cleaned it up a-bit by using another tell block

Nope, same error.

It wouldn’t seem to me that removing the first tell block from out of System Events would help. The problem is in the 2nd block – it does display the keyboard settings with the function key checkbox before it throws the error – and either way, the 2nd block is System Settings within System Events.

I do like the tell group 2 etc. though.

Hi mschmitt, nice to meet you!

I’m not great at English
so I might be misunderstanding your question, but you’re asking about how to work around the “not allowed assistive access” issue, right?
Let me share the method I use.
To avoid the ‘not allowed assistive access’ error when calling System Events in AppleScript, I separate the System Event part into a standalone script and execute it via zsh.

Since my English is pretty poor
(I got some help from OpenAI for this post… haha)
I made a video to explain it:

The points are:
Set the appropriate security permissions.
Separate the system event section into a standalone text script.
The standalone script should be saved in UTF-8 encoding with executable permissions (chmod +x).
Add a Shebang line at the top, such as:
#!/usr/bin/env osascript or #!/usr/bin/osascript.
Use zsh to call this system event script from the application script.
With this setup, I’ve managed to work around the issue in my environment. However, please keep in mind:

The script shown in the video is written in Japanese.
I’m using macOS 15.2.

For UI click operations using this method, the following may require Accessibility permissions:
/usr/bin/osascript
/System/Library/CoreServices/System Events.app
The script application itself
Additionally, permissions might also be required for:
/System/Library/CoreServices/SystemUIServer.app
/bin/zsh

Hope this helps!