AppleScript app Assistive access trouble

I have saved an apple script as an app and when I launch it I get the following error

System Events got an error: DualFinder is not allowed assistive access. (-1719)

I have given the app accessability permission in system settings but still recieve this error any ideas

It runs fine from the script editor but the final project needs to be a stay open app


tell application "System Events"
	tell application "Mission Control" to launch
	tell group 2 of group 1 of group 1 of process "Dock"
		click (every button whose value of attribute "AXDescription" is "add desktop")
		tell list 1
			set countSpaces to count of buttons
			delay 0.5
			click button (countSpaces)
		end tell
	end tell
end tell

DualFinder is the name of your script application as I understand. So,

  1. First, clean your script:

tell application "Mission Control" to launch

tell application "System Events" to tell group 2 of group 1 of group 1 of process "Dock"
	click (every button whose value of attribute "AXDescription" is "add desktop")
	delay 0.5
	click last button of list 1
end tell
  1. Save it as usual application (not stay-open)
  2. Launch it, give to it permission to control your computer. Go System Preferencesā€“>Security&Privacy----> Accessibility. See: if the checkbox is checked (and application throws error) , then uncheck it and check again. If it is unchecked then simply check it.
  3. If this doesnā€™t help, then remove DualFinder.app from the list using MINUS button, then go step 3) again .

The probable reason for this is the ā€˜countSpacesā€™ variable, which, being a run handler variable (ie. used in the top level of the script), is saved back to the script file every time the scriptā€™s run. This changes the fileā€™s modification date and the security system thinks itā€™s a new file.

KniazidisRā€™s suggestion gets rid of the variable.

1 Like

I found the best way to avoid this error is to use + sign and add application that way first.
Then try run it. (I.e donā€™t try run it first. I.e. donā€™t drag and drop in there first.)

As a last resort fix: open system preferences. Remove the app with minus. Lock. Quit system preferences. Reopen system preferences. Add app with plus sign. Lock. Quit system preferences. Then try run app. Take your time with each of these stepsā€¦donā€™t rush it. Still not working. You can mix in a restart and some log out log insā€¦it will eventually workā€¦sounds like madness I knowā€¦but it works.

Monterey 12.4.

Thereā€™s some info on the bugginess of this whole thing here: https://dougscripts.com/itunes/2021/12/system-events-error-and-the-fix/

But Iā€™ve found my method at the top the most reliableā€¦and now just do it that way to begin with.

I have the same issue and I followed all the steps above however I still get the same error. Iā€™m currently working on a Mac Studio with Sonoma 14.4.1. Are there any other suggestions?

I have three miscellaneous suggestions, two of which you may already have tried.

First, give the AppleScript app both accessibility and full disk access permissions. Depending on what your app does, the full disk access permission may not be required.

Second, if your app uses System Events, give System Events both accessibility and full disk access permissions. Once again, the full disk access permission may not be required.

Third, as a last resort, create a new app with a different name and copy and paste the code from the old app. Edit the code in some insignificant way and then set permissions for the new app before running it. This has worked for me on a few occasions when all else failed.

Thank you for the suggestions. Unfortunately they did not resolve the issue.

rsstorey. Iā€™m sorry to hear that. The suggestions contained in this thread covers about every solution Iā€™ve come across.

I canā€™t imagine what it would be, but the only other thought that comes to mind is that something in your script is causing the issue. If you donā€™t mind, you could post the script and we could test it for privacy issues.

I posted it at the beginning of this thread but here it is again.

I did read one item that suggested some line of code within the script but it wasnā€™t elaborated on.
Also, There is a line in the script that that call "tell application ā€œSystem Eventsā€.

tell application ā€œMailā€ to activate
tell application ā€œSystem Eventsā€
tell application process ā€œMailā€
ā€“ Erase Junk Mail
tell menu item ā€œErase Junk Mailā€ of menu ā€œMailboxā€ of menu bar item ā€œMailboxā€ of menu bar 1 to click
tell button ā€œEraseā€ of sheet 1 of window 1 to click
ā€“ Erase Deleted Items
tell menu item ā€œIn All Accountsā€¦ā€ of menu ā€œErase Deleted Itemsā€ of menu item ā€œErase Deleted Itemsā€ of menu ā€œMailboxā€ of menu bar item ā€œMailboxā€ of menu bar 1 to click
tell button ā€œEraseā€ of sheet 1 of window 1 to click
end tell
end tell

