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

Open files from the Terminal with the appropriate application UNIX
I often have files of the same type (eg: text, html, jpg) created by different programs, so if I double-click or type

open foo.jpg
the file might open in Preview, or Photoshop, or AppleWorks, or whatever. With html and text documents, depending on what I would like to do, I might want to open the file in SubEthaEdit, or in a WYSIWYG editor, or browse it in Safari. Although it is not hard to type

open -a Preview foo.jpg
it is more annoying to remember to type

open -a "Adobe Photoshop 7.0" foo.jpg
and when I accidently invoke Photoshop, I have to sit there for 30 seconds watching it lumbering to start up, when it would be nice to have a chance to abort and open in Preview. So to learn a bit about zsh shell scripting, I wrote a zsh shell script called edit.zsh. You can make the script executable and drop it into /usr/local/bin/edit (or your favorite directory), and invoke it by typing

edit filename
to open a file in an appropriate program, or

edit -f filename
to force the file to be opened in your default editor. You can do this with any user shell as long as the file is in your path. Also, you can make it a zsh function (it will not work as a bash or ksh function because of the differing syntax.)

I've chosen some defaults, but have tried to make it easy for the user to customize the script to her or his own needs. (These defaults include SubEthaEdit for text editing, Photoshop for image editing, Netscape composer for WYSIWYG html editing, and so on.) Where more than one option seems useful (say editing an HTML file in Netscape vs. SubEthaEdit), the script prompts you for a choice and waits for input for a few seconds before choosing a default. It attempts not to create inappropriate suffix files like a jpg file with a text editor (but the -f flag gives you the option to force it).

I've also added the ability to edit files that you don't own, but for now the only way I've found to let you do this is with the command sudo vi(m).

The script doubtlessly could be improved and I welcome all constructive feedback. I hope this is of use to someone.
  • Currently 3.29 / 5
  You rated: 3 / 5 (7 votes cast)

Open files from the Terminal with the appropriate application | 4 comments | Create New Account
Click here to return to the 'Open files from the Terminal with the appropriate application' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Open files from the Terminal with the appropriate application
Authored by: wgscott on Jan 30, '04 12:23:22PM

I should have mentioned this works on 10.3.x but not on 10.2.x

[ Reply to This | # ]
Open files from the Terminal with the appropriate application
Authored by: MattHaffner on Jan 30, '04 04:37:25PM

You can abort: right click and force quit the bouncing app in the dock. No need to wait and 'Quit' properly.

[ Reply to This | # ]
Authored by: gatorparrots on Jan 30, '04 06:49:04PM
Gee Bill, I thought you said one day you would have to stop customizing your work environment and actually get work done.
(Isn't zsh addictive?)

[ Reply to This | # ]
Custom "open" implementation, anyone?
Authored by: kmue on Feb 02, '04 03:46:00AM

I recently stumbled over an "open" implementation in C which is much more conffgurable than the one provided by Apple. Unfortunately I can't find the location again. Anyone?

[ Reply to This | # ]