Saturday, November 1, 2014

#1 2012-02-22 10:21:54 pm

V.K.
Member
Registered: 2007-12-24
Posts: 31

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:

Applescript:

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

error "ASObjC Runner got an error: unrecognized function sharedWorkspace." number -10000

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

Applescript:

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

It  fails with the message

error "NSworkspace doesn’t understand the sharedWorkspace message." number -1708 from NSworkspace

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)

Last edited by V.K. (2012-02-22 10:24:34 pm)

Offline

 

#2 2012-02-23 12:20:54 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10624
Website

Re: using sharedWorkspace method in applescript

Hi,

NSWorkspace is spelled with a capital W


regards

Stefan

Offline

 

#3 2012-02-23 12:50:59 am

V.K.
Member
Registered: 2007-12-24
Posts: 31

Re: using sharedWorkspace method in applescript

StefanK wrote:

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.

Applescript:


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

error "ASObjC Runner got an error: unrecognized function sharedWorkspace." number -10000

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

Applescript:


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.

Last edited by V.K. (2012-02-23 01:07:01 am)

Offline

 

#4 2012-02-23 02:56:09 am

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3731

Re: using sharedWorkspace method in applescript

why would it do that?

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#5 2012-02-23 11:21:48 am

V.K.
Member
Registered: 2007-12-24
Posts: 31

Re: using sharedWorkspace method in applescript

Shane Stanley wrote:

why would it do that?

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.

Offline

 

#6 2012-02-23 04:33:23 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3731

Re: using sharedWorkspace method in applescript

I must say i really don't like this behavior.

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#7 2012-02-23 04:55:55 pm

DJ Bazzie Wazzie
Member
From: the Netherlands
Registered: 2004-10-20
Posts: 1970

Re: using sharedWorkspace method in applescript

V.K. wrote:

....except in my testing the naming of a variable in one document propagates to all new documents I make unless I restart AS editor...

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:

open -n /Applications/Utilities/AppleScript\ Editor.app/

Last edited by DJ Bazzie Wazzie (2012-02-23 05:03:36 pm)


Kind regards

Offline

 

#8 2012-02-23 05:51:57 pm

V.K.
Member
Registered: 2007-12-24
Posts: 31

Re: using sharedWorkspace method in applescript

Shane Stanley wrote:

I must say i really don't like this behavior.

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.

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.

DJ Bazzie Wazzie wrote:

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:

open -n /Applications/Utilities/AppleScript\ Editor.app/

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

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.028 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)