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

Launch X11 apps from the Finder UNIX
I was thrilled to see the X11 release package from apple. Finally, an easy and clean way to install X on my machine. Along with the gtk/gimp packages made available from the opendarwin.org people, I now have gimp and X11 installed and working great.

It was all so easy... but what I really wanted was to be able to start The Gimp from the Finder. So I whipped up a quick applescript to do just that. This should work, more or less for any X11 app you want to use. It will launch X11 if it's not already running, and then launch your X app.
Use Script Editor to save this applescript as an application... and viola! dockable Gimp.
tell application "Finder"
launch application "X11"
end tell

set results to do shell script "cd ~; DISPLAY=:0.0;
export DISPLAY; PATH=$PATH:/usr/local/bin; export PATH ;
/usr/local/bin/gimp > /dev/null 2>&1 &"
[Note: The "set results" bit is one long line; replace each line break with a single space.]

If you want to use this for something other than the gimp, just change the /usr/local/bin/gimp to the app you want to start.
    •    
  • Currently 2.20 / 5
  You rated: 3 / 5 (5 votes cast)
 
[26,900 views]  

Launch X11 apps from the Finder | 20 comments | Create New Account
Click here to return to the 'Launch X11 apps from the Finder' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Didn't try this but...
Authored by: VEGx on Jan 20, '03 12:15:48PM

Yesterday, we were trying to get this work:
set result to do shell script "/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background &"

and the script just hangs if you run it in the Script Editor. Which makes me very suspicious about all "&" ending scripts. I have a feeling it's taken away the possibility to run scripts in the background... but then again... I didn't try your script... maybe it works... I'll try it in a sec...



