2008-07-23

もっとオープンに --- Open more...

Mac OS X には open というコマンドがあって、引数として渡されたファイル や URL を適切なアプリケーションで開いたり、-a オプションで直接アプリケーションを指定して開いたりすることができる。これは、コマンドラインでの作業が多いととても便利で、alias や関数、ルスクリプトなどに利用することでさらに有難かったりもする、利用頻度の高いコマンド。

つい最近、 -a オプションであるアプリケーションを起動しようとして、引数にアプリケーションを指定せずそのまま実行してしまったら、以下のようなヘルプメッセージが表示された。

[mbp15:~] rok% open -a
open: option requires an argument -- a
Usage: open [-e] [-t] [-f] [-W] [-n] [-g] [-h] [-b ] [-a ] [filenames]
Help: Open opens files from a shell.
By default, opens each file using the default application for that file.
If the file is in the form of a URL, the file will be opened as a URL.
Options:
-a Opens with the specified application.
-b Opens with the specified application bundle identifier.
-e Opens with TextEdit.
-t Opens with default text editor.
-f Reads input from standard input and opens with TextEdit.
-W, --wait-apps Blocks until the used applications are closed (even if they were already running).
-n, --new Open a new instance of the application even if one is already running.
-g, --background Does not bring the application to the foreground.
-h, --header Searches header file locations for headers matching the given filenames, and opens them.

誤ったオプションや、使い方を間違えたときにヘルプメッセージが表示されるようにするのはよくあることで、それ自体はなというほどのこともでないのだが、明かにオプションの数が増えている。

以前(不確かだけど Tiger 時代まで)は、アプリケーションを指定する「-a」、TextEdit.app で開くことを指定する「-e」、標準入力からの入力を利用するための「-f」くらいしかなかったものが、さらに多くのオプションが追加されたようだ。「-t」と「-e」なんて微妙な違いだし、「-b」ではアプリケーションバンドルの ID を使ってアプリケーションを開いたりするのも一部の人を除いては手軽とも言難いし、なんだか不思議な機能追加ではある。

ただ、プログラムを Xcode などを使わずに書いていると -h オプションはけっこう重宝しそうではある。たとえば、「open -e -h NSURLConnection」を実行すれば、NSURLConnection.h を TextEdit.app で開いてくれるし、「open -e -h NSURL」のように不完全なヘッダファイルの名前を指定すれば、候補となりそうなファイルをリストアップしてくれる。
[mbp15:~] rok% open -e -h NSURL
NSURL?
[0] cancel
[1] all

[2] /System/Library/Frameworks/Foundation.framework/Headers/NSURL.h
[3] /System/Library/Frameworks/Foundation.framework/Headers/NSURLAuthenticationChallenge.h
[4] /System/Library/Frameworks/Foundation.framework/Headers/NSURLCache.h
[5] /System/Library/Frameworks/Foundation.framework/Headers/NSURLConnection.h
[6] /System/Library/Frameworks/Foundation.framework/Headers/NSURLCredential.h
[7] /System/Library/Frameworks/Foundation.framework/Headers/NSURLCredentialStorage.h
[8] /System/Library/Frameworks/Foundation.framework/Headers/NSURLDownload.h
[9] /System/Library/Frameworks/Foundation.framework/Headers/NSURLError.h
[10] /System/Library/Frameworks/Foundation.framework/Headers/NSURLHandle.h
[11] /System/Library/Frameworks/Foundation.framework/Headers/NSURLProtectionSpace.h
[12] /System/Library/Frameworks/Foundation.framework/Headers/NSURLProtocol.h
[13] /System/Library/Frameworks/Foundation.framework/Headers/NSURLRequest.h
[14] /System/Library/Frameworks/Foundation.framework/Headers/NSURLResponse.h

Which header(s) for "NSURL"?

最後の「Which header(s) for "NSURL"?」というプロンプトに対して、対応する番号をタイプしてやるとそのファイルを開くことができる。Mac OS X のアプリケーションじゃなくて、less とか lv で開ければもっと便利になりそうなのが惜しいところ。

No comments: