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

Easy application launch Terminal script UNIX
Here is a shell script that will open applications from the terminal without having to type out the full path to the application. It assumes that your applications are kept in /Applications. Using this script and an alias, you can type something like "o TextEdit" to open TextEdit, or "o TextEdit ~/Documents/some_file" to open some_file in TextEdit.

If you'd like to see the script, read the rest of the article.

A good place to store these user-specific scripts is in a 'bin' directory in your user's home diretory. Your path is already set up to look there (type 'echo $PATH' to see it). If you don't have one, just create a folder named 'bin' at the same level as Documents, Movies, etc. Once you've done that (in the Terminal or the Finder), open a Terminal and 'cd ~/bin'.

The first step is to create the script. Type 'pico openApplication' (or use your favorite editor, or even TextEdit in the Finder) to launch the editor, and then paste in the following (note the first line is just a colon by itself!):
:
# @(#) --Opens an application if found in /Applications folder BC 10/01
APPDIR="/Applications/"
SUFX=".app"
APPNAME=$1
if [ -e $APPDIR$APPNAME$SUFX ]
then
#if there is more than one argument assume that it is a file to be opened
if [ $# -gt 1 ]
then
shift
echo found $APPNAME$SUFX
echo Attempting to Open application along with $# Files
open $APPDIR$APPNAME$SUFX $*
else
echo found $APPNAME$SUFX
echo Attempting to Open application
open $APPDIR$APPNAME$SUFX
fi

else
if [ $# -eq 0 ] #if no arguments were given show proper usage
then
echo
echo usage openApplication applicationName [ filename1 filename2 ... ]
else # Show some stuff to help out
echo
echo $APPNAME Not found in /Applications Folder
echo
echo Capitalization is Important!!
echo
echo Do not add .app to the Name, I\'ll do it for you
echo
echo Here is a list of applications in your /Applications folder
echo
echo /Applications/*.app
fi
fi
Save your changes, making sure the script winds up in the ~/bin directory. In the Terminal, type "chmod +x ~/bin/openApplication" (this makes the script executable) and then 'rehash' (tells the shell to find new programs).

You can now type "openApplication TextEdit", for example, to launch TextEdit. Big deal you say -- typing out 'openApplication' isn't much shorter than typing out the path in the first place. But a quick alias takes care of the problem. Add the following line to your aliases.mine file (in ~/Library/init/tcsh, which you may have to create if it doesn't exist):
alias o `openApplication`
Of course if you are already using 'o' as an alias for something else, choose another shortcut letter. To see what aliases you are using, type 'alias' with no arguments and hit enter. Once you've put the alias in the aliases.mine file, close and open the Terminal. Now you have the command 'o' at your disposal in any Terminal window.

(If you need more help with the aliases.mine file, just search macosxhints for a 'aliases.mine')

How to use the script:
Type "o [application name], without .app appended, and the path of any files you want the application to open. Remember, this only works for apps in the /Applications directory. Some examples:
o TextEdit
--will open TextEdit
o TextEdit ~/Documents/note.txt
--will open the document note.txt with TextEdit. You can add as many files as you wish, if a filename has a space in it enclose the filename in quotes.
    •    
  • Currently 3.75 / 5
  You rated: 3 / 5 (4 votes cast)
 
[31,075 views]  

Easy application launch Terminal script | 13 comments | Create New Account
Click here to return to the 'Easy application launch Terminal script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
spaces in app names
Authored by: Hes Nikke on Nov 10, '01 03:38:12AM

how does this deal with spaces? what about spaes? eg my path to BBEdit is "/Applications/BBEdit 6.0 Folder/BBEdit 6.0/BBEdit 6.1 for OS X"

would i have to use
%o BBEdit 6.0 Folder/BBEdit 6.0/BBEdit 6.1 for OS X filename

or would it just figure out what i mean by
%o BBEdit filename

i would only use this script to open things with BBEdit, and i don't want to bother installing it untill i know how this would work.



[ Reply to This | # ]
spaces in app names
Authored by: percy on Nov 10, '01 08:06:04AM

Look through the script, it's reall not hard to follow. Do you think it looks like it searches through all sub folders in the Applications folder? It's not that I mean to sound rude, but the script is right in front of you.



[ Reply to This | # ]
spaces in app names
Authored by: percy on Nov 10, '01 08:19:23AM

By the way. If you only want to open BBEdit, you can easily do it by adding an alias to ~/Library/init/tcsh/aliases.mine. Like for instance:

alias ob "open -a '/Applications/BBEdit 6.0 Folder/BBEdit 6.0/BBEdit 6.1 for OS X'"

Then you'd just have to write ob [filename] [filename2] to open that file.



[ Reply to This | # ]
spaces in app names
Authored by: littlebilly on Nov 10, '01 02:07:09PM

There are 2 ways to have this script open applications that are enclosed in folders inside your /Applications folder.

1. add a line for each application that is embeded in folders to your aliases.mine file
I recomend the second option rather that cluttering up your aliases.mine file

Or (better I think)....
2. In the finder go through your ~/Applications folder and locate all the applications that reside in subfolders and that you commonly use, make aliases for each of them and place them in the /Applications folder. If the app name is the same as the Folder that was holding it them something different than the folder name. I used to append ƒ (option f) to the foldername but this is cumbersome to navagate to in the Terminal. Devise a scheme that works for you

For example the folder for GraphicConverter is called "GraphicConverter US" I make an alias (in the finder) of the application in that folder and rename it "graphicConverter" (from "GraphicConverter alias") and move it to the /Applications folder
now i can just type (with script from original post)

o graphicConverter ~/Pictures/park.tiff

instead of

open "/Applications/GraphicConverter US/GraphicConverter.app" ~/Pictures/park.tiff

Obviously you could have named the alias something even shorter.

so For Hes Nikke's Question here is what to do:
make an alias of the application (BBEdit 6.1 for OS X alias) and rename it "BBEdit", place this in you /Applications folder and now all you have to type to open filename.txt is

o BBEdit filename.txt

and if you want to open BBEdit from the finder you dont have to navigate several folders to get to it.

I should have included this in the original Post but I have always created aliases in my applications folder for applications that are "burried" and frequently used. Something that makes opening apps easier from both the terminal and from the finder.



[ Reply to This | # ]
A better way (soon)
Authored by: sabi on Nov 10, '01 05:29:34PM
I'm working on a replacement for 'open', to be called 'launch'. The help message may give you some idea of what it does (most of the functionality is there now, I just need to rearrange things so it opens by default, add a couple of features, and I'll release it):

[p6:1] ~%FindApp                                                         4:24PM
usage: FindApp [-opsbmhCX] [-c creator] [-i bundleID] [-u URL] [-n name] [document ...]
   or: FindApp [-oplsbmhCX] item ...
  -o            attempt to launch target(s) instead of printing paths/URLs
  -p            print document(s) instead of opening them
  -l            launch URLs (e.g. treat http:// URLs as Web sites, not WebDAV)
  -s            launch target(s) as superuser (authenticating if needed)
  -w            wait for application to finish opening before exiting
  -b            launch application in the background
  -m            launch application again, even if already running
  -h            hide application once it's finished opening
  -C            force CFM Carbon application to launch in Classic
  -X            don't start Classic for this app if Classic isn't running
  -c creator    match application by four-character creator code ('ToyS')
  -i bundle ID  match application by bundle identifier (com.apple.scripteditor)
  -u URL        open item with file:// URL (NOT RECOMMENDED for scripts)
  -n name       match application by name (NOT RECOMMENDED, very fragile)
'document' may be a file, folder, or disk - whatever the application can open.
'item' may be a file, folder, disk, or URL.
Some examples:
[p6:4] ~%FindApp -i com.barebones.BBEdit                                 4:26PM
/Volumes/GrayApps/Applications/BBEdit 6.1.2/BBEdit for OS X
[p6:5] ~%FindApp -c 'R*ch'                                               4:28PM
/Volumes/GrayApps/Applications/BBEdit 6.1.2/BBEdit for OS X
[p6:6] ~%FindApp -n 'Disk Copy'                                          4:28PM
/Applications/Utilities/Disk Copy.app
Here's an example of how to use it as a script to launch BBEdit (pre 6.5).
[p6:3] ~%which bbedit                                                    4:26PM
bbedit () {
        for f
        do
                if [[ ! -f $f ]]
                then
                        echo "n" > $f
                fi
        done
        FindApp -oi com.barebones.BBEdit $@ 2> /dev/null
}
Using bundle identifiers (i.e. com.barebones.BBEdit) is a nice, human-readable way to specify applications compared to creator code, and it's robust if the application changes its name (modulo some LaunchServices bugs that I've reported to Apple...).

[ Reply to This | # ]
Released! (sort of)
Authored by: sabi on Nov 15, '01 02:51:55PM

I've released an alpha version of launch for testing, please download
and let me know what you think.

http://web.sabi.net/nriley/software/



[ Reply to This | # ]
the find command...
Authored by: mervTormel on Nov 11, '01 04:17:29PM

seems that the 'find' command could help a couple of ways:

- traverse a hierarchy
- find all apps, or specific app

% find /Applications -name "*.app"
/Applications/Address Book.app
/Applications/AppleScript/Script Editor.app
/Applications/AppleScript/Script Runner.app
/Applications/Calculator.app
/Applications/Chess.app
/Applications/Clock.app
/Applications/Dantz Beta/RetroClient.app
/Applications/DVD Player.app
/Applications/Image Capture.app
/Applications/Internet Connect.app
/Applications/Internet Explorer.app
/Applications/iTunes.app
/Applications/iTunes.app/Contents/Resources/iTunesHelper.app
/Applications/Mail.app
/Applications/Preview.app
/Applications/QuickTime Player.app
/Applications/Sherlock.app
/Applications/Stickies.app
/Applications/StuffIt Expander.app
/Applications/System Preferences.app
/Applications/TextEdit.app
/Applications/Utilities/AirPort Admin Utility.app
/Applications/Utilities/AirPort Setup Assistant.app
/Applications/Utilities/Apple System Profiler.app
/Applications/Utilities/Applet Launcher.app
/Applications/Utilities/ColorSync Utility.app
/Applications/Utilities/Console.app
/Applications/Utilities/CPU Monitor.app
/Applications/Utilities/DigitalColor Meter.app
/Applications/Utilities/Directory Setup.app
/Applications/Utilities/Disk Copy.app
/Applications/Utilities/Disk Utility.app
/Applications/Utilities/Display Calibrator.app
/Applications/Utilities/Grab.app
/Applications/Utilities/Installer.app
/Applications/Utilities/Java Web Start.app
/Applications/Utilities/Key Caps.app
/Applications/Utilities/Keychain Access.app
/Applications/Utilities/NetInfo Manager.app
/Applications/Utilities/Network Utility.app
/Applications/Utilities/Print Center.app
/Applications/Utilities/ProcessViewer.app
/Applications/Utilities/StuffIt Lite 6.5/StuffIt Expander.app
/Applications/Utilities/Terminal.app

% find /Applications -name "*nfo Man*.app"
/Applications/Utilities/NetInfo Manager.app



[ Reply to This | # ]
Am I missing something...
Authored by: serversurfer on Nov 11, '01 11:33:59PM
or is all of this kind of a waste of time?
First of all, the original poster suggested you call the script 'openApplication' Then, to avoid having to type openApplication all the time, he says you should alias it to 'o'. Is there some reason for not calling the script 'o' in the first place?
Second, and perhaps most important, it seems to me that all this script does is reduce the functionality of the open command. open, on its own, is case-insensitive, handles spaces in app/file names (just quote them), opens Carbon applications (which don't have an '.app' extension), and, best of all, it automagically searches down in to your entire Application tree to find the app in question (This includes /Applications ~/Applications and even /Developer/Applications. I presume it also searches /Network/Applications, but I can't test this on my setup.)
So for example, the command
open -a "appleworks 6" ~/documents/budget.cwk

executed from any directory, opens my Budget in AppleWorks, which resides in "/Applications/Productivity/Office/AppleWorks 6/Appleworks 6.app".
open -a "quartz debug"

will open "/Developer/Applications/Quartz Debug.app"
open -a "contract timer"

will open "~/Applications/Contract Timer" which is a Carbon app in my home app folder (which must be called Applications). As a matter of fact,
open -e readme

will still open the readme with TextEdit, even though it now lives in "/Applications/Productivity/Editors/TextEdit.app". (The man page states it must live in /Applications)
So, unless someone can demonstrate that I am an idiot, and you are looking to save a few keystrokes, I would recommend that you just
alias o 'open -a'

This will let you open iTunes by typing
o itunes

regardless of where you moved it, as long as it's still downstream of an Applications folder that resides in '/' '~/' '/Developer/' or '/Network'
Furthermore, open also uses the "default" application defined in the Finder to open files. So in the first example, you could just type
open ~/documents/budget.cwk

and open will use AppleWorks automatically.
Like I said, if I'm missing something, let me know, but I just tested all of these tips and they all work on my 10.1 iBook466.
P.S. Can anyone get StuffIt Expander to open on a remote machine? The open command works on a local box, but on a remote, it always reports
kCGErrorIllegalArgument : CGSNewConnection cannot get connection port

kCGErrorIllegalArgument : CGSNewConnection cannot get connection port
kCGErrorInvalidConnection : CGSGetEventPort: Invalid connection


[ Reply to This | # ]
Yes :-)
Authored by: sabi on Nov 12, '01 12:04:58AM

Locating an application by name does not work if you've renamed the
application. Consider the standard practice of including an
application's version in its name, e.g. (picking one example on my
hard disk) "Adobe GoLive 5.0". Once you upgrade to GoLive 6, all your
scripts that refer to it will break. This is why matching an
application by name is not the answer. See my post above, a decent
replacement for 'open' would match apps by creator and/or bundle ID,
neither of which change no matter where you move the application. Of
course there are still issues if you have multiple versions of an
application installed.



[ Reply to This | # ]
Perhaps,but...
Authored by: serversurfer on Nov 12, '01 03:19:01AM
I renamed ~/Applications/Xoptimize/Xoptimize to Xoptimize2 in the Finder then used the duplicate menu item on it. Then, in the terminal, I executed
o xoptimize2

o "xoptimize2 copy"

This opened two copies of Xoptimize side by side. I didn't have to log out or even rehash. They both still thought they were called Xoptimize, but they operated independently of each other. (X rules)
I don't disagree that your program looks pretty cool. The 'open owner of this pref' option seems interesting enough on its own. I'm just saying that for opening apps and their data files from the terminal open does the job pretty well. And as far as breaking all your old scripts when you update/rename your app, just run the scripts through sed. Besides, that's why you shouldn't put version numbers in the bin name.
BTW, is your app called launch or FindApp? :-)

[ Reply to This | # ]
Am I missing something...
Authored by: littlebilly on Nov 12, '01 01:36:01PM

The long name of the script is just to reduce confusion when I want to edit, remove or replace a script. As for your second point... not having to type the full path name.. Your right. I have already quit using this script. Is this new to 10.1? Before upgrading I am pretty sure I had to type the full path.. hence the script. Thanks for pointing that out, I would have gone on using my "limited features" script.



[ Reply to This | # ]
New to 10.1?
Authored by: serversurfer on Nov 12, '01 07:16:21PM
I'm pretty sure that it has always been this way. I don't recall ever typing a path myself. I reread the man page just now (still dated Mar. 24, 2001) and it states
You can specify one or more file names (or pathnames), which are interpreted relative to the shell or Terminal window's current working directory.
Perhaps this line caused your confusion. At first glance, it seems to be telling you to specify any needed pathnames, but I think this line only refers to the file argument not the application arg. Since it uses the "default" app automatically if unspecified, this means it already had some ability to search on its own. It is possible that open 10.0.x only searched '/Applications' (where 98% of my apps reside) and not the other appdirs, like '~/Applications', but I kind of doubt it, since all four appdirs have always been considered part of the Application tree. See Inside Mac OS X: System Overview pages 166 and 242.
I have been meaning to install X on my parent's iMac. Whenever I finally get around to it, I'll test this before I upgrade them to 10.1 as post back here. Unless someone out there is still running 10.0.x and can beat me to the punch. (What's wrong with you anyway? X.1 rules! ;-) )

P.S. You can even use a directory as the file arg and open will open a new Finder window in that directory.

open ~

If the specified directory is already the focus of a Finder window, Finder will bring that window to the front rather than open a new one.

[ Reply to This | # ]
New to 10.1?
Authored by: littlebilly on Nov 19, '01 03:21:04PM

I figured out why I had to type the whole path-- I wasn't using the -a option! so i was using
open /Applications/TextEdit.app to open from the terminal.
I have 10.04 installed on another drive and it is the same.
A couple of seconds spent looking at the man page would have saved me some trouble.



[ Reply to This | # ]