[ Reply to This | # ]
Didn't try this but...
Authored by: macavenger on Jan 20, '03 05:20:37PM

after some experimenting, it looks to me like the key part (to keep the script from hanging) is the '> /dev/null 2>&1 &' If you take this section out, it still works, but the script hangs. Put it back in, and the script exits cleanly. On a related note, the "set results to" section can be shortened to: set results to do shell script "open-x11 /path/to/gimp > /dev/null 2>&1 &"



[ Reply to This | # ]
Didn't try this but...
Authored by: Bernd P. Ziller on Jan 25, '03 08:17:10AM

As you found out, the key part is the redirection of stdout and stderr to /dev/null...

by this you fully detach the background job from shell. if you don't do this, the shell and your backgrounded command still share the same file-handles for stdout and stderr, which makes your script hang.

Sometimes you might even have to redirect stdin with '</dev/null'...

as in 'command >/dev/null 2>&1 </dev/null &'.



[ Reply to This | # ]
If you have `fink'
Authored by: VEGx on Jan 20, '03 12:21:48PM

tell application "Finder"
launch application "X11"
end tell

set results to do shell script "cd ~; DISPLAY=:0.0; export DISPLAY; PATH=$PATH:/usr/local/bin; export PATH ; /sw/bin/gimp > /dev/null 2>&1 &"

[Appears it's not the "&" that is the trouble... somehow the ScreenSaverEngine makes the script hang.]



[ Reply to This | # ]
how to open civserver
Authored by: bartelsrausch on Jan 31, '03 11:06:42AM

Hi,
would would a script look like to open civserer in a X-11 Terminal and civclient with the same applescript?



[ Reply to This | # ]
Just use a unix script
Authored by: Anonymous on Jan 20, '03 02:54:25PM

I've been doing this for a long time, but with ssh xwindow sessions:

#!/bin/sh
/usr/bin/open /Applications/X11.app
until ps -cx|grep quartz-wm
do tail /etc/hosts >> /dev/null
done
/usr/bin/ssh -X -C anne "/usr/bin/CAClient" &

There's probably a better way to ensure the window manager is up, but this was what I threw together on a quick session quite some time back, and it's functional so I've had no reason to rethink it... For the uninitiated, it's opening X11, running a loop to check for the existence of quartz-wm (which means X11 is up) then opening a ssh xwindow session to the machine "anne" and running the xwindow app "CAClient" as a background process on the remote box. With ssh keys this is a complete background process. Name the script whatever.command and it's a double-click process as well. As for adding the display env var in the script, I'd add that in the .cshrc, and have it available for all shells. Add these 2 lines to your .cshrc:

setenv PATH "${PATH}:/usr/local/bin:/usr/local/sbin:/usr/X11R6/bin"
setenv DISPLAY localhost:0

Now you can get xapps from any shell, anytime. Cleaner, IMHO.



[ Reply to This | # ]
Just use a unix script
Authored by: jafager on Jan 20, '03 05:30:12PM

This can be a real pain if you don't have a terminal running all the time, or if you use an application launcher like LaunchBar.

I have a USENET post on turning X and console applications into regular double-clickable applications via Applescript:



[ Reply to This | # ]
Point conceded...
Authored by: Anonymous on Jan 20, '03 10:01:34PM

Heh heh... Being a sysadmin, there's almost never a time that I don't have term open... And I also use ssh port forwarding to deal with my personal mail, so even when I'm not working term is up... But it's a good point, for the traditional non-unixy macuser the applescript has some definite advantages...



[ Reply to This | # ]
Script example for Command Line Mongrals
Authored by: ktohg on Jan 21, '03 02:44:03AM
My text editor (VIM) has both an X11 version and a MacOS version. I prefer the X11 one but don't always have X11 running. So I created a script to select the prefered version depending. It's a sample that you can use.
#!/bin/sh showhelp() { cat <<EOF This is a wrapper script for the graphical version of VIM. The script recognizes the following command line options. Only the first option will be recognized.    -h              This cruft    --help          vim --help    -x, --x11       force X11 gvim (Start X Server if                    not one already runnung)    -X, --no-x11    force MacOS gvim    --              NO-OP (pass remaining options as-is) If you do not force a display method one will be guessed based on the DISPLAY environment variable and the presence of X11.app running. EOF } case "$1" in    --) shift; X11=ukn;;    -h|-help) showhelp; exit -1;;    --help) exec vim --help;;    -x|--x11)  shift; X11=yes;;    +x|-X|--no-x11)  shift; X11=no;;    *)  X11=ukn;; esac case `basename "$0"` in    xvim) X11=yes;;    mvim) X11=no;; esac if [ -z "$DISPLAY" ]; then    if [ -z "`ps ax | grep X11.app | grep -v grep`" ]; then        if [ $X11 == yes ]; then            open -a X11            sleep 5            export DISPLAY=:0        else            X11=no        fi    elif [ $X11 != no ]; then        export DISPLAY=:0        X11=yes    fi elif [ $X11 != no ]; then    X11=yes fi case "$X11" in    yes) exec vim -g $*;;    no)  exec open -a Vim $*;; esac


[ Reply to This | # ]
Another More Generic Script
Authored by: ktohg on Jan 21, '03 02:51:23AM
This one is a simpler one than the gvim one but it is more generalized. It's not as pretty as the gvim one. Also is an alias so the DISPLAY veriable gets set properly (unline a script which only sets it locally. This allows me to open an X11 app using `x' once or just typing `x' will start up the X11.app. Here is my alias: alias x='export DISPLAY=:0;Xapp' and the Xapp program:
#!/bin/sh if [ -z "`ps ax | grep X11.app | grep -v grep`" ]; then    open -a X11    sleep 5 fi if [ -n "$*" ]; then    export DISPLAY=:0    exec $* else    exit 0 fi


[ Reply to This | # ]
an easy and clean way to install X on my machine?
Authored by: wgscott on Jan 20, '03 06:37:32PM

Essentially identical easy and clean installs have been available for about 2 years now. The only thing new here is speedy quartz display in X-windows. Also, literally thousands of X-windows based unix programs have been ported by the fink project over the last two years. Check out
http://fink.sourceforge.net/pdb/index.php



[ Reply to This | # ]
Aha!
Authored by: Eponymous on Jan 20, '03 10:59:39PM
Yes, it seems to be that last little bit that does it:
> /dev/null 2>&1
I know what that first little bit does (send responses out to never-never land), but what does the second part do?

This has allowed me to run another script that I couldn't figure out how to run without the terminal.

[ Reply to This | # ]
Sends stderr to the same place as stdout
Authored by: owain_vaughan on Jan 21, '03 10:16:59AM

ie. nowhere :)



[ Reply to This | # ]
Aha!
Authored by: david-bo on Jan 23, '03 05:51:22PM

To send stdout to a file you write something like this:

ls -R > allFiles.txt

2>&1 sends stderr to the same destination as stdout (typically a terminal).



[ Reply to This | # ]
Open documents in X11 apps from finder
Authored by: Bishop on Jan 21, '03 08:41:12AM
Not only can you launch applications from the finder, using XLaunch-O-Matic (http://xlaunch.sf.net) you can open documents in X11 apps by double-clicking on them in the finder.

This way you can have a .xpm or .ps file open with the gimp or gv in X11. This was noted earlier here: http://www.macosxhints.com/article.php?story=20020927044738228

[ Reply to This | # ]

How do I configure this for Matlab?
Authored by: hamarkus on Oct 09, '03 06:42:49AM

How do I configure XLaunch-O-Matic for Matlab 6.5?



[ Reply to This | # ]
Force mail to use X11 apps
Authored by: Donal Day on Jan 24, '03 10:24:35AM

I modified the script to launch ghostview (gv). Now I would like to force Mail.app to use ghostview (via this the application this script creates) to open postscript files that come as attachments.

1) How to do I modify the script to open when I drag files to it?

2) How do I get Mail.app to use it?

Regards,
dbd



[ Reply to This | # ]
Droplet to open files with X11 app
Authored by: mbclark on Jan 26, '03 03:52:00PM
I was just working on a droplet open one or more text files with Nedit when I ran across this tip. I just keep the droplet on my desktop for easy access. Here's the code for the droplet if anyone's interested. It could easily be modified to work with just about any X app.
on open these_items	
	tell application "Finder"		
		--Launch X11 and bring to front
		launch application "X11"
		--String that will be executed by the do shell script command
		--First set the display
		set theCommand to "export DISPLAY=':0.0'; /sw/bin/nedit "
		
		--Process each file dropped on the droplet, appending the 
		--POSIX path of the file to the end of "theCommand"
		set theFiles to these_items as string
		repeat with i from 1 to the count of these_items
			set theFile to POSIX path of (item i of these_items as string)
			set theFiles to these_items as string
			set theCommand to theCommand & " " & theFile
		end repeat
		
		-- Finish theCommand string
		-- Pipe output to stdout and stderr so the command 
		-- returns and AppleScript won't wait around for 
		-- stdout and stderr to close
		set theCommand to theCommand & "> /dev/null 2>&1 &"
		do shell script theCommand
	end tell
end open


[ Reply to This | # ]
it works!
Authored by: willchernoff on Aug 09, '03 11:33:41AM

the author is right about changing "set" as one line, However, with the posted code, you have to specify the right pathway to your desired application and there is not space between pathway and executable.

tell application "Finder"
launch application "X11"
end tell

set results to do shell script "cd ~; DISPLAY=:0.0;
export DISPLAY; PATH=$PATH:/usr/local/bin; export PATH ;
/opt/local/bin/gimp > /dev/null 2>&1 &"



[ Reply to This | # ]
if you have fink
Authored by: slacker on Aug 16, '03 09:53:00PM

I ran into some problems with my laptop when X11 wasn't loaded where the script was running before X11 was up. Stuck in a "delay #sec" command and it started working. This version of the script uses fink. Replace emacs with your favorite command. All X applications take a -display argument.

--- cut here

tell application "Finder"
launch application "X11"
end tell

delay 1

set results to do shell script "(cd ~; source /sw/bin/init.csh; emacs -display :0.0) > /dev/null 2>&1 &"

--- cut here



[ Reply to This | # ]