クリップボードに入っているテキストをAppleScriptとみなして構文確認を行い、AppleScriptの構文要素色分けを反映させたスタイル付きテキストを取得してクリップボードに入れるAppleScriptの改良版です。
初版があまり使い物にならなかったので、機能を追加してみました。クリップボード中の文字のサイズを取得しておき、クリップボード中のテキストをAppleScriptとして構文確認してスタイル付きテキストを取得したあとで、最初に取得した文字サイズに変更して、再度クリップボードに書き戻しています。
テキスト(おそらくAppleScriptのプログラムあるいはその一部)をコピーした状態で本Scriptを実行します。
スタイル付きテキスト(NSAttributedString/NSMutableAttributedString)のフォントサイズを変更するようなAPIが存在していなかったので、元のスタイル付きテキストを細切れに読み込んで、各種情報に分解。分解した情報をもとに(フォントサイズだけ変更して)同じものを再構成する、というやり方でスタイル付きテキストのフォントサイズ変更を行なっています。
AppleScript自体にはもともとスタイル付きテキストを操作する機能は皆無なので、他のアプリケーションの機能を利用するか本ScriptのようにCocoaの機能を利用することになります。他のアプリケーションを操作しなくてもスタイル付きテキストを操作できるようになってきたことは、非常に意義のあることです。
AppleScript名:クリップボードに入っているテキストをAppleScriptとみなして、構文確認してスタイル付きテキストに変換する v2 |
— Created 2017-04-24 by Shane Stanley — Modified 2019-02-05 by Takaaki Naganoya use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "AppKit" use framework "OSAKit" property NSFont : a reference to current application’s NSFont property NSColor : a reference to current application’s NSColor property OSANull : a reference to current application’s OSANull property OSAScript : a reference to current application’s OSAScript property OSALanguage : a reference to current application’s OSALanguage property NSFontAttributeName : a reference to current application’s NSFontAttributeName property OSALanguageInstance : a reference to current application’s OSALanguageInstance property NSMutableAttributedString : a reference to current application’s NSMutableAttributedString property NSForegroundColorAttributeName : a reference to current application’s NSForegroundColorAttributeName –クリップボードの内容をNSAttributedStringに set anAttr to my getClipboardASStyledText() if anAttr = missing value then return set maxSize to getAttributeStringFontSize(anAttr) of me set theSource to (anAttr’s |string|()) as string –OSデフォルトのOSA言語で構文確認してStyed Stringを取得する repeat 2 times set styledSourceText to compileTextAsAppleScriptStyledText(theSource) of me if styledSourceText is not equal to false then exit repeat set theSource to repChar(theSource, "“", "\"") of me set theSource to repChar(theSource, "”", "\"") of me end repeat if styledSourceText is equal to false then display dialog "Error: Non-AppleScript text" buttons {"OK"} default button 1 return end if –Make Font Size As Original Pasteboard set outStyledString to changeAttributeStringFontSize(styledSourceText, maxSize) of me — Set Styled AppleScript data to Clipboard set theArray to {outStyledString} restoreClipboard(theArray) of me –テキストをデフォルトOSA言語で構文確認してStyled Stringを取得する on compileTextAsAppleScriptStyledText(theSource) set anOSALanguageInstance to OSALanguageInstance’s languageInstanceWithLanguage:(OSALanguage’s defaultLanguage()) set theScript to OSAScript’s alloc()’s initWithSource:theSource fromURL:(missing value) languageInstance:anOSALanguageInstance usingStorageOptions:(OSANull) set {theResult, theError} to theScript’s compileAndReturnError:(reference) if theResult as boolean is false then return false else set sourceText to theScript’s source() return (theScript’s richTextSource()) end if end compileTextAsAppleScriptStyledText –Clipboardにデータを設定する on restoreClipboard(theArray as list) set thePasteboard to current application’s NSPasteboard’s generalPasteboard() thePasteboard’s clearContents() thePasteboard’s writeObjects:theArray end restoreClipboard –文字置換 on repChar(origText as string, targChar as string, repChar as string) set curDelim to AppleScript’s text item delimiters set AppleScript’s text item delimiters to targChar set tmpList to text items of origText set AppleScript’s text item delimiters to repChar set retText to tmpList as string set AppleScript’s text item delimiters to curDelim return retText end repChar — クリップボードの内容をNSAttributedStringとして取り出して返す on getClipboardASStyledText() try set theNSPasteboard to current application’s NSPasteboard’s generalPasteboard() set theAttributedStringNSArray to theNSPasteboard’s readObjectsForClasses:({current application’s NSAttributedString}) options:(missing value) set theNSAttributedString to theAttributedStringNSArray’s objectAtIndex:0 return theNSAttributedString on error return missing value end try end getClipboardASStyledText –Styled String中の最大のフォントサイズを取得 on getAttributeStringFontSize(theStyledText) set maxSize to 0 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} set aFont to (theAtts’s valueForKeyPath:"NSFont") –Font if aFont is not equal to missing value then set aDFontName to aFont’s displayName() set aDFontSize to (aFont’s pointSize()) as real end if if maxSize < aDFontSize then set maxSize to aDFontSize end if set startIndex to current application’s NSMaxRange(theRange) end repeat return maxSize end getAttributeStringFontSize –Styled Stringのフォントサイズを指定のサイズに変更 on changeAttributeStringFontSize(theStyledText, targFontSize as real) set outAttr to current application’s NSMutableAttributedString’s alloc()’s initWithString:"" 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} set aText to (thePureString’s substringWithRange:theRange) as string –String set aColor to (theAtts’s valueForKeyPath:"NSColor") –Color set aFont to (theAtts’s valueForKeyPath:"NSFont") –Font if aFont is not equal to missing value then set aDFontName to aFont’s displayName() set aDFontSize to aFont’s pointSize() end if set tmpAttrStr to changeAttrStrsFontAttribute(aText, aDFontName, targFontSize, aColor) of me outAttr’s appendAttributedString:tmpAttrStr set startIndex to current application’s NSMaxRange(theRange) end repeat return outAttr end changeAttributeStringFontSize –指定の文字列をAttributed Stringに変換して任意のフォントを一括指定 on changeAttrStrsFontAttribute(aStr, aFontPSName, aFontSize, aColor) set tmpAttr to NSMutableAttributedString’s alloc()’s initWithString:aStr set aRange to current application’s NSMakeRange(0, tmpAttr’s |length|()) set aVal1 to NSFont’s fontWithName:aFontPSName |size|:aFontSize tmpAttr’s beginEditing() tmpAttr’s addAttribute:(NSFontAttributeName) value:aVal1 range:aRange tmpAttr’s addAttribute:(NSForegroundColorAttributeName) value:aColor range:aRange tmpAttr’s endEditing() return tmpAttr end changeAttrStrsFontAttribute |