Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking for a simple script that will do the following. Can anyone take a shot at it (+ if cryptic, please explain what it does), or give me some pointers so I can run this myself?

I want to start up a GUI application in the Applications directory from the command line prompt, giving it parameters if necessary:

$> launch Foobar arg1 arg2 arg3

What the launch shell script should do is:

1) Read a configuration file that looks something like this:


2) Try to match the first argument ("Foobar") in this case with each line of the configuration file, in order, with "" representing any sequence of characters in a path segment and "*" representing any path segment, and $0 representing the program name. So in this case it's looking for and Foobar in the Applications directory and any of its subdirectories.

3) Execute the resulting command with the remaining arguments as specified ("arg1", "arg2", "arg3")

It sounds easy, but I'm too much of a newbie with shell scripts at the moment. I may end up using JSDB since I know how to use it. I am just not sure how to specify that a script called "launch" should require the JSDB program to run it, or whether it's compatible with doing that.

Specific use case: If I'm in a directory within the command prompt, I often want to do one of the following:

  • Launch TextEdit to create a new file named X
  • Launch TextEdit to open a file named X
  • Launch {name your favorite program} to create or open a file named X
  • Open a Finder window in this directory

In particular, I wanted to edit my Mercurial .hg/hgrc file yesterday, and it took me forever to figure out how, since .hg is a directory that's hidden from GUI file-open dialog boxes. I still don't remember how I got it to work.

share|improve this question
up vote 6 down vote accepted

"open" is the command you want. Use this to see the open options

open -h

You'll see that to open something in textedit, just use -e

open -e foo.txt

if it doesn't exist, just "touch" the file and open it:

touch foo.txt 
open -e foo.txt

There are other applications out there (like textmate) that I think are better GUI editors that install their own command line command (ie: mate) that can automatically create files.

To open the current file with the default editor, just don't pass any switched. So if .txt files are associated on your system with TextEdit, just using "open foo.txt" should work. Since directories are automatically associated with Finder, just use

open .

to open the current directory up in finder.

share|improve this answer
If the file is associated with a different program, won't this open it with some other than TextEdit, for example? – Jared Updike Jul 17 '09 at 18:55
right, you can see what the default association is by right clicking on a file in finder and choosing "get info" under the "open with" section. This is also the place where you can change the default association for all files with the same extension, by changing the app in the dropdown and hitting "change all..." – Ted Naleid Jul 17 '09 at 20:39
You can do open -a TextMate file.txt to force a file to open in a non-default application – dbr Jul 21 '09 at 8:44

One quick way of launching a GUI app from the Terminal it to just use open -a AppName. But you can't pass along command line arguments that way.

However, it seems like the minority of programs even accept command line arguments, it would be crazy to require the user to write a script like this just to use them. GUI apps that do accept command line arguments usually have shell scripts in the PATH to handle them, for example mvim.

Could you provide a more specific example of what you hope this script would achieve?

share|improve this answer
You stole the first three letters of my username! :P – jtbandes Jul 17 '09 at 17:13
I noticed that myself but I wasn't reputable enough to leave you a comment :-) – jtb Jul 17 '09 at 17:32
so I would do "open -a Finder" to run Finder in the current directory? – Jason S Jul 17 '09 at 17:41
If you want to open Finder in the current directory you'll need to specify the directory. Use . for the current directory. open -a Finder . would work, or since Finder is the default app for directories you can just type open . – jtb Jul 17 '09 at 19:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .