Working With Multiple Scripts in AppleScriptObjC

All:

I am a former Applescript Studio user and I am trying to convert a Applescript Studio application to AppleScriptObjC, using XCode 5.1.1, for the purpose of controlling Pages (for now, version 4.3). I have a good size application written in Applescript Studio that needs to be converted to AppleScriptObjC, and I have been going through Shane Stanley’s AppleScriptObjC Explored, which has been very useful. Everything is coming along. However, I have run into a basic problem that I need to resolve and I feel I am close, but not there. Any assistance would be appreciated.

I have multiple AppleScript files that do many different things depending on the user’s needs (they are called through menus), and I need to have a file full of useful sub-routines that can be loaded and incorporated into whatever file is running at the moment (in affect, merged). (The application is way to large to have it only be one file.) In the past, with Applescript Studio, the following would load another script file full of these sub-routines that are used on a regular basis, and they could then be called: (For these test purposes, the AppleScript file to be loaded is called “secondFile”)

 set theGlobalHandlers to load script file (((path to me) as string) & "Contents:Resources:Scripts:secondFile.scpt")

I understand from Shane’s book that “(path to me)” does not work as in the past and that I must use:

 set myPath to current application's NSBundle's mainBundle()'s bundlePath()

I have done that, and that appears to work (I get the path to “me”). However, the problem comes in loading the the AppleScript file “secondFile.scpt”. I thought this would work, but it does not:

 set myPath to current application's NSBundle's mainBundle()'s bundlePath() -- per Shane Stanley, and that seems fine.

 set theExternalHandlersPath to (myPath as string) & "Contents:Resources:secondFile.scpt"  -- that seems fine as well.

 set theExternalFile to load script file theExternalHandlersPath  -- here I get the error.  

It is the last line that generates an error. (For what it is worth, the error is “Can’t make current application into type file. (error -1700)”)

What am I doing wrong here?

Model: Mac Pro, Late 2013 with Mavericks
Browser: Safari 537.75.14
Operating System: Mac OS X (10.8)

Hi,

Like the shell Cocoa methods use POSIX paths, so you have to do some coercions


set myPath to (current application's NSBundle's mainBundle()'s pathForResource:"secondFile" ofType:"scpt" inDirectory:"Scripts") as text -- coerce from Cocoa string class to AppleScript string class
set theExternalHandlersPath to myPath as POSIX file as text -- coerce from POSIX via file URL to HFS path
set theExternalFile to load script file theExternalHandlersPath

StefanK:

First, thanks for the code. That is complicated and I will study it. It is more that I would have come up with using my limited experience. However, I still get an error as below: (I have added the log comments so that you can see my results.)

set myPath to (current application’s NSBundle’s mainBundle()'s pathForResource:“secondFile” ofType:“scpt” inDirectory:“Scripts”) as text – coerce from Cocoa string class to AppleScript string class

    log myPath  -- produces "2014-05-23 17:23:14.011 Two Files[4595:303] missing value"

set theExternalHandlersPath to myPath as POSIX file as text – coerce from POSIX via file URL to HFS path

    log myPath  -- produces "2014-05-23 17:23:14.011 Two Files[4595:303] missing value"

set theExternalFile to load script file theExternalHandlersPath

    -- Produces "2014-05-23 17:23:14.027 Two Files[4595:303] *** -[HSAppDelegate applicationWillFinishLaunching:]: Can't make current application into type file. (error -1700)"

What do you think?

It’s unclear where your file is. In your earlier post you wrote:

     set theGlobalHandlers to load script file (((path to me) as string) & "Contents:Resources:Scripts:secondFile.scpt")

and:

     set theExternalHandlersPath to (myPath as string) & "Contents:Resources:secondFile.scpt"  -- that seems fine as well.

The paths are different. Because of the way ASObjC loads .scpt files at startup, you should put these non-ASObjC files in a subfolder of Resources, like a Scripts folder.

     set theExternalFile to load script file theExternalHandlersPath  -- here I get the error.  

See the section on gotchas on page 199 of my book. You probably need something like:

    set theExternalFile to load script current application's file theExternalHandlersPath  

Alright! Success! Thanks guys, Here were the (two) issues.

The path regarding the:

set myPath to (current application’s NSBundle’s mainBundle()'s pathForResource:“secondFile” ofType:“scpt” inDirectory:“Scripts”) as text – coerce from Cocoa string class to AppleScript string class

was in error. I removed the inDirectory:“Scripts” changing it to as follows:

set myPath to (current application’s NSBundle’s mainBundle()'s pathForResource:“secondFile” ofType:“scpt”) as text – coerce from Cocoa string class to AppleScript string class – per Stefan

That fixed the directory. Then I replaced

set theExternalFile to load script file theExternalHandlersPath

with

set theExternalFile to load script current application’s file theExternalHandlersPath – per Shane

and everything now works.

Thanks Stefan and Shane. That was a big hurtle for me.

I suggest you add the Scripts back in, and add the equivalent folder to Resources.

The problem with putting .scpt files loose in the Resources folder is that when your app runs, any such files are assumed to be ASObjC class files, and the system tries to load them as such. Depending on what they contain, you may end up causing all sorts of problems, and of a kind that will be very difficult to track down.

OK - will do. Thanks again.