CotEditorでオープン中の複数のウィンドウから、各ウィンドウ(書類)の選択範囲を取得し、各ウィンドウの画面上の位置情報をもとに、その順番(Y座標、X座標)にテキストを組み合わせて新規書類を作成するAppleScriptです。
▲DEMO
▲Live Coding
当初、ウィンドウの重ね合わせ順(windowのindex)も考慮していたのですが、実際にためしてみたところ、重ね合わせ順を使うのは無駄だったので、機能を外しました。
AppleScript名:CotEditorの各Windowで選択しておいた内容を重ね合わせ順と座標でソートして結合.scpt |
— – Created by: Takaaki Naganoya – Created on: 2021/02/10 — – Copyright © 2021 Piyomaru Software, All Rights Reserved — use AppleScript version "2.5" use scripting additions use framework "Foundation" set wList to {} tell application "CotEditor" set wCount to count (every window whose visible is true) if wCount < 2 then return repeat with i from 1 to wCount tell window i set tmpBounds to bounds set tmpProp to properties end tell set tmpDoc to document of tmpProp tell tmpDoc set aCon to contents of selection end tell set tmpX to item 1 of tmpBounds set tmpY to item 2 of tmpBounds set tmpRec to {winIndex:i, winX:tmpX, winY:tmpY, selText:aCon} set the end of wList to tmpRec end repeat end tell –座標値をもとにソートして選択テキストを返す set sWList to sortRecListByLabel(wList, {"winY", "winX"}, {true, true}) of me set anArray to current application’s NSArray’s arrayWithArray:sWList set vList to (anArray’s valueForKey:"selText") as list set jointStr to retArrowText(vList, return) of me –CotEditor上で指定テキストでドキュメントを新規作成 makeNewCotEditorDoc(jointStr) of me –リストを指定デリミタを追加しつつテキスト化 on retArrowText(aList, aDelim) set aText to "" set curDelim to AppleScript’s text item delimiters set AppleScript’s text item delimiters to aDelim set aText to aList as text set AppleScript’s text item delimiters to curDelim return aText end retArrowText –リストに入れたレコードを、指定の属性ラベルの値でソート on sortRecListByLabel(aRecList as list, aLabelStr as list, ascendF as list) set aArray to current application’s NSArray’s arrayWithArray:aRecList set aCount to length of aLabelStr set sortDescArray to current application’s NSMutableArray’s new() repeat with i from 1 to aCount set aLabel to (item i of aLabelStr) set aKey to (item i of ascendF) set sortDesc to (current application’s NSSortDescriptor’s alloc()’s initWithKey:aLabel ascending:aKey) (sortDescArray’s addObject:sortDesc) end repeat return (aArray’s sortedArrayUsingDescriptors:sortDescArray) as list end sortRecListByLabel –指定テキストで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 |