Mac OS Xがファイルとアプリケーションをどう関連づけているかに関しての備忘録。
普通に開く
この場合、Finderからダブルクリックすればよいのは、我が家の娘達も三歳になる前から知っている。Windowsでも同様なのはご存じのとおり。
アプリケーションを指定して開く
この場合、使いたいアプケーションのアイコンにドラッグ&ドロップするか、ctrl-クリックして[Open With...]メニューを出せばよい。マウスボタンが複数ある場合は、右クリックでもOK。これまたWindowsと同じ。
openコマンド
Mac OS X ならでは、というと、openというコマンドがある。これを使うと、shellからFinderと同じことができる。
% open Usage: open [-e] [-t] [-f] [-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.
面白いのは、-fというオプション。これを使うと標準入力の結果をGUIアプリで開いて読める。TextEditがデフォルトだが、-aオプションで任意のアプケーションを指定できるのは他と同様。
デフォルトのアプリケーションを変更する
Finderから[Get Info]でそのファイルに関する情報を出しからアプリケーションを選択し、[Change All..]ボタンを押せばOK。
その仕組み
かつてのMac OSでは、各ファイルにTypeとCreatorという、それぞれ4 byteのメタデータが付属していた。このTypeとCreatorがファイルとアプリケーションの関連づけをやっていたのだが、OS Xになってからはファイルにそういうメタデータがあるとは限らなくなってきた。Windowsのように拡張子もその判断に使われるようになったのだが、その仕組みはどのようになっているだろうか。
実はOS Xでも、この仕組みはある意味継承されている。各アプリケーションは、Info.plistというファイルを内包していて、その中のCFBundleDocumentTypesどんなファイルに対してそのアプリケーションが何ができるのかが記載されている。記事の最後にFirefoxのそれを添付しておいたで参考にしてほしい。
Finder、正確には Launch Service はこの情報を元に、適切なアプリケーションを判定してそのアプリケーションにファイルを渡すのであるが、先ほどの「デフォルトのアプリケーションを変更」した場合、その情報はどこに保管されているのだろうか。
~/Library/Preferences/com.apple.LaunchServices.plistである。これ、Tiger現在ではバイナリーファイルになっているが、以下のようにしてXMLに変換することが出来る。
XCodeをインストールしてあれば、Property List Editorというアプリケーションが付属しているので、それで直に開くことも出来る。
% cp ~/Library/Preferences/com.apple.LaunchServices.plist tmp.plist % plutil -convert xml1 tmp.plist
ちなみに、XMLのものをバイナリーファイルに戻すには、
% plutil -convert binary1 tmp.plist
でOK。
yamaq blog ? Blog Archive - plistファイルの変換このTIPSは、見るだけで編集して再度バイナリーに戻すことはできません。あくまでも内容の確認が目的です。
とありますが、上記の方法でbinaryに戻す事もできます。さらに補足すると、この.plistはbinaryでもXMLでも、API経由で使えばどちらも同じように使える模様です。もっとも私はそれを保障しませんが。At your own riskということで。
書式は、先ほどのInfo.plistとほぼ同じ。これを手で編集しても、同様の効果が得られる。いろいろ使っているうちにファイルとアプリケーションの対応付けがおかしくなってきたと感じたら、このファイルを確認してみよう。
まとめ
これを書いたきっかけは、Smultronを入れた途端に今まで白紙アイコンだったものがみんないちごのアイコンになってちょっとびびったこと。Smultronはいいエディタだが、ボタンやアイコンがちょっとポップすぎる。
ちょっとびびったものの、このあたりの仕組みを知っておけば「アイコン化け」も恐れる必要はない。そしてありがたいことに、それほど難しい仕組みではなく、昔のようにResEditでむりやりアプリケーションを開くという作業も必要ないし、「デスクトップの再構築」を行う必要もない。こういう「エンジンフードの下」に関しては、Mac OS XとMac OSの違いは天と地ほどだ。
Dan the File Opener
/Applications/Firefox.app/Contents/Info.plist<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>CFBundleDevelopmentRegion</key> <string>English</string> <key>CFBundleDocumentTypes</key> <array> <dict> <key>CFBundleTypeExtensions</key> <array> <string>html</string> <string>htm</string> <string>shtml</string> <string>xht</string> <string>xhtml</string> </array> <key>CFBundleTypeIconFile</key> <string>document.icns</string> <key>CFBundleTypeName</key> <string>HTML Document</string> <key>CFBundleTypeOSTypes</key> <array> <string>HTML</string> </array> <key>CFBundleTypeRole</key> <string>Viewer</string> </dict> <dict> <key>CFBundleTypeExtensions</key> <array> <string>text</string> <string>txt</string> <string>js</string> <string>log</string> <string>css</string> <string>xul</string> <string>rdf</string> </array> <key>CFBundleTypeIconFile</key> <string>document.icns</string> <key>CFBundleTypeName</key> <string>Text Document</string> <key>CFBundleTypeOSTypes</key> <array> <string>TEXT</string> <string>utxt</string> </array> <key>CFBundleTypeRole</key> <string>Viewer</string> </dict> <dict> <key>CFBundleTypeExtensions</key> <array> <string>jpeg</string> <string>jpg</string> <string>png</string> <string>gif</string> </array> <key>CFBundleTypeIconFile</key> <string>fileBookmark.icns</string> <key>CFBundleTypeName</key> <string>document.icns</string> <key>CFBundleTypeOSTypes</key> <array> <string>GIFf</string> <string>JPEG</string> <string>PNGf</string> </array> <key>CFBundleTypeRole</key> <string>Viewer</string> </dict> </array> <key>CFBundleExecutable</key> <string>firefox-bin</string> <key>CFBundleGetInfoString</key> <string>Firefox 2.0.0.3, © 1998-2007 Contributors</string> <key>CFBundleIconFile</key> <string>firefox</string> <key>CFBundleIdentifier</key> <string>org.mozilla.firefox</string> <key>CFBundleInfoDictionaryVersion</key> <string>6.0</string> <key>CFBundleName</key> <string>Firefox</string> <key>CFBundlePackageType</key> <string>APPL</string> <key>CFBundleShortVersionString</key> <string>2.0.0.3</string> <key>CFBundleSignature</key> <string>MOZB</string> <key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLIconFile</key> <string>document.icns</string> <key>CFBundleURLName</key> <string>http URL</string> <key>CFBundleURLSchemes</key> <array> <string>http</string> </array> </dict> <dict> <key>CFBundleURLIconFile</key> <string>document.icns</string> <key>CFBundleURLName</key> <string>https URL</string> <key>CFBundleURLSchemes</key> <array> <string>https</string> </array> </dict> <dict> <key>CFBundleURLName</key> <string>ftp URL</string> <key>CFBundleURLSchemes</key> <array> <string>ftp</string> </array> </dict> <dict> <key>CFBundleURLName</key> <string>gopher URL</string> <key>CFBundleURLSchemes</key> <array> <string>gopher</string> </array> </dict> <dict> <key>CFBundleURLName</key> <string>file URL</string> <key>CFBundleURLSchemes</key> <array> <string>file</string> </array> </dict> </array> <key>CFBundleVersion</key> <string>2.0.0.3</string> <key>NSAppleScriptEnabled</key> <true/> </dict> </plist>
>いろいろ使っているうちにファイルとアプリケーションの対応付けがおかしくなってきたと感じたら、
>このファイルを確認してみよう。
その手のファイルを開くことすら怖い私は、こんなソフト(system preferences)を使ってます。
使いやすいですよ〜!
Rubicode - RCDefaultApp
http://www.rubicode.com/Software/RCDefaultApp/