CotEditorで2つの書類をオープンしておき、行単位での差分を検出し、新規書類に結果を出力するAppleScriptです。
もともと、macOS搭載のFramework名一覧の差分を検出する資料を作成する必要があり、これを片付けるために作成しました。
本AppleScriptを実行すると……
のように、結果を出力します。それほど巨大なデータを想定して作ってはいないので、ほどほどの規模に抑えて利用してください。
AppleScript名:左右のドキュメントを比較.scpt |
use AppleScript version "2.4" use scripting additions use framework "Foundation" script spd property aList : {} property bList : {} end script set (aList of spd) to {} set (bList of spd) to {} tell application "CotEditor" set dCount to count every document if dCount is not equal to 2 then display notification "Error: This script needs only 2 documents" return –Document number Error end if tell window 1 set w1Bounds to bounds copy w1Bounds to {w1X1, w1Y1, w1X2, w1Y2} set w1Doc to document of it end tell tell window 2 set w2Bounds to bounds copy w2Bounds to {w2X1, w2Y1, w2X2, w2Y2} set w2Doc to document of it end tell if w1X1 > w2X1 then set {w1, w2} to {2, 1} else set {w1, w2} to {1, 2} end if tell document w1 –左ウィンドウ set (aList of spd) to paragraphs of contents end tell tell document w2 –右ウィンドウ set (bList of spd) to paragraphs of contents end tell end tell –ゴミ掃除 set (aList of spd) to cleanUp1DList((aList of spd), {" "}) of me set (bList of spd) to cleanUp1DList((bList of spd), {" "}) of me –差分計算 set (cList of spd) to getDiffBetweenLists((aList of spd), (bList of spd)) of me –結果出力テキスト組み立て set aRes to listToTextUsingDelim(addItems of (cList of spd), "") set bRes to listToTextUsingDelim(minusItems of (cList of spd), "") –結果の新規書類への出力 set d3 to makeNewCotEditorDoc("左→右で追加された項目" & return & return & aRes) of me set d4 to makeNewCotEditorDoc("左→右で削除された項目" & return & return & bRes) of me on getDiffBetweenLists(aArray as list, bArray as list) set allSet to current application’s NSMutableSet’s setWithArray:aArray allSet’s addObjectsFromArray:bArray –重複する要素のみ抜き出す set duplicateSet to current application’s NSMutableSet’s setWithArray:aArray duplicateSet’s intersectSet:(current application’s NSSet’s setWithArray:bArray) –重複部分を削除する allSet’s minusSet:duplicateSet set resArray to (allSet’s allObjects()) as list set aSet to current application’s NSMutableSet’s setWithArray:aArray set bSet to current application’s NSMutableSet’s setWithArray:resArray aSet’s intersectSet:bSet –積集合 set addRes to aSet’s allObjects() as list set cSet to current application’s NSMutableSet’s setWithArray:bArray cSet’s intersectSet:bSet –積集合 set minusRes to cSet’s allObjects() as list return {addItems:minusRes, minusItems:addRes} end getDiffBetweenLists on cleanUp1DList(aList as list, cleanUpItems as list) set bList to {} repeat with i in aList set j to contents of i if j is not in cleanUpItems then set the end of bList to j end if end repeat return bList end cleanUp1DList –デリミタ文字を指定してリストを文字列化 on listToTextUsingDelim(aList, aDelim) set curDelim to AppleScript’s text item delimiters set AppleScript’s text item delimiters to aDelim set outText to every text item of aList set outText to outText as string set AppleScript’s text item delimiters to curDelim return outText end listToTextUsingDelim –指定テキストでCotEditorの新規書類を作成 on makeNewCotEditorDoc(aCon) tell application "CotEditor" activate set newDoc to make new document tell newDoc set contents to aCon end tell end tell end makeNewCotEditorDoc |
More from my site
(Visited 166 times, 1 visits today)