Menu

Skip to content
AppleScriptの穴
  • Home
  • Products
  • Docs
  • Events
  • Forum
  • About This Blog
  • License
  • 仕事依頼

AppleScriptの穴

Useful & Practical AppleScript archive

タグ: OSANull

クリップボードに入っているテキストをAppleScriptとみなして、構文確認してスタイル付きテキストに変換する v2

Posted on 2月 8 by Takaaki Naganoya

クリップボードに入っているテキストを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

★Click Here to Open This Script 

Posted in Clipboard RTF | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy NSColor NSFont NSFontAttributeName NSForegroundColorAttributeName NSMutableAttributedString OSALanguage OSALanguageInstance OSANull OSAScript | Leave a comment

電子書籍をBOOTH.pmほかで販売中!!

Tags

10.11savvy (1052) 10.12savvy (1069) 10.13savvy (1049) 10.14savvy (94) CotEditor (30) Dictionary (9) Dock (7) Finder (28) ITLibrary (13) iTunes (21) Keynote (24) Mail (9) NSArray (25) NSBezierPath (7) NSBitmapImageRep (7) NSBundle (5) NSButton (8) NSColor (13) NSCountedSet (7) NSDictionary (11) NSFileManager (10) NSImage (17) NSMutableArray (15) NSMutableDictionary (7) NSPredicate (20) NSScreen (14) NSSortDescriptor (10) NSString (35) NSURL (23) NSUUID (7) NSView (6) NSWindow (9) NSWindowController (9) NSWorkspace (6) Numbers (17) OSALanguage (5) OSAScript (7) Pages (5) QuickTime Player (8) Safari (16) Script Editor (9) Sorting (5) System Events (8) Terminal (5) TextEdit (12)

カテゴリー

  • AirDrop
  • AirPlay
  • AppleScript Application on Xcode
  • Bluetooth
  • boolean
  • Calendar
  • Clipboard
  • Color
  • dialog
  • drive
  • exif
  • file
  • File path
  • filter
  • folder
  • Font
  • geolocation
  • GUI
  • GUI Scripting
  • Icon
  • Image
  • Input Method
  • Internet
  • Keychain
  • Language
  • list
  • Locale
  • Machine Learning
  • Markdown
  • Menu
  • MIDI
  • Natural Language Processing
  • Network
  • Noification
  • Number
  • OCR
  • OSA
  • PDF
  • QR Code
  • Record
  • regexp
  • Remote Control
  • Require Control-Command-R to run
  • REST API
  • RTF
  • Sandbox
  • Screen Saver
  • search
  • shell script
  • Sort
  • Sound
  • Spellchecker
  • Spotlight
  • System
  • Tag
  • Text
  • Text to Speech
  • timezone
  • URL
  • UTI
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年2月





メタ情報

  • 登録
  • ログイン
  • 投稿の RSS
  • コメントの RSS
  • WordPress.org
Proudly powered by WordPress
Theme: Flint by Star Verte LLC