指定のRTF書類の内容を読み取って、書式情報をNSDictionaryに変換するAppleScriptです。
stringVal:文字列データ
colorStr:RGBの色データを文字列化したもの
colorVal:RGBの色データの数値list
colorDomainName:おおまかな色名称
fontName:フォント名
fontSize:フォントサイズ
書式情報で抽出しやすくしてあります。複数要素(例:フォント名+色)を一緒にまとめて格納しておくことで、検索パフォーマンスを稼げるはずです。
AppleScript名:RTFを読み込んで書式をDictionary化 v3.0 |
— Created 2017-12-10 by Takaaki Naganoya — Modified 2017-12-11 by Shane Stanley — Modified 2017-12-11 by Nigel Garvey — Modified 2017-12-12 by Takaaki Naganoya use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" property NSData : a reference to current application’s NSData property NSString : a reference to current application’s NSString property NSAttributedString : a reference to current application’s NSAttributedString set fRes to choose file of type {"public.rtf"} set aFilePath to NSString’s stringWithString:(POSIX path of fRes) set aData to NSData’s dataWithContentsOfFile:aFilePath options:0 |error|:(missing value) set theStyledText to NSAttributedString’s alloc()’s initWithData:aData options:(missing value) documentAttributes:(missing value) |error|:(missing value) set attrRes to getAttributeRunsFromAttrString(theStyledText) of me (* –> {{stringVal:"■■■■■■■", colorStr:"0 0 0", colorVal:{0, 0, 0}, colorDomainName:"black", fontName:"ヒラギノ角ゴ ProN W6", fontSize:12.0}, {stringVal:"Xxxxxxx", colorStr:"217 11 0", colorVal:{217, 11, 0}, colorDomainName:"red", fontName:"ヒラギノ角ゴ ProN W6", fontSize:12.0}, … *) on getAttributeRunsFromAttrString(theStyledText) 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()) * 255 set aGreen to (aColor’s greenComponent()) * 255 set aBlue to (aColor’s blueComponent()) * 255 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 domain name set cdnStr to retColorDomainNameFromNSColor(aColor) 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, colorDomainName:cdnStr, fontName:aDFontName as string, fontSize:aDFontSize} set startIndex to current application’s NSMaxRange(theRange) end repeat return (styleList of aSpd) end getAttributeRunsFromAttrString on retColorDomainNameFromNSColor(aCol) 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 retColorDomainNameFromNSColor |
More from my site
(Visited 59 times, 1 visits today)
NSRangeの拡張、隣接検出 - AppleScript Hole says:
[…] たとえば、NSAttributedStringに対して書式を取得する処理を行うと、3点リーダー文字(「…」)や感嘆符(「!」)の箇所で、書式情報が変わっていないのに分割して取得されてしまいま […]