3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

クリップボードの内容をプレーンテキストとHTMLに変換するAppleScript

Last updated at Posted at 2016-09-24

pasteboardAsString

pasteboardAsString.scpt
use framework "AppKit"
use framework "Foundation"

return my pasteboardAsString()

on pasteboardAsString()
	--require framework: AppKit, Foundation
	set pasteboard to current application's NSPasteboard's generalPasteboard
	return (pasteboard's stringForType:(current application's NSPasteboardTypeString)) as text
end pasteboardAsString

更新履歴

  • 2016-02-01: AppKitフレームワークのNSPasteboardクラスとNSAttributedStringクラスを使って作成
  • 2016-09-24: stringForTypeメソッドを使用

pasteboardAsHTML

pasteboardAsHTML.scpt
use framework "AppKit"
use framework "Foundation"

return my pasteboardAsHTML()

on pasteboardAsHTML()
	--require framework: AppKit, Foundation
	set pasteboard to current application's NSPasteboard's generalPasteboard
	set HTML to pasteboard's stringForType:(current application's NSPasteboardTypeHTML)
	if HTML = missing value then
		set RTF to pasteboard's dataForType:(current application's NSPasteboardTypeRTF)
		if RTF = missing value then
			error
		end if
		set attributedString to current application's NSAttributedString's alloc()'s initWithRTF:RTF documentAttributes:(missing value)
		set attributes to current application's NSDictionary's dictionaryWithObject:(current application's NSHTMLTextDocumentType) forKey:(current application's NSDocumentTypeDocumentAttribute)
		set HTMLData to attributedString's dataFromRange:{location:0, |length|:attributedString's |length|()} documentAttributes:attributes |error|:(missing value)
		set HTML to current application's NSString's alloc()'s initWithData:HTMLData encoding:(current application's NSUTF8StringEncoding)
	end if
	return HTML as text
end pasteboardAsHTML

更新履歴

  • 2016-02-01: AppKitフレームワークのNSPasteboardクラスとNSAttributedStringクラスを使って作成
  • 2016-09-24: stringForTypeメソッドとdataForTypeメソッドを使用
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?