TextEditで本文色をポスタライズ v1

TextEdit書類の本文色をポスタライズするAppleScriptです。

複数のユーザーでRTFに対して色をつけてチェックを行なったような場合に、指定色が微妙にゆらいでしまっても色のゆらぎを吸収するためのものです。

TextEditでオープン中の最前面のRTF書類中の文字色のゆらぎを吸収します。

オープンソースの「Colours」(By Ben Gordon)をフレームワーク化したcolorsKitを呼び出して、RGB→HSBA変換を行なっています。2D List中のリスト項目検索のためにShane StanleyのBridge Plus AppleScript Librariesを利用しています。

実行には、Bridge PlusおよびcolorsKit.frameworkのインストールを必要とします。colorsKit.frameworkについてはアーカイブをダウンロードして展開し、~/Library/Frameworksフォルダに入れてください(あくまで自己責任で)。

–> Download Framework Binary

te1png.png
▲処理前(緑色の文字の色にゆらぎがある)

t22.png
▲処理後(文字色が12色程度の原色に統一される)

AppleScript名:TextEditで本文色をポスタライズ v1
– Created 2018-01-08 by Takaaki Naganoya
– 2018 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “AppKit”
use framework “colorsKit” –https://github.com/bennyguitar/Colours
use bPlus : script “BridgePlus” –https://www.macosxautomation.com/applescript/apps/BridgePlus.html
–http://piyocast.com/as/archives/5097

property NSColor : a reference to current application’s NSColor
property NSArray : a reference to current application’s NSArray
property NSSortDescriptor : a reference to current application’s NSSortDescriptor

script spd
  property colList : {}
  
property attList : {}
end script

load framework

set cList to retTextEditColors() of me
set dList to {}

repeat with i in cList
  copy i to {rVal, gVal, bVal}
  
set newColList to posterizeColor(rVal, gVal, bVal, 65535) of me
  
repTextEditColor(i, newColList) of me
end repeat

on repTextEditColor(targColor, newColor)
  set hitIndex to {}
  
  
tell application “TextEdit”
    tell text of front document
      set colList to color of every attribute run
    end tell
  end tell
  
  
set hitIndex to (current application’s SMSForder’s indexesOfItem:targColor inArray:(colList) inverting:false) as list
  
  
tell application “TextEdit”
    tell text of front document
      repeat with i in hitIndex
        ignoring application responses
          set color of attribute run (i + 1) to newColor –0 based index to 1 based index conversion
        end ignoring
      end repeat
    end tell
  end tell
end repTextEditColor

on posterizeColor(rCol as integer, gCol as integer, bCol as integer, aColorMax)
  set aRes to makeNSColorFromRGBAval(rCol, gCol, bCol, aColorMax, aColorMax) of me
  
set aDic to aRes’s hsbaDictionary()
  
  
set hueVal to (|HSBA-h| of aDic) as real
  
set satVal to (|HSBA-s| of aDic) as real
  
set brightVal to (|HSBA-b| of aDic) as real
  
set alphaVal to (|HSBA-a| of aDic) as real
  
  
if satVal 0.01 then set satVal to 0.0
  
  
if satVal = 0.0 then
    if brightVal 0.2 then
      set colVal to {0, 0, 0} –Black
    else if (brightVal > 0.95) then
      set colVal to {65535, 65535, 65535} –White
    else
      set colVal to {32768, 32768, 32768} –Gray
    end if
  else
    if hueVal (15.0 / 360) or hueVal (330 / 360) then
      set colVal to {65535, 0, 0} –red
    else if hueVal (45.0 / 360) then
      set colVal to {65535, 32768, 0} –orange
    else if hueVal < (70.0 / 360) then
      set colVal to {65533, 63639, 2654} –yellow
    else if hueVal < (150.0 / 360) then
      set colVal to {4626, 35488, 17789} –green
    else if hueVal < (190.0 / 360) then
      set colVal to {0, 60802, 65535} –cyan, light blue
    else if (hueVal < 250.0 / 360.0) then
      set colVal to {0, 0, 65535} –blue
    else if (hueVal < 290.0 / 360.0) then
      set colVal to {32768, 0, 32768} –purple
    else
      set colVal to {65535, 0, 65535} –magenta, pink
    end if
  end if
  
  
return colVal
end posterizeColor

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 aCocoaList to {aRedCocoa, aGreenCocoa, aBlueCocoa, aAlphaCocoa}
  
set aColor to NSColor’s colorFromRGBAArray:aCocoaList
  
return aColor
end makeNSColorFromRGBAval

on retTextEditColors()
  tell application “TextEdit”
    set dCount to count every document
    
if dCount = 0 then return
    
tell text of front document
      set aList to color of every character
    end tell
  end tell
  
  
set ap1List to uniquify1DList(aList, true) of me
  
set cList to {}
  
repeat with i in ap1List
    set the end of cList to contents of i
  end repeat
  
  
return cList
end retTextEditColors

–1D Listをユニーク化
on uniquify1DList(theList as list, aBool as boolean)
  set aArray to NSArray’s arrayWithArray:theList
  
set bArray to aArray’s valueForKeyPath:“@distinctUnionOfObjects.self”
  
set bList to bArray as list
  
return bList
end uniquify1DList

★Click Here to Open This Script 

Leave a Reply