書式付きテキスト(NSAttributedString)にパラグラフスタイルを指定して、tab指定を行い、tab展開を行うテストです。
Keynote/Pagesなどのテキストアイテムで、中にタブを含むスタイル付きテキストだった場合に、スタイル付きテキストの幅を計算したら大きな誤差が生じていたため、タブを展開して近似するサイズを取得できないかと考え、試してみたものです。
いまひとつ、まだしっくり来ていません。
AppleScript名:Attributed Stringにparagraph styleを指定してtab指定.scptd |
— – Created by: Takaaki Naganoya – Created on: 2021/12/27 — – Copyright © 2021 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use framework "AppKit" use scripting additions set aStr to "Piyomaru Software 100 200 10 20" set anAssrStr to makeRTFfromParameters(aStr, 3.0, 16, "HelveticaNeue") of me set aTabStops to {90, 400, 500, 600, 700} set aStyle to makeParagraphStyle(aTabStops) of me anAssrStr’s addAttribute:(current application’s NSParagraphStyleAttributeName) value:aStyle range:(current application’s NSMakeRange(0, (anAssrStr’s |length|()))) return anAssrStr –Make Paragraph Style on makeParagraphStyle(tList as list) set tabList to {} repeat with i in tList set the end of tabList to (current application’s NSTextTab’s alloc()’s initWithTextAlignment:(current application’s NSTextAlignmentLeft) location:i options:(missing value)) end repeat set aParagraphStyle to (current application’s NSParagraphStyle’s defaultParagraphStyle())’s mutableCopy() aParagraphStyle’s setTabStops:tabList return aParagraphStyle end makeParagraphStyle –Make Attributed String on makeRTFfromParameters(aStr as string, outlineNum as real, aFontSize as real, aFontName as string) set aVal1 to current application’s NSFont’s fontWithName:aFontName |size|:aFontSize set aKey1 to (current application’s NSFontAttributeName) set aVal2 to current application’s NSColor’s redColor() set aKey2 to (current application’s NSForegroundColorAttributeName) set aVal3 to 1.0 set akey3 to (current application’s NSKernAttributeName) set aVal4 to 0 set akey4 to (current application’s NSUnderlineStyleAttributeName) set keyList to {aKey1, aKey2, akey3, akey4} set valList to {aVal1, aVal2, aVal3, aVal4} set attrsDictionary to current application’s NSMutableDictionary’s dictionaryWithObjects:valList forKeys:keyList set attrStr to current application’s NSMutableAttributedString’s alloc()’s initWithString:aStr attributes:attrsDictionary return attrStr end makeRTFfromParameters |