Help us understand the problem. What is going on with this article?

Cocoaの機能を使ってHTML文字列をコピーするAppleScriptハンドラ

More than 3 years have passed since last update.
  • HTML文字列をクリップボードにコピー
  • リッチテキストエディタにはスタイル付きでペーストできる
copyHTML.scpt
use scripting additions
use framework "AppKit"
use framework "Foundation"

my copyHTML("<p>あいうえお</p>")

the clipboard as record
(*
{«class HTML»:«data HTML3C703EE38182E38184E38186E38188E3818A3C2F703E», «class utf8»:"あいうえお
", «class RTF »:«data RTF 7B5C727466315C616E73695C616E73696370673933325C636F636F61727466313430345C636F636F617375627274663437300A7B5C666F6E7474626C5C66305C66726F6D616E5C6663686172736574302054696D65732D526F6D616E3B7D0A7B5C636F6C6F7274626C3B5C7265643235355C677265656E3235355C626C75653235353B5C726564305C677265656E305C626C7565303B7D0A5C6465667461623732300A5C706172645C7061726465667461623732305C736C3238305C73613234305C7061727469676874656E666163746F72300A0A5C66305C66733234205C636632205C6578706E64305C6578706E647477305C6B65726E696E67300A5C6F75746C305C7374726F6B65776964746830205C7374726F6B656332205C7563305C753132333534205C753132333536205C753132333538205C753132333630205C753132333632205C0A7D», «class ut16»:"あいうえお
", uniform styles:«data ustl0200000090000000000000001400000020000000010000000600000000000000010000006C000000040000000000000000000000020100000100000000000000050100002C000000646D616E2400000001000000040000000100000000000000000000000B00000054696D657320526F6D616E00060100000400000000000C0007010000060000000000000000000000», string:"あいうえお
", scrap styles:«data styl02000000000012000D00A1E900950C000000000000000A0000000E001100140000950C00000000000000», Unicode text:"あいうえお
"}
*)

on copyHTML(HTML)
    --require framework: AppKit, Foundation
    set pasteboard to current application's NSPasteboard's generalPasteboard()
    pasteboard's clearContents()
    pasteboard's declareTypes:{current application's NSPasteboardTypeHTML, current application's NSPasteboardTypeString, current application's NSPasteboardTypeRTF} owner:me
    pasteboard's setString:HTML forType:(current application's NSPasteboardTypeHTML)
    set attributedString to my attributedStringOfHTMLText(HTML)
    pasteboard's setString:(attributedString's |string|()) forType:(current application's NSPasteboardTypeString)
    set attributes to current application's NSDictionary's dictionaryWithObject:(current application's NSHTMLTextDocumentType) forKey:(current application's NSDocumentTypeDocumentAttribute)
    set HTMLData to attributedString's RTFFromRange:{location:0, |length|:attributedString's |length|()} documentAttributes:attributes
    pasteboard's setData:HTMLData forType:(current application's NSPasteboardTypeRTF)
end copyHTML

on attributedStringOfHTMLText(HTML as text)
    --require framework: AppKit, Foundation
    set HTMLString to current application's NSString's stringWithString:HTML
    set HTMLData to HTMLString's dataUsingEncoding:(current application's NSUnicodeStringEncoding)
    return current application's NSAttributedString's alloc()'s initWithHTML:HTMLData documentAttributes:(missing value)
end attributedStringOfHTMLText

更新履歴

  • 2016-02-01: AppKitフレームワークのNSAttributedStringクラスとNSPasteboardクラスを使って作成
  • 2016-09-24: declareTypes:owner:メソッドを使ってHTML、String、RTFの3種類でコピー
szk-3
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away