TextEditの最前面の書類本文のうち、指定の色名称の色(red)に分類できる文字を出力するAppleScriptです。
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 67 times, 1 visits today)
TextEditで本文中の使用色をリストアップ(色判定つき)v3 – AppleScript Hole says:
[…] 色が検出されるケースもあります。そのような場合には、TextEditの文章のうち色っぽい色でマークされた箇所をピックアップする v2を使って指定色に該当する箇所の文字データを抽出して […]
TextEditの文章のうち赤くマークされた行をカウントする – AppleScriptの穴 says:
[…] → TextEditの文章のうち赤っぽい色でマークされた箇所をピックアップする v2 […]