My goal is to distribute an X11-based application for OS X as nicely as possible. One step toward this goal is to associate an icon with the binary that can be placed on the dock or clicked from the finder. I can get most of the way there with a few lines of AppleScript.
First record the command line that you need to execute to start the X11 application. Use a relative path from the location of the script so that it doesn't matter where the application is installed. If the binary is in the same directory as the script, use './binary' rather than plain 'binary'.
set bin to "usual command line"Read the rest of the hint for the remainder of the how-to...
If .xinitrc is missing, or if it contains the line xterm &, this will start an extra terminal window. We could try to hack the .xinitrc so that it wouldn't do that, then restore it when we are done. I'm not doing so because it is risky, but here's how you might do it if you wanted to.
# The following THREE lines should be ONE line
do shell script "if test -f ~/.xinitrc; then F=`mktemp
-t ~/.xinitrc-XXXX`; cp -f ~/.xinitrc $F;
else F=/etc/X11/xinit/xinitrc; fi; echo $F"
set saveInit to result
do shell script "sed -e's/^ *xterm/#xterm/' ~/.xinitrc"
Start X11 if it is not already running.
run application "X11"
Restore .xinitrc, if hacked as above
do shell script "F='" & saveInit & "'; rm -f $F; if test ${F#/}==$F; then mv $F ~/.xinitrc; fi"
Find the location of the script. Make sure special path characters are escaped.
set scriptfile to POSIX path of (path to me)
set bindir to do shell script "perl -e 'my $file="" & scriptfile & ""; $file =~ s,[^/]*$,,; print quotemeta($file);'"
Start the application running in the background. This allows the script to end before the application.
do shell script "cd " & bindir & "; DISPLAY=:0.0 " & bin & ">/dev/null 2>&1 &"
If your application takes a long time to start up, display a dialog so the user knows that something is happening.
display dialog "Loading... this may take awhile" buttons { "OK" } default button "OK" giving up after 15
Avoid returning to finder when script is complete.
tell application "X11" to activate
Saving the script as an application lets you click on it and drag it to the dock.
It would be nice to be able to assign a .icns file to it, but that is beyond my skill level. The AppleScript application uses the resource fork (at least for version 10.2) so presumably the usual tools apply, but I don't know what they are or how to use them. Also, the resource fork means that special packaging is needed since the usual .tar.gz method of unix doesn't support them.
Mac OS X Hints
http://hints.macworld.com/article.php?story=20040226174801525