MacOS – How to create an OSX Application to wrap a call to a shell script

applescriptautomatorbashcommand linemacos

My goal is to include in a zip file what amounts to a shortcut, instead of telling my customer(s) to open up Terminal and run a shell script.

My deployable essentially looks like this:

$ unzip Deliverable.zip 
$ cd Deliverable; ls
app.jar run.sh

Script in run.sh:

#!/bin/sh
java -jar app.jar

There's a lot more in the directory; suffice to say I need to run the script from the directory Deliverable since I need to access paths relative to it. However, I can't guarantee where a customer is going to open Deliverable.zip (could be home directory, could be right in the Downloads directory, etc.)

I found this that describes how to create a new workflow in Automator, then save it as an application to launch a shell script. I tried to do that to wrap run.sh but it says it can't find run.sh.

Somebody suggested I use applescript and also sent me a link to how to use applescript to switch into the current directory; there's an applescript "action" in automator; so I made a new workflow with that, and saved it as an application. This is what that looks like:

the automator applescript program

The code:

on run {input, parameters}
    tell application "Finder"
        set current_path to container of (path to me) as alias
    end tell

    do shell script "java -jar app.jar" 

    return input
end run

And this is the error I get when I run it:

welcome to 2015

C'mon, this should be pretty simple. What I am doing wrong here?

Best Answer

Rename your .sh file to .command and you can cd to the directory that the .command file is located in with the following at the beginning of the file:

cd "$(dirname $BASH_SOURCE)"