Rsstorey. I looked earlier in this thread and couldnā€™t find anywhere you had posted your script.

After making necessary changes to allow your script to compile, I saved it as an app in Script Debugger and then set Accessibility and Full Disk Access permissions for the app. I confirmed that System Events had Accessibility and Full Disk Access permissions. I then ran the app in the Finder by way of the context menuā€™s Open command and the app ran without issue. I canā€™t be sure if the app did what it was intended to do (no junk mail), but there were no permission errors. I donā€™t know why you are receiving a different result. Iā€™m on macOS 14.4.1.

tell application "Mail" to activate
tell application "System Events"
	tell application process "Mail"
		-- Erase Junk Mail
		tell menu item "Erase Junk Mail" of menu "Mailbox" of menu bar item "Mailbox" of menu bar 1 to click
		tell button "Erase" of sheet 1 of window 1 to click
		-- Erase Deleted Items
		tell menu item "In All Accountsā€¦" of menu "Erase Deleted Items" of menu item "Erase Deleted Items" of menu "Mailbox" of menu bar item "Mailbox" of menu bar 1 to click
		tell button "Erase" of sheet 1 of window 1 to click
	end tell
end tell

Thank you again for all the help. I try to replicate you procedures. Need to get Script Debugger. I was using the native apple script editor. I will use the script you included in your reply. Will let you know how it turns out.

My applet doesnā€™t have any variables, but getting the assistive permission to stick after resaving is maddening.

The only way I can get it to work reliably:

  1. Quit the applet.
  2. Delete the applet from the permissions list. (If it doesnā€™t work, System Settings is effed. Close it and try again.)
  3. Save the applet.
  4. Run the applet.
  5. Click OK to the error about assistive permissions.
  6. Enable the applet in the permissions list. (If it wonā€™t stay enabled, System Settings is effed again. Close it, force quit the applet, and start over from step 1.)
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

-- Check to see if the Close Tab command has disappeared from 
-- the File menu in Edge.

on idle
	
	tell application "System Events" to tell application process "Microsoft Edge"
		
		-- Close Tab command is inexplicably named "Close All." 
		if not (exists menu item "Close All" of menu "File" of menu bar item "File" of menu bar 1) then
			
			tell application "Microsoft Edge"
				
				display alert "The Close Tab menu item has gone missing again. Restart Edge to restore it."
				
			end tell
			
		end if
		
	end tell
	
	return 60 -- Run once a minute.
	
end idle

On my system (Sonoma 14.6), System Events isnā€™t in the assistive permissions list to begin with, and since itā€™s not an app I wouldnā€™t know where to find it.

System Events is a background application, located at /System/Library/CoreServices/System Events.app/. The steps you take to get your applet working sure look like the script is getting modified. As mentioned, historically AppleScript saved all top level properties, globals, and run handler variables in the script file to make them persistent, although code signing the application should stop all that.

1 Like

Thanks ā€” Iā€™m noting its location in a comment in the code, just in case my current routine ever stops working. Wouldnā€™t be the first time.

Iā€™ve only been half paying attention to code signing, so my vague impression has been that it mostly causes headaches. This is the first Iā€™ve heard that it can prevent one. :slightly_smiling_face:

I assume Iā€™d still have to go through this dance routine when Iā€™m making changes, but it would prevent variables and properties from ā€œmodifyingā€ the applet every time I run it? Something to be said for thatā€¦

(My original reply was flagged ā€” automated, I guess? ā€” for a couple of words I used to describe the ā€œdance routineā€ and my applet. I hope this hasnā€™t put me on somebodyā€™s watch list. :frowning_face: )

Code signing, amongst other things, keeps AppleScript from modifying script files. Script Editor and Script Debugger both support automatic code-signing applications, which is actually a requirement for Apple Silicon - you can look in the bundle to see if it being signed. You donā€™t need a paid developer account, a self-signed certificate can be created for local use. It is mainly when setting up an app for distribution that it can be a pain.