Starting Java app from Applescript

Suppose you have a Java Swing application which you can start by typing the following on the command line:

java -jar myTerrificSwingApp.jar

If you want to make that application start from the Finder in OSX, there are a lot of options which range from ugly to convoluted. If you need a quick fix, here's how to do it.

Open ScriptEditor.app on your Mac, and copy paste:

-- Get path to this script using "/" in stead of ":" as seperators

set myPath to POSIX path of (path to me as string)

-- Save text item delimiters and set it to "/"

set delims to AppleScript's text item delimiters

set AppleScript's text item delimiters to "/"

-- Get the whole string except the last token, and add a "/"

set myFolder to (text 1 thru text item -2 of myPath) & "/" ¬

** as** Unicode text

-- Restore default text item delimiters (being tidy)

set AppleScript's text item delimiters to delims

-- Start java from this directory, as nohup and background

-- so this applescript can disapear from the Dock

do shell script ¬

"cd " & myFolder & ¬

"; nohup java -jar myTerrificSwingApp.jar &"

Save the script as "Application Bundle" and don't forget to un-check the startup window option. If you have some extra time, you can open the package contents of the app, and replace the Contents/Resources/applet.icns with your own icon.

There are a lot more ways to do this, for instance you can also put your jar file inside the app bundle, or build a double-clickable jar file, or bundle a complete application. You can find more info on all these options on the Apple developer website.