(* diff コマンドラインツールを用いた mi (OSX) 用ファイル比較ツール (AppleScript スクリプト:利用するにはスクリプトエディタで、スクリプトとして、 mi の tool フォルダ内に保存。) copyright (c) 2005 HIROSE, Yukio (http://hcn.zaq.ne.jp/___/) 公開日 2005-08-18 利用は自己責任で。複製/配布/改変は自由に行ってよろしい。 ただし、改変したものを配布する場合は、その事実を配布物に明記すること。 *) -- 作成日が同じファイルを自動的に比較対象にする場合は下を true に、常にファイルダイアログから選ぶ場合は下を fasle にする。 property auto_select_target : true -- diff コマンド文字列。追加で与えたいオプションがあれば diff に続けて指定する property diff_command : "diff " as Unicode text -- オプションの詳細は diff のマニュアルを参照のこと:http://www.linux.or.jp/JM/html/gnumaniak/man1/diff.1.html property msg_choose_file1 : "比較対象を選択" property msg_choose_file2 : "比較対象(同一作成日の文書)が見つかりません。選択して下さい。" property msg_choose_file3 : "同一作成日のファイルが複数あります。比較対象を選んで下さい。" property msg_not_saved : "実行前に文書を保存する必要があります。" property msg_unsupported_format : "文書のエンコーディングを UTF-8 に、改行コードを LF にして*保存する*必要があります(比較対象も)。" property msg_same_file : "選択されたファイルは編集中の文書と同じファイルです。" on run local _file, _file1, _headerstr, _commandstr, _diff, _LF set _LF to ASCII character 10 tell application "mi" tell document 1 set _file to file try set _file1 to _file on error my showNote(my msg_not_saved) end try -- UTF-8、 LF 限定。はっきり言うと手抜きです(^^; -- ちゃんとやると一時ファイルを作成したりと、結構面倒。 if (character code is not UTF8 or return code is not LF) then my showNote(my msg_unsupported_format) end tell end tell set _file1 to my getTargetFile(_file) set _file to POSIX path of _file set _file1 to POSIX path of _file1 if (_file is _file1) then showNote(my msg_same_file) set _headerstr to "diff from file: " & _file1 & _LF & "to current file: " & _file & _LF & "------ compare result: -------" & _LF as Unicode text set _commandstr to my diff_command & " " & quoted form of _file1 & " " & quoted form of _file & " 2>&1" try set _diff to (do shell script _commandstr) on error msg number x set _diff to msg as Unicode text end try tell application "mi" make new document with properties {name:"diff result" as Unicode text, character code:UTF8} set selection of document 1 to _headerstr & _diff end tell end run on showNote(msg) display dialog msg buttons {"OK"} default button 1 with icon note error number -128 end showNote on map(value, list1, list2) repeat with i from 1 to length of list1 if value is item i of list1 then return item i of list2 end repeat end map --比較対象ファイルを取得するハンドラ on getTargetFile(_file) local _date, _folder, _list, _size, _type if (not my auto_select_target) then return choose file with prompt my msg_choose_file1 default location _file end if -- auto_select_target が true なら、同じフォルダ内の同一作成日の文書を自動的に取得する tell application "Finder" set _date to creation date of _file set _name to name of _file set _folder to container of _file set _list to every document file of _folder where creation date of it is _date and name of it is not _name if (length of _list is 1) then return (item i of _list) as alias if length of _list is 0 then tell me to return choose file with prompt my msg_choose_file1 default location _file end if repeat with f in _list set contents of f to name of f end repeat end tell try --複数あった場合はユーザに問い合わせる set _name to item 1 of (choose from list _list with prompt my msg_choose_file3) on error error number -128 end try tell application "Finder" to return (item _name of _folder) as alias end getTargetFile