Hi All,
Well I finally took the plunge and am starting to attempt to join the world of applescriptobjc. I’ve got a pretty big app I’m trying to convert, and while I’m excited about the number of potential improvements that will come with this change, it’s slow going and frustrating at this point. My latest difficulty is trying to get this to work:
on loadTheScript(theScript)
set scriptPath to current application's NSBundle's mainBundle's pathForResource_ofType_(theScript, "scpt") as text
set scriptPath to (POSIX file scriptPath)
return load script scriptPath as alias
end loadTheScript
I had been using “path to me” without success, so just getting to this point was an ordeal. The frustrating thing is that this works fine if I run it (minus the NSBundle stuff) in script editor, but when I run it in xcode I get the error “Can’t make «script» into type alias.” I’m not sure if I’m doing something wrong in terms of applescript, of if this in another one of those gotchas I seem to keep running into with applescriptobjc. Any help would be appreciated.
Do you even need the POSIX file or alias statements? I don’t know about loading a script, but for running a script, this worked for me in the past:
set myFile to current application's NSBundle's mainBundle()'s pathForResource_ofType_("flag", "scpt") as string
run script myFile
The only catch is, that for a scpt file, it needs to be moved manually from the resources folder to the Targets → Loading -->Copy Bundle Resources folder to work.
The other thing the OP needs to be aware of is that any script loaded into an ASObjC script will be run as an ASObjC script, so it will be subject to the same gotchas with things like specifiers, and will also be able to do things like call methods.
What exactly does it mean to “load” a script? While I was looking into this problem, I realized that I had no way to test whether a script had been loaded, since I don’t know what to look for. How can you test for the presence of a loaded script (besides running it, which you can do without explicitly loading it first)?
I’ve got a lot of script objects in the app I’m trying to convert to ASObjC. I was hoping I could do a straight copy and paste and use them as is since most do not interact with the gui and I thought that’s where all the changes were with the move to ASObjC. So it’s not going to be that easy?
There wouldn’t happen to be a good reference that lists all these “gotchas”, things that work fine with applescript, but not ASObjC - like “path to me” no longer working?
You load the script into a variable. if you do the follow:
set myScript to load script theScriptFile
then myScript is the script object defined by theScriptFile. To test that it was loaded, set a property in theScriptFile:
property theProperty: "testing"
and then:
get theProperty of myScript
should return 'testing". For me this process helps with organizing. I have a number of related script objects defined in various files and then load them at the launch of the application.