Project98.Open Aqua Items


Project 98. Open Aqua Items

"How do I open a Mac OS X application from the command line?"

This project is all about the open command. It's the command-line equivalent of double-clicking, used to open files and folders and to launch Mac OS X graphical applications.

Learn More

Project 18 shows you how to use the find and xargs commands in combination.


Open a Folder

To open a folder into a Finder window from the command line, use the open command, passing the name of the folder. Open your Documents folder, for example, by typing

$ open ~/Documents


Tip

Use open to open a folder that's normally hidden by the Finder. For example, open /etc by typing

$ open /etc



A Finder window opens, showing the contents of your Documents folder in Icon view.

Although this command is not very useful on its own, you could use open in a script to open a set of commonly used folders or as part of another command. You could search for folders named backup and open them all in a single command by typing

$ find ~ -type d -iname backup -print0 | xargs -0 open


Make a commonly used and deeply nested folder the subject of an alias for quick and easy opening.

$ alias archive="open ~/Documents/101/Chapter11/archive"


Open an Application

The open command simulates a double-click action and can be used to launch any of the applications in the /Applications directory. To launch Calculator, type

$ open /Applications/Calculator.app


An application written to Apple's guidelines is part of a package. Although it looks like an application icon in the Finder, a package is really a folder with a .app extension. The executable itself is located within the package, as are other resources, including the application's icon, displayed instead of the usual blue folder icon. Explore a package such as /Applications/Calculator.app: You'll find the executable code in the subdirectory Contents/MacOS/Calculator.

Learn More

Project 55 covers background jobs.


Older (Carbon-based) applications are not implemented as Mac OS X packages and don't have an .app extension. If, for example, your publishers were to insist that you use Microsoft Word, launch it by typing

$ open "/Applications/Microsoft Office X/Microsoft Word"


The open command will launch an application's executable directly if you specify the -a option. To launch the Calculator executable instead of double-clicking its package, type

$ open -a /Applications/Calculator.app/¬     Contents/MacOS/Calculator


Clicking and Launching

The open command issued without option -a simulates double-clicking, whereas with option -a, it launches an application directly. The difference is subtle, but you'll see it in effect if you apply option -a to a package or omit option -a when launching an executable directly.


An executable can also be launched directlywithout using open -ajust like a Unix command. (Remember to launch it as a background job so that it returns control of the Terminal immediately.)

$ /Applications/Calculator.app/Contents/MacOS/Calculator & [1] 22820 $


Tip

Run two or more Calculators at the same time by double-clicking (or using open without option a ) the executable instead of the package.


This technique does not work for Carbon-based applications such as Microsoft Word, because the executable is not in the native format required by Mac OS X. Although you'd probably use open to launch such an application, here's how to launch its executable directly.

$ /System/Library/Frameworks/Carbon.framework/Versions¬    /Current/Support/LaunchCFMApp ¬    "/Applications/Microsoft Office X/Microsoft Word" &


Short and snappy!

Open a File

The open command can be used to open a document in the default application for the document type. To open a screen shot that has the extension .png, in the default application Preview, for example, type

$ open ~/Desktop/Picture\ 1.png


Note

Although it's possible to pass an application's alias to the open command, this will not work when used in combination with the option -a.


You may use wildcards, of course, to open multiple files.

$ open *.png


Tip

The open command option -e is shorthand for

-a /Applications/TextEdit.app



Like double-clicking, open uses Launch Services to determine which application to launch to open a document. To have a document open in an application other than the default, specify the -a option, and name the application. To open the same screen shot in Adobe Photoshop 7 instead of Preview, type

$ open -a "/Applications/Adobe Photoshop 7/¬      Adobe Photoshop 7.0" ~/Desktop/Picture\ 1.png


A handy option, -f has open read text from its standard input, or a pipe, and open the text in TextEdit. To open this year's calendar using the cal command covered in Project 96, type

$ cal -y | open -f


Alternatively, specify an application other than TextEdit.

$ cal -y | open -a "/Applications/Microsoft Office X/¬     Microsoft Word" -f


Open URLs

Try the following commands, which demonstrate that open understands URLs.

$ open ftp://ftp.apple.com $ open http://www.apple.com


You can even connect to a server. To mount an AFP share called saruman from a host called carcharoth, for example, type

$ open afp://carcharoth/saruman





Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net