Applescript – opening file inside app bundle

applescriptautomator

I have used Automator to create an app bundle that activates an app ("Fuse") within the Resources directory of the bundle, which then opens the file "Exolon.tzx" (also located in Resources).

However, I can only get this to work when pointing to the full location of "Exolon.tzx" while the app is on my Desktop. Obviously, when I move the app it no longer works.

What do I need to change in this script to have it point to and open "Exolon.tzx" regardless of where I keep the app (in particular, I want it to work when I transfer the app to another Mac).

My script:

tell application "Fuse"
        open POSIX file "/Users/almeath/Desktop/Exolon.app/Contents/Resources/Exolon.tzx"
    end tell
    tell application "System Events"
        set frontmost of process "Fuse" to true
    end tell
    tell application "System Events"
        keystroke "f" using command down
    end tell

When I run the app from the Desktop it activates the Fuse app, opens Exolon.tzx and then executes the specified keystroke. If I move the app to another location (i.e not the Desktop) then an error is produced: “The action Run AppleScript encountered an error: “Fuse got an error: Connection is invalid”

Best Answer

You can use path to resource to get the pathname of the file that's in an App's Resource folder.

Example AppleScript code:

set resourceName to "Exolon.tzx"

set filePathName to quoted form of POSIX path of (path to resource resourceName) as text

tell application "Fuse"
        open POSIX file filePathName
end tell

Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.