AppleScript Objective-C: Dealing with Paths

Here are a couple of notes that might be helpful when dealing with paths in Cocoa-AppleScript applications in Xcode:

Getting the POSIX Path to Resources With Global Scope

If you need the POSIX path to the Resources folder in the App (for instance, if you intend to reference some resource files in a shell script) you need to do 2 things:

  1. Declare a property at the outset of the program to hold the variable, like so:
    property pathToResources : "NSString"
  2. At startup (eg. in applicationWillFinishLaunching_) assign the appropriate address to the property:
    set pathToResources to (current application's class "NSBundle"'s mainBundle()'s resourcePath()) as string

Getting Alias, aka Colon Path, From a POSIX Path

Now that you’ve got the POSIX path what do you do if you need to get the Alias instead (say, for example, to open a file with an application)?  ApplescriptObjC has some inconsistencies compared to plain-jane Applescript when it comes to switching from POSIX to Aliases (going the other way works as expected, though).  But the samplecode below works well.  In it we’re appending the name of a resource to the pathToResource and getting Finder to open it with the default application:

        set pathToFile to pathToResources & "/"
        set pathToFile to (((pathToFile as text) as POSIX file) as alias)
        tell application "Finder"
            open pathToFile
        end tell
        log "File " & pathToFile & " Opened!"

Hope that helps somebody. That bit of info was gleaned by combing through the terrific info over at MacScripter