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

Associate X11 applications with filename extensions UNIX
I like using gv(ghostview, available from Fink) as my PostScript viewer. Unfortunately, you can't choose /sw/bin/gv in the Get Info -> Open With -> Other dialog. To get around this, you can use DropScript! Create a script like so (gv.open)
#!/bin/sh

PATH=/sw/bin:/sw/sbin:/usr/bin:/bin
/usr/bin/open -a X11

for file in $@
do
  gv $file
done
When you open this script with DropScript, it will create an application called "Dropgv." Then you can use this as the Open With application. The PATH line is necessary because /sw/bin isn't in the default path. As gvgs, writing /sw/bin/gv won't work. The open line will open X11 if it's not already open.
    •    
  • Currently 3.25 / 5
  You rated: 4 / 5 (4 votes cast)
 
[7,610 views]  

Associate X11 applications with filename extensions | 20 comments | Create New Account
Click here to return to the 'Associate X11 applications with filename extensions' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Associate X11 applications with filename extensions
Authored by: cedric on May 16, '03 11:46:52AM

It does not work on my machine! I was looking for such a hint, but I failed to get it right. I also tried "csh" instead of "sh", but no results. I use DropScript 0.5, gv is installed with Fink (and Apple X11 Beta 3).

What it does is launching Dropgv and quit immediately.

Help?!



