Mac OS Xのアプリケーションの登録・検索方法

たつをさんのMac体験サイトStart MacBookが着々と更新されてます。第6回はアプリケーションのインストールについてで*1、ダウンロードしてから起動するまで、非常に丁寧に基本的なところを解説しているのですが、一部にマニアックな記述がありますね。こんなの:

ターミナルから「open -a Skype」を入力して起動することもできます。 この方法は、 /Applications/ 以下にあるアプリケーションに対して有効です。 デフォルトで入っているアプリも「open -a Chess」「open -a iPhoto」などと 一発起動できますよ。

ホームディレクトリの下にディレクトリApplicationsを作って(/User/ユーザ名/Applications, ~/Applications)、 そこに入れておいても「open -a XXX」で起動できます。 open は起動するアプリを /Applications と ~/Applications から探すという仕様みたいです。 (id:ogijun談)

http://start-macbook.net/06_install_applications/

ちうわけで名前が出てしまったので、このアプリケーションを検索するときの仕様を(だいたいこれでいいんだけどより細かいことを知りたい人のために念のため)補足しておきます。
実は /Applications, ~/Aplications の他に、ふつうの人はあんまり使わないと思うけど /Network, /Local 以下の Applications フォルダなども検索されます。さらに、その他の場所にあってもたいていは起動できるはず。これらMac OS Xのアプリケーション情報はLaunch Servicesというフレームワークによって管理されています(上のopenコマンドは、実際のところLaunch Servicesの単なるwrapperです)。ドキュメントによると:

All applications available on the user’s system must be registered to make them known to Launch Services and copy their document binding and other information into its database. It isn’t ordinarily necessary to perform this task explicitly, since a variety of utilities and services built into the Mac OS X system software take care of it automatically:

  • A built-in background tool, run whenever the system is booted or a new user logs in, automatically searches the Applications folders in the system, network, local, and user domains and registers any new applications it finds there. (This operation is analogous to “rebuilding the desktop” in earlier versions of Mac OS.)
  • The Finder automatically registers all applications as it becomes aware of them, such as when they are dragged onto the user’s disk or when the user navigates to a folder containing them.
http://developer.apple.com/documentation/Carbon/Conceptual/LaunchServicesConcepts/LSCConcepts/chapter_2_section_5.html

となっています。ログイン時に上述のフォルダをたどって登録したもの*2の他、Finderでドラッグ&ドロップしたときなどにもアプリケーション情報が登録されます。
Launch Servicesはアプリケーションのありかだけでなく、ファイル形式との関連付けなども管理していて、Finderでファイルをダブルクリックしたときなども同じ仕組みを使っています。↑のopenコマンドでも -a を付けないで open foo.bar のようにすると、そのファイルに関連したアプリケーションが起動します。関連付けの保存の仕方、複数あったときの選び方なども引用したドキュメントなどに記載されています。
こんな感じで、Mac OS XUnixをうまくwrapするためにすごい努力をしていて、長い時間をかけてだんだんと洗練されてきたので現状はさすがによく出来てるなーと思う。

*1:あれ? 第5回がない?

*2:このへんはUnixのshellのpath解決に似ていると思う。