NSReceiverEvaluationScriptError even though UI scripting ON

OS: Mac OS X 10.3 Panther
SystemPreferences:Universal Access: Enable Access for Assistive Devices is CHECKED

I run the following script on a selection in ANY app:
tell application “System Events”
set GUIenabled to get UI elements enabled
if GUIenabled is false then
display dialog “You have not enabled the GUI scripting.”
end if
set theProcess to every process whose frontmost is true
tell process theProcess
set selectedText to value of attribute “AXSelectedText” of text area 1 of window 1
end tell
end tell

if selectedText is empty then
display dialog “you need to select some text!.”
display dialog "Selected text is: " & selectedText
end if

Script Compiles, but I get an error message like so: “NSReceiverEvaluationScriptError: 4” when I run it.

Have searched Web for answers. Only “solution” suggested was that UI scripting was off, but that is not the case for me.

Ergo, I’m stumped. Any help would be greatly appreciated.

A universal syntax for windows is impossible since every app developer will build their windows in different ways. The following script will work in the Script Editor. Open it and run from there:


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

sigh So much for being able to capture selections from any app. Seems like a ridiculously basic feature for the language. Is there a selection in the frontmost app? Yes. Okay, assign it to variable X.

I am sorry to see that even with UI scripting there is still no global way to access basic properties and objects of various apps. It’s still app specific, and that’s a real shame and a real weakness.

Thanks for your help. I appreciate it greatly. Good to know I shouldn’t waste any more time on the project.

A common approach is to try and get the selection by copying it to the clipboard and then reading that. The following script will try to do that while also trying to preserve the contents of the clipboard:


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Thank you so much Jon, for this tip. I only wish it worked! :? When I run the script from the script menu against ANY foreground app, it always presents the contents of the old clipboard, never the new selection. The only time I get the new selection presented to me is if I run the script from within Script Editor. The script does not throw an error; it just silently fails to put the selection in the foremost process into the selectedText variable. I gave it a longer delay… up to five seconds, and it still didn’t work.

I think I’m going to have to give up and force the user to copy the selection to the pasteboard first, and then grab it off of that. I’m thinking

set selectedText to do shell script "pbpaste"

followed by a restore of the old clipboard back (although that causes problems from a UI perspective, I guess)

Thanks again for your help. AppleScript is growing up rapidly, but it still seems a bit adolescent. It sneaks out of the house and slams doors for no discernable reason.

Hmmm. There is something odd going on in System Events & the Script menu (I assume that’s how you’re running this). If you hardcode the app to target with an activate command at the beginning, it works every time (as long as the app supports Command-C for copy):


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Thanks again, Jon.

From my testing, it looks like we’re dealing with something fairly simple… when you set theProcess to item 1 of (every process whose frontmost is true), two different things happen:

  1. If you have already explicitly, via hardcoding, activated a frontmost application (like Safari in your example), then the AppleScript respectfully leaves the activated app as the frontmost application and thinks of it as the frontmost process.

  2. However, if you get the active application dynamically, through a set currentApp to name of (info for (path to frontmost application)) or simply thorugh the set theProcess to item 1… syntax you use, WITHOUT hardcoding an application up top, the AppleScript usurps the place of the frontmost application… and you can’t do command “c” GUI scripting on a running script…, so the clipboard never gets the new information.

What’s interesting is that no error message is generated when you try to run keystroke “c” with command down on a running script.

In 10.3.1 (AS 19.2) this works:

According to the AS 1.9.2 Release Notes:


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Under 10.3.1 your solution is indeed the correct incantation. Thanks for your help. We’re on our way!

Here’s the feature complete script, just to document the solution to several issues. By the way, it seems like you need to make sure, when you run this from the script menu, that you have it saved as a script, and not an application. Saving it as an application will cause the script to usurp the frontmost application.

[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

By the way, I’ve contributed this script to http://scriptbuilders.net. It is called “New Palm memo from selection”