[ Reply to This | # ]
Associate X11 applications with filename extensions
Authored by: bts on May 16, '03 01:37:55PM

You need to have Fink installed, and then have used it to install gv, for this to work. (Technically, you could install gv yourself in /sw/bin, but that would be a strange choice.)

-Brian



[ Reply to This | # ]
Associate X11 applications with filename extensions
Authored by: fxt on May 16, '03 01:06:13PM

this doesn't work for me either. the basic script works from the terminal, but not when turned into an app via DropScript. using the console, i've tracked it to "gv: Unable to open the display." so, i tried setting DISPLAY=:0.0 and several other variants, but it still fails. 've also played with different shells and different permissions, but they all fail as well. thoughts?

fxt



[ Reply to This | # ]
Associate X11 applications with filename extensions
Authored by: ssevenup on May 16, '03 03:18:41PM
Several comments...

First.. I have the PATH variable set in my "~/MacOSX/environment.plist" as per this hint . I also have my "DISPLAY=:0.0" set there. The interactions between environment.plist and your shell environment "dot" files is not well documented, so use at your own risk.

Second.. I prefer to check to see if X11 is already running.

Third.. I had a few issues with syntax, so I tweaked some of the quoting. Here is the beef...


#!/bin/sh

# Not needed if set in ~/.MacOSX/environment.plist
#
# PATH=/sw/bin:/sw/sbin:/usr/bin:/bin
# DISPLAY=:0.0

StartX11 ()
{
x11pid=$( ps -auxww | grep X11.app | grep -v grep | awk '{ print $2 }' )
if [ -z "${x11pid}" ]; then
	/usr/bin/open -a X11.app
fi
}

StartX11

for file in $@
do
	/sw/bin/gv "${file}"
done

I found that the file opened in gv faster if the PATH was set in environment.plist instead of the shell script. You will also notice that I use the full path to the binaries just on general principals. I never figured out how to pass gv the path to gs to eliminate the need for the PATH completely, but I'm convinced it's possible.

Using Open Menu I can now right click a ".ps" file, and open it straight into gv... pretty cool, and it opens the door to many other possibilities.


---
Mark Moorcroft
ELORET Corp. - NASA/Ames RC
Sys. Admin.

[ Reply to This | # ]

Associate X11 applications with filename extensions
Authored by: cedric on May 16, '03 03:55:14PM

I have used your version of the script, but still, it does not resolve the problem of "Unable to open display". The script works by itself very well from the command line, but not with DropScript. Maybe it's a problem with DropScript (I'm not a expert at all on Cocoa app).

Any clue?
Thanks.

Cedric



[ Reply to This | # ]
Associate X11 applications with filename extensions
Authored by: ssevenup on May 16, '03 07:38:51PM

Uncomment the PATH and DISPLAY lines or follow the tip for adding them to your ~/.MacOSX/environment.plist like I did.

--MM

---
Mark Moorcroft
ELORET Corp. - NASA/Ames RC
Sys. Admin.



[ Reply to This | # ]
Associate X11 applications with filename extensions
Authored by: ssevenup on May 16, '03 08:03:52PM

Remember to log out and back in after changing the environment.plist.

--MM


---
Mark Moorcroft
ELORET Corp. - NASA/Ames RC
Sys. Admin.



[ Reply to This | # ]
Associate X11 applications with filename extensions
Authored by: KOHb on May 17, '03 12:18:52PM

If you really want to get rid of the PATH dependence, you can use .Xdefaults and set "GV.gsInterpreter". (Look at man gv.) It looks like it'll even use a ".gv" file if you want.

I timed (using zsh's 'time') checking for X11 already running versus just running "open -a X11" (which doesn't seem to do anything except gain window focus) and open was faster. YMMV.

One could probably write an all-in-one Unix opener (per extension) by copying the idea of UnShove.sh (from DropScript's example).

So far I use the idea to open postscript, dvi, and occasionally vim.

BTW, a quick way to make these dropscripts is to do "open -a DropScript script.sh".



[ Reply to This | # ]
Associate X11 applications with filename extensions
Authored by: unforeseen:X11 on Jun 07, '03 08:17:03PM
by editing gv to gimp, this script opens dropped pics with my fink-installed Gimp. the problem is that every image gets opened within a "new" gimp, means that the gimp starts up several times. has anyone figured out how to pass the variables to the (eventually) already running gimp??

[ Reply to This | # ]
use multiple X11 applications
Authored by: jfg on May 16, '03 05:22:30PM

I have been working on doing similar stuff for a while now. I wanted to use one of several X11 applications with one drop application. I chose to do this with an applescript application. Because of the kinds of problems I see in this thread, I use a unix script called "doinshell" that executes the commands in a tcsh or from the terminal so that I have exactly the same environment as if running the command from a terminal. Put this shell script in "~/bin/doinshell" and make it executable "chmod a+x ~/bin/doinshell". (Make sure the terminating character is unix-style). Note that this shell script creates a temporary file in your home directory containing the list of commands called .doinshell0 etc, but it is removed after completing the commands.

#! /bin/sh
usage="Usage: `basename $0` [-h] [-t title] [cmd]"
title=NONE
tflag=0
while :
do
case "$1" in
-h*)
echo "execute commands within a tcsh shell"
echo "[option: within a new Apple terminal]"
echo $usage
echo " -h help"
echo " -t in terminal with specified title"
exit 0;;
-t) tflag=1; shift; title="$1" ;;
*) break ;;
esac
shift
done
# create unique temporary file
for num in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
do
tempfile=~/.doinshell$num
if [ ! -e $tempfile ]
then
break
fi
done
echo "#! /bin/tcsh" > $tempfile

if [ $tflag -eq 1 ]
then
weird=\"\]2\;\\\!\:1\\"
echo "alias settermtitle 'echo -n $weird'" >> $tempfile
echo "settermtitle $title" >> $tempfile
fi

for cmd
do
echo "${cmd}" >> $tempfile
done
echo "rm ${tempfile}" >> $tempfile
chmod +x $tempfile

if [ $tflag -eq 1 ]
then
open -a /Applications/Utilities/Terminal.app/ $tempfile
else
exec $tempfile &
fi


Now save the following applescript (I call it "visual X") as an application selecting the save option "stay open", but not "run only", "startupscreen", or "requires classic".

--this applescript uses my homemade unix script "doinshell"
--which start an application within a tcsh (or terminal)
--this sets the environment the same as if typing the command at a terminal
property launchstr : "~/bin/doinshell "
property bgstr : " > /dev/null 2>&1 & " --need last bit to redirect standard output and error to proceed
property appNameList : {"GhostView", "XView", "ImageMagick", "Gimp"}
property appCmdList : {"/sw/bin/gv", "/sw/bin/xv", "/sw/bin/display", "/sw/bin/gimp"}
property appTermList : {"", "", " -t ImageMagick", ""}

on run
tell application "X11" to activate
end run

on open (fileList)
activate
set numApps to count of appNameList
set xappName to the first item of (choose from list appNameList with prompt "View with X11 application:" default items {"GhostView"})
tell application "X11" to activate
set n to 1 as number
repeat numApps times
if xappName is item n of appNameList then
set xapp to item n of appCmdList
set termstr to item n of appTermList
exit repeat
end if
set n to n + 1
end repeat
repeat with theFile in fileList
tell application "Finder"
set theFolder to container of theFile
set file_name to the name of theFile
set file_ext to the name extension of theFile
end tell
set file_name to "'" & file_name & "'"
set folder_name to theFolder as string
set unix_dir to POSIX path of folder_name
set unix_dir to "'" & unix_dir & "'"
set cdcmd to " \"cd " & unix_dir & "\" " as string
set cmd to "\"" & xapp & " " & file_name & "&\" " as string
set wholecmd to launchstr & termstr & cdcmd & cmd & bgstr
do shell script wholecmd
end repeat
end open


Now you can drop a file or files into the applescript and open in either gv, xv, ImageMagick or gimp. For some reason, I am only able to launch ImageMagick from the terminal so I use the -t option of the "doinshell" script.

Hope someone finds this useful.



[ Reply to This | # ]
use multiple X11 applications
Authored by: jfg on May 16, '03 06:45:41PM

The strange characters that make up the string "weird" did not come out correctly. If you want the correct characters you can cut and paste them from the file "/usr/share/tcsh/examples/aliases"



[ Reply to This | # ]
use multiple X11 applications
Authored by: ssevenup on May 16, '03 07:49:11PM

You need to use the

<pre>
<code>

Code text

...

</code>
</pre>

tags (html format) to preserve the wierd stuff. That's what makes the code stand out in yellow. There should be an FAQ or style guide somewhere here, but I have never found one.

Your mechanism is cool, but it's seems a bit Rube Goldberg ;-)

--MM


---
Mark Moorcroft
ELORET Corp. - NASA/Ames RC
Sys. Admin.



[ Reply to This | # ]
Associate X11 applications with filename extensions
Authored by: fxt on May 16, '03 07:22:15PM

ok, i finally got this hint to work with a tcsh script and dropscript. still can't figure out why i couldn't get it to work under sh.


#!/bin/tcsh -f

setenv PATH ${PATH}:/sw/bin:/sw/sbin:/usr/bin:/bin
setenv DISPLAY :0.0
/usr/bin/open -a X11

foreach file ($*)
/sw/bin/gv "${file}"
end

you can insert the fancier if-x11-running test mentioned
above if you like. does this work for you cedric?

fxt



[ Reply to This | # ]
Associate X11 applications with filename extensions
Authored by: cedric on May 16, '03 07:53:40PM

> you can insert the fancier if-x11-running test mentioned
> above if you like. does this work for you cedric?

Well, not completely...

I took your version of the script, and change absolutely nothing. So now, when I right-click to a .ps document in the Finder and choose "Open with... Other..." and then choose Dropgv (that I have put in the Applications/Utilities Folder) it works.

So I said "well, I 'get info' on one .ps file and set the preferences to open every .ps file with Dropgv. Then, when I click to a .ps file in the folder or in Mail, it shoud work. "

But it does not. It works only when I click "Open with... Other..." Strange, isn't it? I tried many possibilities, different documents etc. Same behavior for every .ps document.

I checked the console. Sometimes I still get "Unable to open the display" and sometimes I get:

gv: Unable to open the display.
May 16 19:44:45 Cedric-Computer WindowServer[183]: CGXGetWindowShape: Invalid window 2951

or:

May 16 19:44:40 Cedric-Computer WindowServer[183]: CGXGetScreenRectForWindow: Invalid window 6125


We are getting closer to the Truth, but still...

Cedric



[ Reply to This | # ]
Associate X11 applications with filename extensions
Authored by: Karinms on May 30, '03 04:31:43AM

Did you ever figure out what is wrong here? I am having a similar problem with Dropgv. it will work for one file, but when I set it to open all .ps files with Dropgv, it will not work.

On my console I get the message:
2003-05-30 00:57:08.088 Dropgv[4748] *** NSTask: Task create for path /Users/karin/Documents/Dropgv.app/Contents/Resources/script failed: 8, "Exec format error".
May 30 00:57:08 Karin-Sandstroms-Computer crashdump: Crash report written to: /Users/karin/Library/Logs/CrashReporter/Dropgv.crash.log



[ Reply to This | # ]
Associate X11 applications with filename extensions
Authored by: airdrummer on Jun 10, '03 06:50:12PM

bummer...i'm getting the same error)-: any clues?



[ Reply to This | # ]
Associate X11 applications with filename extensions
Authored by: airdrummer on Jun 10, '03 09:37:07PM

oops, i didn't save my script as unix...the mac line endings rilly fsckup bash, although u'd never guess from the error msgs:-P

saving as unix makes the script work, and thus the dropscripted app works...although the "${file}" doesn't quote the args...



[ Reply to This | # ]
Associate X11 applications with filename extensions
Authored by: ssevenup on May 16, '03 08:01:12PM
How exactly are you specifying the environment variables in /bin/sh ? sh and tcsh have different requirements. You can add this to your script after the variables are set to troubleshoot it...

env >env_test.txt
Then look in env_test.txt to see if the variables are actually being set or not after running the script from the command line. --MM

---
Mark Moorcroft
ELORET Corp. - NASA/Ames RC
Sys. Admin.

[ Reply to This | # ]

Associate X11 applications with filename extensions
Authored by: fenner on May 16, '03 08:43:12PM
Using DropScript, I found it easier allow it to make the filename association itself by adding the special comments at the top of the shell script:
#!/bin/sh
#
# EXTENSIONS : "ps"
# ROLE       : Viewer


[ Reply to This | # ]
Associate X11 applications with filename extensions
Authored by: bdm on May 17, '03 09:25:05PM

Some people are reporting problems with sequences like this
in /bin/sh (and the same will be true for /bin/bash):

DISPLAY=:0.0
gv file

The explanation is that DISPLAY is only defined for the sh
process and is not passed to the gv process. This has been
a normal feature of the Bourne shell and its relatives since
the age of the dinosaurs. You have to explicitly tell the shell
to pass the variable to subprocesses. The traditional way is

DISPLAY=:0.0
export DISPLAY

In more recent shells including sh and bash on MacOSX, you
can do it in one step like this:

export DISPLAY=:0.0

None of these comments apply to shells like tcsh.

Brendan.



[ Reply to This | # ]