他のUnix系システムやらLinuxですと、文字コードを変換できるnkfなど、なにがしか日本語のテキストをいじる上でも役に立つものが入っているものなのですが、Mac OS Xはそのあたりはダメダメでしたね。しかし、10.4からは状況が変わってきたようで、textutilという(おそらくAppleオリジナルの)ツールが加わっています。Appleはどうも単機能ユーティリティーよりも統合ユーティリティーが好きなようで、textutilもかなり高機能です。詳細はtextutilのhelpを見ていただくことにして、普通に生活していて使えそうな機能だけを紹介します(普通に生活していたらターミナル使わないだろ、という至極真っ当な指摘は却下します)。
今回は実験用に以下の4つのファイルを用意しました。
PowerBookG4:~ hiro$ cd terminaltest/ PowerBookG4:~/terminaltest hiro$ ls test1.rtf test2.rtf test3.txt test4.txt
| test1.rtf | Rich Textファイル。日本語。 |
|---|---|
| test2.rtf | Rich Textファイル。日本語。 |
| test3.txt | Textファイル。英語。 |
| test4.txt | Textファイル。日英混在。 |
まずはtextutilでどのようなことができるのか表示してみましょう。textutilのhelpは「textutil -help」で表示できます。
PowerBookG4:~ hiro$ textutil -help
textutil: [command_option] [other_options] file...
Command options are (-help is the default):
-help show this message and exit
-info display information about each file
-convert fmt convert each input file to format (one of
txt, html, rtf, rtfd, doc, wordml, or webarchive)
-cat fmt concatenate input files into one output file
(長いので中略)
-comment val comment metadata attribute for output files
-editor val last editor metadata attribute for output files
-company val company metadata attribute for output files
-creationtime yyyy-mm-ddThh:mm:ssZ
creation time metadata attribute for output files
-modificationtime yyyy-mm-ddThh:mm:ssZ
modification time metadata attribute for output files
フォーマットの変換からメタデータの設定まで、とにかく多彩ですね。
まずはファイルの情報を見てみましょう。この場合、「textutil -info ファイル名」とします。
PowerBookG4:~/terminaltest hiro$ textutil -info test1.rtf File: test1.rtf Type: rich text format (RTF) Size: 588 bytes Length: 18 characters Contents: あいうえお...
サイズや文字数、内容の先頭が表示されますね。しかも日本語もちゃんと通ります。
次に、ファイルのフォーマットを変換してみましょう。この場合、「textutil -convert 変換後のフォーマット ファイル名」とします。フォーマットには、txt、rtf、htmlという標準的なものから、docなんて色物まであるところがすてきです。まずは、普通にテキストファイルにしてみましょう。フォーマットはtxtで指定します。
PowerBookG4:~/terminaltest hiro$ textutil -convert txt test1.rtf PowerBookG4:~/terminaltest hiro$ ls test1.rtf test1.txt test2.rtf test3.txt test4.txt PowerBookG4:~/terminaltest hiro$ cat test1.txt あいうえお かきくけこ さしすせそ
きちっと拡張子の前を流用して.txtのファイルを作ってますね。内容も問題なさそうです。
ついでにdocにもしてみましょう。この場合、フォーマットにはdocを指定します。
PowerBookG4:~/terminaltest hiro$ textutil -convert doc test1.rtf
できあがったのはこんな感じ(test1.doc)です。微軟社の単語で開いてみると、色の指定やサイズの指定は合っているのですが、フォントの指定がアウトです(筆者の環境ではなぜか全てDFP勘亭流で表示されてしまいます)。
textとhtmlにする場合は、文字コードを合わせて指定することもできます。この場合「-encoding 文字コード」で指定します。初期設定ではUTF-8に変換されますので、それ以外の文字コードにしておく必要がある場合にはこのオプションを使ってください。文字コードはIANAの文字コードの規格名か、NSStringEncoding型のCocoaの文字コード名を使います。試しに、test1.rtfをShift_JISのhtmlにしてみましょう。
PowerBookG4:~/terminaltest hiro$ textutil -convert html -encoding Shift_JIS test1.rtf PowerBookG4:~/terminaltest hiro$ ls test1.doc test1.rtf test2.rtf test4.txt test1.html test1.txt test3.txt
できあがったhtml(test1.html)は見事にフォントやサイズ、色の指定が生きていますし、文字コードもShift_JIS、<head>の中にある<meta>でも正しくShift_JISが指定されています。なかなか優秀です。
もちろん、変換は相互に可能で、docをtxtにするといった変換もできます(この場合、書式の設定などはもちろん落ちますが)。
PowerBookG4:~/terminaltest hiro$ rm test1.txt PowerBookG4:~/terminaltest hiro$ ls test1.doc test1.rtf test3.txt test1.html test2.rtf test4.txt PowerBookG4:~/terminaltest hiro$ textutil -convert txt test1.doc PowerBookG4:~/terminaltest hiro$ ls test1.doc test1.rtf test2.rtf test4.txt test1.html test1.txt test3.txt
ファイルの結合も可能です。catと同じ機能なので、オプションも「cat」で、書式は「textutil -cat フォーマット 結合したいファイル名」とします。できあがるファイルの名前は「out.フォーマットに応じた拡張子」となります。
PowerBookG4:~/terminaltest hiro$ textutil -cat html test1.rtf test2.rtf PowerBookG4:~/terminaltest hiro$ ls out.html test1.html test1.txt test3.txt test1.doc test1.rtf test2.rtf test4.txt
結合したファイル(out.html)は内容がつながっています。ただし、前のファイルの最後が改行ではなく普通の文字の場合、前のファイルの最後の行と次のファイルの最初の行がつながってしまうので注意が必要です。
生成するファイルの名前を変える場合は、>を使ってリダイレクトするのではなく、専用のオプションで指定します。「-output ファイル名」としますが、この時、ファイル名は拡張子を含めて指定する必要があります。-catの結果がout.htmlではつまらないので、それらしくcatfile.htmlにしてみましょう。
PowerBookG4:~/terminaltest hiro$ textutil -cat html -output catfile.html test1.rtf test2.rtf PowerBookG4:~/terminaltest hiro$ ls catfile.html test1.doc test1.rtf test2.rtf test4.txt out.html test1.html test1.txt test3.txt
今度はcatfile.htmlという分かりやすい名前になりましたね。
なかなか便利なtextutilですが、ちょっと困った問題もあります。日本語を含むtextファイルの扱いにやや難ありです。
PowerBookG4:~/terminaltest hiro$ textutil -info test3.txt File: test3.txt Type: plain text Size: 16 bytes Length: 16 characters Contents: Apple... PowerBookG4:~/terminaltest hiro$ textutil -info test1.html File: out.html Type: HTML Size: 1140 bytes Length: 36 characters Contents: あいうえお... PowerBookG4:~/terminaltest hiro$ textutil -info test4.txt Error reading test4.txt
このように、アルファベットだけのtest3.txtや、日本語のtest1.htmlは正常に扱えるのですが、日本語を含むテキストファイルtest4.txtを扱おうとするとこのようにエラーが発生してしまいます。
フォーマットや文字コードの変換は普通ならテキストエディタやワープロソフトを使うと良いのですが、ファイル数が多い場合にはtextutilとforeachを使ってまとめてやってしまうと良いでしょう。それから、シェルスクリプトなどでdocやhtmlからテキストを取り出す必要がある場合などに使えるかと思います。