using sharedWorkspace method in applescript

I just recently found out that Cocoa frameworks are now accessible directly from applescript. I don’t know Objective C but I do know a bit of applescripting (haven’t touched it in a couple of years though) so I wanted to see how it works. Unfortunately my very first try seems to be failing.
Basically, anything I try which involves the method sharedWorkspace() for the class NSworkspace returns an error.
Here is the simplest example that fails:

script x
	 current application's NSworkspace's sharedWorkspace()'s absolutePathForAppBundleWithIdentifier_("com.apple.safari") as text
end script

tell application "ASObjC Runner" to run the script {x} with result returned

fails with the message

I get a similar error if I save this script as a Cocoa-applescript applet and run it as an app.
The script then is just

display dialog (current application's NSworkspace's sharedWorkspace()'s absolutePathForAppBundleWithIdentifier_("com.apple.safari") as text)

It fails with the message

I’m sure this is some silly noob error on my part and I would appreciate a pointer in the right direction.

Model: Mac Pro 3.2Ghz, early 2008
AppleScript: 2.2.1
Browser: Firefox 10.0.2
Operating System: Mac OS X (10.7)

Hi,

NSWorkspace is spelled with a capital W

Doh! I should have my eyes examined! :lol:
Thanks a lot!
I don’t know how that happened - I remember fixing this. Actually, it’s really weird: applescript editor keeps changing the capital “W” to the lowercase “w” in NSWorkspace when I press “compile” unless I save the document first. why would it do that?
but in any case, the applet works now. The first version of the script still spews the same error though.


script x
	
	current application's NSWorkspace's sharedWorkspace()'s absolutePathForAppBundleWithIdentifier_("com.apple.safari") as text
	
end script

tell application "ASObjC Runner" to run the script {x} with result returned

results in

any idea why?
EDIT: actually, pressing “compile” in the applescript editor changes “W” to “w” in NSWorkspace no matter what I do. this is probably the reason for the error but I can’t understand why this happens.

one more Edit:

forcing the issue by escaping the reference to NSWorkspace fixed the problem


script x
	
	current application's |NSWorkspace|'s sharedWorkspace()'s absolutePathForAppBundleWithIdentifier_("com.apple.safari") as text
	
end script

tell application "ASObjC Runner" to run the script {x} with result returned

But I’m still very confused why this was necessary.

The first time a variable is compiled, AppleScript adds it to an internal list of variable names, matching its case. You’re stuck with it unless you save as text, edit, and reopen, or make a new script document (recent versions of editors give each document its own instance of AppleScript). The reason has to do with the way AS works under the hood.

Short story: try to get those method names right first time, or get used to using pipes.

Thanks a lot for the explanation. Now I will know. It does work as you say. except in my testing the naming of a variable in one document propagates to all new documents I make unless I restart AS editor. so this seems to be the behavior of AS editor rather than of AS itself? My version of applescript is 2.2.1 and of AS editor is 2.4.2. I believe those are the latest versions.

I must say i really don’t like this behavior.
Also, to make matters worse, ASObjC Runner does this too which is even more insidious. Even after I restarted the AS editor and typed in the correct version of the script in a new document it still produced the same error even though there were no visible mistakes in the script anymore and compiling didn’t change anything. only after I restarted ASObjC Runner did the error go away.

Most people feel the same way, but there’s not much you can do about it. Starting a new document does work in Script Debugger.

And if you do much AppleScriptObjC, my editor, AppleScriptObjC Explorer, does code-completion with Cocoa terminology – that helps you avoid the initial problem.

The problem with using script editor is that there is one instance of AppleScript for all document that are open… this is indeed a drawback. I have those problems with do shell scripts as well.

EDIT: FWIW, to solve the problem with script editor for (blocking) do shell script commands is opening a new instance of script editor by saying in the terminal:

Thanks, I just tried AppleScriptObjC Explorer and it really is very nice. I probably won’t buy it though since Script Editor is sufficient for my modest needs. As I mentioned in my first post I’m not a programmer and I was just brushing up on applescript and testing its new capabilities after a couple of years of non-use as I needed to write a few small scripts for my personal consumption.

thanks. this does serve as a workaround in a pinch.