指定のRTF書類の本文中の色を置換するAppleScriptです。
指定のRTF書類内のカラーをざっくりとした色に分類し、同じくざっくりとした色名で置換対象を指定し(blue, green)、指定色(black)に色置換。結果をデスクトップ上に別名で保存します。
AppleScript名:RTF本文内の色を置換 v2 |
— Created 2018-01-13 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" property NSData : a reference to current application’s NSData property NSUUID : a reference to current application’s NSUUID property NSColor : a reference to current application’s NSColor property NSString : a reference to current application’s NSString property NSPredicate : a reference to current application’s NSPredicate property NSDictionary : a reference to current application’s NSDictionary property NSMutableArray : a reference to current application’s NSMutableArray property NSMutableAttributedString : a reference to current application’s NSMutableAttributedString property NSForegroundColorAttributeName : a reference to current application’s NSForegroundColorAttributeName property NSDocumentTypeDocumentAttribute : a reference to current application’s NSDocumentTypeDocumentAttribute set targFilePath to POSIX path of (choose file of type {"public.rtf"}) set targColorNameList to {"blue", "green"} –replace target color names set toColor to NSColor’s blackColor() –to color set aRes to replaceRTFColorsByColorName(targFilePath, targColorNameList, toColor, 65535) of me –指定RTF書類本文中、名称で指定した色の該当箇所を指定色(NSColor)に置換する(複数色) on replaceRTFColorsByColorName(targFilePath as string, targColorNameList as list, toColor, aColorMax as integer) script spd property hitList : {} end script set (hitList of spd) to {} set aFilePath to NSString’s stringWithString:(targFilePath) set aData to NSData’s dataWithContentsOfFile:aFilePath options:0 |error|:(missing value) set theStyledText to NSMutableAttributedString’s alloc()’s initWithData:aData options:(missing value) documentAttributes:(missing value) |error|:(missing value) set attrList to getAttributeRunsFromAttrString(theStyledText, aColorMax) of me set attrArray to NSMutableArray’s arrayWithArray:attrList theStyledText’s beginEditing() —— repeat with ii in targColorNameList set jj to contents of ii set thePred to NSPredicate’s predicateWithFormat_("colorName == %@", jj) set (hitList of spd) to ((attrArray’s filteredArrayUsingPredicate:thePred)’s valueForKey:"rangeVal") as list repeat with i in (hitList of spd) (theStyledText’s addAttribute:(NSForegroundColorAttributeName) value:toColor range:(contents of i)) end repeat end repeat theStyledText’s endEditing() —— –Save RTF to desktop set targFol to current application’s NSHomeDirectory()’s stringByAppendingPathComponent:"Desktop" set aRes to saveStyledTextAsRTF(targFol, theStyledText) of me return aRes as boolean end replaceRTFColorsByColorName –AttributedStringを書式でlist of record化 on getAttributeRunsFromAttrString(theStyledText, aColorMax) script aSpd property styleList : {} end script set (styleList of aSpd) to {} —for output set thePureString to theStyledText’s |string|() –pure string from theStyledText set theLength to theStyledText’s |length|() set startIndex to 0 repeat until (startIndex = theLength) set {theAtts, theRange} to theStyledText’s attributesAtIndex:startIndex longestEffectiveRange:(reference) inRange:{startIndex, theLength – startIndex} –String set aText to (thePureString’s substringWithRange:theRange) as string –Color set aColor to (theAtts’s valueForKeyPath:"NSColor") if aColor is not equal to missing value then set aSpace to aColor’s colorSpace() set aRed to (aColor’s redComponent()) * aColorMax set aGreen to (aColor’s greenComponent()) * aColorMax set aBlue to (aColor’s blueComponent()) * aColorMax set colList to {aRed as integer, aGreen as integer, aBlue as integer} –for comparison set colStrForFind to (aRed as integer as string) & " " & (aGreen as integer as string) & " " & (aBlue as integer as string) –for filtering else set colList to {0, 0, 0} set colStrForFind to "0 0 0" end if –Color Name set cName to retColorName(aRed, aGreen, aBlue, aColorMax) of me –Font set aFont to (theAtts’s valueForKeyPath:"NSFont") if aFont is not equal to missing value then set aDFontName to aFont’s displayName() set aDFontSize to aFont’s pointSize() end if set the end of (styleList of aSpd) to {stringVal:aText, colorStr:colStrForFind, colorVal:colList, fontName:aDFontName as string, fontSize:aDFontSize, rangeVal:theRange, colorName:cName} set startIndex to current application’s NSMaxRange(theRange) end repeat return (styleList of aSpd) end getAttributeRunsFromAttrString –RGB値から色名称(だいたいの色)を計算する on retColorName(rCol as integer, gCol as integer, bCol as integer, aColMax as integer) set aCol to makeNSColorFromRGBAval(rCol, gCol, bCol, aColMax, aColMax) of me set hueVal to aCol’s hueComponent() as real set satVal to aCol’s saturationComponent() as real set brightVal to aCol’s brightnessComponent() as real if satVal ≤ 0.01 then set satVal to 0.0 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 retColorName 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 –スタイル付きテキストを指定フォルダ(POSIX path)にRTFで書き出し on saveStyledTextAsRTF(targFol, aStyledString) set bstyledLength to aStyledString’s |string|()’s |length|() set bDict to NSDictionary’s dictionaryWithObject:"NSRTFTextDocumentType" forKey:(NSDocumentTypeDocumentAttribute) set bRTF to aStyledString’s RTFFromRange:(current application’s NSMakeRange(0, bstyledLength)) documentAttributes:bDict set theName to (NSUUID’s UUID()’s UUIDString()) set thePath to NSString’s stringWithString:targFol set thePath to (thePath’s stringByAppendingPathComponent:theName)’s stringByAppendingPathExtension:"rtf" return (bRTF’s writeToFile:thePath atomically:true) as boolean end saveStyledTextAsRTF |
More from my site
(Visited 65 times, 1 visits today)