スタイル付きテキストの中に画像を入れるAppleScriptです。
–> Download Script bundle with Library
▲上左:macOS 10.13、上右:macOS 10.14、下左:macOS 10.15
画像をNSTextAttachmentに入れてNSAttributedStringに追加するのが本来の処理方法ですが、macOS 10.13と10.14ではNSTextAttachment中の画像が上下逆になるというバグを見つけてしまいました(10.15以降では修正されています)。
ただし、これは全言語共通のバグなので回避方法が紹介されていました。NSTextAttachmentCell経由で画像を突っ込む方法が紹介されています。
これにもとづいて修正+Shane Stanleyからのアドバイスで書き換えたのがこのScriptです。
AppleScript名:インライン画像つきスタイル付きテキストの作成 v1b.scptd |
— – Created by: Takaaki Naganoya – Created on: 2020/07/22 — – Copyright © 2020 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use framework "AppKit" use scripting additions use tvLib : script "tvLib" set anImage to (current application’s NSImage’s imageNamed:(current application’s NSImageNameComputer)) set anAttachment to current application’s NSTextAttachment’s alloc()’s init() anAttachment’s attachmentCell()’s setImage:anImage anAttachment’s setBounds:(current application’s NSMakeRect(0, 0, 32, 32)) set img1Str to current application’s NSAttributedString’s attributedStringWithAttachment:anAttachment set anAssrStr to makeRTFfromParameters("Piyomaru Software", 32, "Times") of me img1Str’s appendAttributedString:anAssrStr dispAttrStr("Main message", "Sub message", img1Str, 400, 80) of tvLib on makeRTFfromParameters(aStr as string, 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 blackColor() set aKey2 to (current application’s NSForegroundColorAttributeName) set aVal3 to 0 set akey3 to (current application’s NSKernAttributeName) set keyList to {aKey1, aKey2, akey3} set valList to {aVal1, aVal2, aVal3} 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 |
More from my site
(Visited 80 times, 1 visits today)