The Infamous OS X execution error: An error of type -10810 has occurred. (-10810)

The Infamous OS X execution error: An error of type -10810 has occurred. (-10810)

This error seems to appear often when I am using osascript for automating things.  This error is a bit of a mystery as hundreds of users will say.  In addition, it is not even listed in the MacErrors.h file found at:

/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CarbonCore.framework/Versions/A/Headers/MacErrors.h

After a lot of searching, I found some possible reasons why this error occurs:

Since I mostly deal with scripting and automation, this post will be more about that and not so much about the Finder not being able to open because of this error.

There Is Not A GUI, But One Is Required

This error sometimes appears when running a script that requires a GUI, but the script is being run as root.  The solution here is to get the current user and substitute as them.

First, you need to get the current user and store that value in a variable, which can be done a number of ways:

currentUser=$(stat -f "%Su" /dev/console)
# Alternate ways to get the currently logged in user
currentUser=$(who | grep console | awk '{print $1}')
currentUser=$(logname)
currentUser=$(ls -l /dev/console | cut -d " " -f4)
currentUser=$(printf "get State:/Users/ConsoleUser\nd.show" | scutil | awk '/kCGSSessionUserNameKey/ {print $3}')

Then you can run the script under that user’s context:

su "$currentUser" -c "your commands here"

When running as root, another method is to use bsexec.  I found this method useful with terminal-notifier.

loggedInUser=$( ls -l /dev/console | awk '{print $3}' )
loggedInPID=$( ps -axj | awk "/^$loggedInUser/ && /Dock.app/ {print \$2;exit}" )
/bin/launchctl bsexec "${loggedInPID}" sudo -iu "${loggedInUser}" "some commands here"

Access To Assistive Devices Is Not Enabled

I saw this a few times when trying to script a GUI login, but was resolved with enabling access to assistive devices.

On OS X 10.8 and Earlier…

Just run this command and reboot the computer:

sudo touch /private/var/db/.AccessibilityAPIEnabled

On OS X 10.9 and Up…

Use something like tccutil.py to add a specific app to the Accessibility database from the command line.

The Process Table Is Full

You can find out how many processes are allowed to run using either of these commands:

sysctl kern.maxproc ulimit -a

I never tried this, but you could try setting the max allowed processes to a higher number.  I’m not certain what would happen if it gets set too high:

sysctl -w kern.maxproc=1000

Another good troubleshooting tip is this command, which will count all processes so you can see if you are close to the limit.

ps -xa | wc -l

LaunchServices Failure

Yet another cause of this error has to do with LaunchServices, but the best solution would be to reboot, or investigate using the process-table method above.

Help With This Error

If you have run into this error elsewhere or have been able to solve it, let me know in the comments.