TextEditでオープン中の最前面のドキュメント本文の色情報にアクセスして、「赤っぽい色」でマークされた行をカウントするAppleScriptです。
RGB色からHSB色に変換して、おおよその色の名前を計算することで、「赤っぽい色でマークされた行」を抽出してカウントできるようにしてみました。
テストのためにTextEditで処理してみましたが、PagesでもKeynoteでも、InDesignでもIllustratorでも同様のアプローチで色を数値から「だいたいの色」に抽象化することで、高度な判定処理を行うことが可能になります。
AppleScript名:TextEditの文章のうち赤っぽい色でマークされた行をカウントする v2 |
— Created 2018-01-08 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" property NSColor : a reference to current application’s NSColor set rNum to 0 set colMax to 65535 tell application "TextEdit" tell text of document 1 set pCount to (count paragraphs) repeat with i from 1 to pCount set {rCol, gCol, bCol} to color of paragraph i set cName to retColorDomainName(rCol, gCol, bCol, colMax) of me if cName = "red" then –{r, g, b} set rNum to rNum + 1 end if end repeat end tell end tell return {pCount, rNum} on retColorDomainName(rCol as integer, gCol as integer, bCol as integer, aColorMax) set aCol to makeNSColorFromRGBAval(rCol, gCol, bCol, aColorMax, aColorMax) of me set hueVal to aCol’s hueComponent() set satVal to aCol’s saturationComponent() set brightVal to aCol’s brightnessComponent() if satVal ≤ 0.01 then set satVal to 0.0 set colName to "" if satVal = 0.0 then if brightVal ≤ 0.2 then set colName to "black" else if (brightVal > 0.95) then set colName to "white" else set colName to "gray" end if else if hueVal ≤ (15.0 / 360) or hueVal ≥ (330 / 360) then set colName to "red" else if hueVal ≤ (45.0 / 360) then set colName to "orange" else if hueVal < (70.0 / 360) then set colName to "yellow" else if hueVal < (150.0 / 360) then set colName to "green" else if hueVal < (190.0 / 360) then set colName to "cyan" else if (hueVal < 250.0 / 360.0) then set colName to "blue" else if (hueVal < 290.0 / 360.0) then set colName to "purple" else set colName to "magenta" end if end if return colName end retColorDomainName on makeNSColorFromRGBAval(redValue as integer, greenValue as integer, blueValue as integer, alphaValue as integer, aMaxVal as integer) set aRedCocoa to (redValue / aMaxVal) as real set aGreenCocoa to (greenValue / aMaxVal) as real set aBlueCocoa to (blueValue / aMaxVal) as real set aAlphaCocoa to (alphaValue / aMaxVal) as real set aColor to NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa return aColor end makeNSColorFromRGBAval |
More from my site
(Visited 71 times, 1 visits today)
TextEditの文章のうち赤くマークされた行をカウントする – AppleScriptの穴 says:
[…] → TextEditの文章のうち赤っぽい色でマークされた行をカウントする v2 […]