Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Create a dockable X11 application UNIX
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...

[robg adds: I haven't tested this one...]

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.

    •    
  • Currently 2.50 / 5
  You rated: 2 / 5 (4 votes cast)
 
[11,025 views]  

Create a dockable X11 application | 13 comments | Create New Account
Click here to return to the 'Create a dockable X11 application' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Create a dockable X11 application
Authored by: wgscott on Mar 08, '04 12:16:21PM

You could save yourself a lot of work by just wrapping your shell script with Platypus.

http://sveinbjorn.vefsyn.is/software



[ Reply to This | # ]
Create a dockable X11 application
Authored by: dafdaf on Mar 08, '04 12:37:03PM

Nice hint. I just created a tiny little app to launch (fink-) gimp for me:

run application "X11"
do shell script "cd /sw/bin/; export DISPLAY=:0.0; ./gimp & > /dev/null 2>&1 &"
tell application "X11" to activate

After saving as application, assigning an icon is just a matter of copying the appropriate icon to the info-window of the app.

But one annoyance I've found: After creating the said Gimp-icon, I was unable to open graphic-files by dragging them to the dock icon. I don't know if that can be changed by setting parameters in the applescript or if that's just impossible with X11-apps.

Maybe someone with mor applescript-skills can point me to the manual/faq entry...



[ Reply to This | # ]
Create a dockable X11 application
Authored by: glusk on Mar 08, '04 02:01:41PM
Here's a kludge of many examples (some from MacOSXhints) that I use to start up GIMP. Thanks to the people submitted hints for me to copy from. This applescript can be saved as an application bundle and have a .icns associated with it. You can find a copy at: http://homepage.mac.com/glusk/FileSharing3.html If anyone has any improvements, please share.

tell application "X11" to activate

set filecount to 0

if filecount < 1 then
	set isRunning to 0
	try
		set isRunning to do shell script "/bin/ps -auxww | /usr/bin/grep gimp | /usr/bin/grep -vc grep"
	end try
	if isRunning is 0 then
		do shell script "export DISPLAY=0:0; /sw/bin/gimp > /dev/null 2>&1 &"
	end if
end if

on open filelist
	tell application "X11" to activate
	
	repeat with i in filelist
		set filecount to 1
		set filename to do shell script ¬
			"perl -e \"print quotemeta ('" & POSIX path of i & "');\""
		
		set isRunning to 0
		try
			set isRunning to do shell script "/bin/ps -auxww | /usr/bin/grep gimp | /usr/bin/grep -vc grep"
		end try
		if isRunning is not 0 then
			do shell script "export DISPLAY=0:0; /sw/bin/gimp-remote " & filename & " > /dev/null 2>&1 &"
		else
			do shell script "export DISPLAY=0:0; /sw/bin/gimp " & filename & " > /dev/null 2>&1 &"
		end if
	end repeat
	
end open



[ Reply to This | # ]
Create a dockable X11 application
Authored by: dafdaf on Mar 08, '04 06:42:34PM
*very* nice script, indeed. - Works like a charm !
By adding the following lines to the bottom of the script, it's possible to quit Gimp via the dock as well:
on quit
	do shell script "/bin/ps -x | /usr/bin/grep gimp | /usr/bin/grep -v grep | /sw/bin/awk '{print $1}' | xargs kill"
	continue quit
end quit

And as ncrocker pointed out: It's important to select 'stay open' when saving the script.

[ Reply to This | # ]
Create a dockable X11 application
Authored by: Eponymous on Mar 09, '04 09:51:30AM
A nice addition would be to have the script quit itself when you close Gimp. The bit below is just modified from the original and checks for "gimp" in running processes, then quits if that isn't found. Note that it ignores "MacGimp" which is in the name of my version of the script; you'll want to get rid of that part if you have a name that doesn't include "gimp".

I can't get it to quit by putting the "tell me to quit" within the repeat loop, which seems odd to me.

This also breaks the "on quit" bit posted earlier, presumably because the script is looping through the repeat and can't look for a "quit" message. Should be a way around that, no?


repeat
	set isRunning to 0
	try
		set isRunning to do shell script "/bin/ps -auxww | /usr/bin/grep gimp | /usr/bin/grep -v MacGimp | /usr/bin/grep -vc grep"
	end try
	if isRunning is 0 then
		exit repeat
	end if
	delay 10
end repeat

tell me to quit


[ Reply to This | # ]
Create a dockable X11 application
Authored by: ncrocker on Mar 08, '04 06:00:31PM

The script that glusk says to save as an application bundle should let you "open graphic-files by dragging them to the dock icon" if you select the "stay open" option when you save the script. I haven't tried it myself, but it seems to have the magic ingredient for a script application which lets the user open files by dropping them onto the dock icon: an "open" subroutine (a.k.a. an "open" handler in Applescript terminology), which is given by the block of code bracketed by the lines "on open filelist" and "end open"



[ Reply to This | # ]
The right way to do this on 10.3
Authored by: acdha on Mar 08, '04 02:13:25PM

do shell script "open -a X11 /path/to/X11/app"

This avoids setting DISPLAY (which will break if you have multiple users) and avoids the need to delay, decide whether to start X11, etc. by reusing the existing launcher infrastructure.



[ Reply to This | # ]
The right way to do this on 10.3
Authored by: hopthrisC on Mar 08, '04 04:32:23PM
Yup! We had that only recently:

[ Reply to This | # ]
open-x11
Authored by: rohanl on Mar 09, '04 03:33:32AM
Some good solutions above, but instead of fiddling with DISPLAY and launching X11 manually, just use open-x11
    $ which open-x11
    /usr/bin/open-x11
There's no man page for it, but you can get some usage info by giving it garbage on the command line:
    $ open-X11 -hfgjhds
    /usr/bin/open-X11: line 1: type: -h: invalid option
    type: usage: type [-afptP] name [name ...]
    : unknown application: -hfgjhds
It makes sure that X11.app is running, and then launches the given command. eg.
    $ open-x11 xclock


[ Reply to This | # ]
open-x11
Authored by: m5comp on Mar 09, '04 09:22:57AM
If the executable isn't in the /usr/X11R6/bin directory, you have to specify the full path:
$ open-x11 /usr/local/bin/pan


[ Reply to This | # ]
open-x11
Authored by: wgscott on Mar 09, '04 05:03:49PM

It also apparently starts as a subprocess, so if your X11 application needs environment variables set in order to work, this won't do it, and you are back to the above type shell script.



[ Reply to This | # ]
open-x11
Authored by: m5comp on Mar 13, '04 12:17:18PM
To my surprise, none of the above methods will work with gvim (the X11 version, not the Carbon port). The only method I found that will work is to create a .command file containing the following shell script:

#! /bin/sh
open -a X11
sleep 5
/usr/local/bin/gvim
Then, of course, you'd make the .command file executable and paste whatever custom icon you'd like. I did several .command files for pan, sol, gimp, etc., and in those the line "open-x11 /full/path/to/app" sufficed.

[ Reply to This | # ]
Don't mess with user's config files
Authored by: frigaut on Mar 10, '04 04:57:26AM

This hint is certainly useful, although there are simpler ways to do this that have been listed in other comments.

Another way is to use DropScript (you can find it in the apple osx apps download site), which I use daily. The advantage of DropScript is that the application it creates is duly registered, and you can associate file types to that application (e.g. I have ps and eps files associated to a dropscript-created app that launches gv).

Another thing: The proposed hint mention moving away the .xinitrc while launching and then restoring it. I think this is really bad practice. You don't want to mess with your other settings, or with config files used by other apps. What if your script die in the middle, for any reason (e.g. it can not find the unix executable)? Then your .xinitrc is not there anymore, and you might find yourself looking for the bug for some time the next time you start up X11.

That's one of the base principle of OSX: one app does not mess with other apps preferences. Stick to it, you'll find yourself much better for it.

Finally, to answer one of the precious comment question: No, there is no way (as far as I know) to make the application pass a file you would drop on the icon to the unix executable. This would require much more than just an applescript.



[ Reply to This | # ]