« [lsof]使用中のファイルを調べる | トップページ | Comments to tab-delimited file.sequ »

[NSHTMLTextDocumentType]ペーストボードのリッチテキストデータをHTMLファイルに書き出す

HTMLタイプを考慮に入れた

【スクリプトエディタで開く】

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
#                       com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application

################################
##### パス関連
################################
set strFilePath to "~/Desktop/クリップボードのRTF書類のHTML.html" as text
set ocidFilePathStr to refMe's NSString's stringWithString:strFilePath
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false


################################
######ペーストボードを取得
################################
set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
####タイプを取得
set ocidPastBoardTypeArray to ocidPasteboard's types
log ocidPastBoardTypeArray as list


##### RTFな場合
set boolContain to ocidPastBoardTypeArray's containsObject:(refMe's NSPasteboardTypeRTF)
if boolContain is true then
  set ocidReadPasteboardData to ocidPasteboard's dataForType:(refMe's NSPasteboardTypeRTF)
  set ocidAttributedString to refMe's NSAttributedString's alloc()'s initWithRTF:ocidReadPasteboardData documentAttributes:(missing value)
else if boolContain is false then
  set boolContain to ocidPastBoardTypeArray's containsObject:(refMe's NSPasteboardTypeHTML)
  #####htmlな場合
  if boolContain is true then
    ###NSDataとしてHTMLデータを受け取る
    set ocidReadPasteboardData to ocidPasteboard's dataForType:(refMe's NSPasteboardTypeHTML)
    ###NSAttributedStringに変換
    set ocidAttributedString to refMe's NSAttributedString's alloc()'s initWithHTML:ocidReadPasteboardData documentAttributes:(missing value)
  else
    set boolContain to ocidPastBoardTypeArray's containsObject:(refMe's NSPasteboardTypeString)
    #####それ以外はプレーンテキスト
    if boolContain is true then
      set ocidPasteboardArray to ocidPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value)
      set ocidPasteboardStrings to (ocidPasteboardArray's objectAtIndex:0) as text
      set ocidAttributedString to refMe's NSAttributedString's alloc()'s initWithString:ocidPasteboardStrings attributes:(missing value)
    else
      return "テキストでの取得に失敗しました"
    end if
  end if
end if

################################
##### HTML変換
################################

###文字数カウント
set numStringLenge to ocidAttributedString's |length|() as integer
log numStringLenge
###アトリビュートをHTMLに変換
set ocidAttributedDict to refMe's NSDictionary's dictionaryWithObject:(refMe's NSHTMLTextDocumentType) forKey:(refMe's NSDocumentTypeDocumentAttribute)
####NSDATAに変換
set listDocAttar to ocidAttributedString's dataFromRange:{location:0, |length|:numStringLenge} documentAttributes:ocidAttributedDict |error|:(reference)
####取り出して
set ocidHTMLData to item 1 of listDocAttar
################################
##### 保存
################################
#####保存
set boolFileWrite to (ocidHTMLData's writeToURL:ocidFilePathURL atomically:true)



|

« [lsof]使用中のファイルを調べる | トップページ | Comments to tab-delimited file.sequ »

AppleScript NSPasteboard」カテゴリの記事