« [exiftool]メタ情報をHTMLで表示(だいぶ改修した) | トップページ | システム設定オープン用のスクリプト作成補助 v5 (アンカーのないパネルに対応した) »

[NSXML]HTML生成基本部分(更新)


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
# HTML生成基本部分
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use framework "UniformTypeIdentifiers"
use scripting additions

property refMe : a reference to current application



set appFileManager to refMe's NSFileManager's defaultManager()

####出力先/var/folders/XX/(再起動時に削除されます)
set ocidTemporaryDirPathURL to appFileManager's temporaryDirectory
set ocidVarFoldersDirURL to ocidTemporaryDirPathURL's URLByDeletingLastPathComponent()
set ocidTemporaryItemsPathURL to ocidVarFoldersDirURL's URLByAppendingPathComponent:"TemporaryItems"
#set aliasTemporaryItems to (ocidTemporaryItemsPathURL's absoluteURL()) as alias
set strTempDirPath to ocidTemporaryItemsPathURL's absoluteString() as text

##フォルダがなければ作る
set boolDirExists to appFileManager's fileExistsAtPath:(ocidTemporaryItemsPathURL's |path|()) isDirectory:(true)
if boolDirExists = true then
log "起動時に削除されるフォルダはすでにあります"
else if boolDirExists = false then
log "起動時に削除されるフォルダを作ります"
  set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
  set listDone to appFileManager's createDirectoryAtURL:(ocidTemporaryItemsPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference)
end if
###保存HTMLのパス
set ocidBaseFilePathURL to (ocidTemporaryItemsPathURL's URLByAppendingPathComponent:("サンプルファイル"))
set ocidSaveFilePathURL to (ocidBaseFilePathURL's URLByAppendingPathExtension:("html"))




##############################
# XML 生成開始
##############################
# タイトル
set strTitleText to ("HTMLのタイトルをここに") as text
#サブタイトル
set strSubTitle to ("サブタイトルをここに") as text

########################################
#headerに渡すエレメント
set ocidSetHeaderElement to (refMe's NSXMLElement's elementWithName:("div"))
set ocidH3Element to (refMe's NSXMLElement's elementWithName:("h3"))
##タイトルを入れる
(ocidH3Element's setStringValue:(strTitleText))
(ocidSetHeaderElement's addChild:(ocidH3Element))

########################################
#footerに渡すエレメント
set ocidSetFooterElement to (refMe's NSXMLElement's elementWithName:("div"))
#リンクをつける
set ocidAElement to (refMe's NSXMLElement's elementWithName:("a"))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:("https://quicktimer.cocolog-nifty.com/"))
(ocidAElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank"))
(ocidAElement's addAttribute:(ocidAddNode))
set strContents to ("AppleScriptで生成しました") as text
(ocidAElement's setStringValue:(strContents))
(ocidSetFooterElement's addChild:(ocidAElement))

########################################
#articleに渡すエレメント
set ocidSetArticleElement to (refMe's NSXMLElement's elementWithName:("div"))
set ocidH3Element to (refMe's NSXMLElement's elementWithName:("h5"))
(ocidH3Element's setStringValue:(strSubTitle))
(ocidSetArticleElement's addChild:(ocidH3Element))
########################################
#テーブル部生成開始
set ocidTableElement to (refMe's NSXMLElement's elementWithName:("table"))
#########【caption】
set ocidCaptionElement to (refMe's NSXMLElement's elementWithName:("caption"))
(ocidCaptionElement's setStringValue:("キャプション"))
(ocidTableElement's addChild:(ocidCaptionElement))
#########【colgroup】
set ocidColgroupElement to (refMe's NSXMLElement's elementWithName:("colgroup"))
#########【col】col生成
#項目番号
set ocidAddElement to (refMe's NSXMLElement's elementWithName:("col"))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("LineNo"))
(ocidAddElement's addAttribute:(ocidAddNode))
(ocidColgroupElement's addChild:(ocidAddElement))
#キー
set ocidAddElement to (refMe's NSXMLElement's elementWithName:("col"))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("キー名"))
(ocidAddElement's addAttribute:(ocidAddNode))
(ocidColgroupElement's addChild:(ocidAddElement))
#値
set ocidAddElement to (refMe's NSXMLElement's elementWithName:("col"))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("値"))
(ocidAddElement's addAttribute:(ocidAddNode))
(ocidColgroupElement's addChild:(ocidAddElement))
#テーブルエレメントに追加
(ocidTableElement's addChild:(ocidColgroupElement))
#########【thead】
set ocidTheadElement to (refMe's NSXMLElement's elementWithName:("thead"))
set ocidTrElement to (refMe's NSXMLElement's elementWithName:("tr"))
#項目番号
set ocidAddElement to (refMe's NSXMLElement's elementWithName:("th"))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("lineNo"))
(ocidAddElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("id") stringValue:("lineNo"))
(ocidAddElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("col"))
(ocidAddElement's addAttribute:(ocidAddNode))
(ocidAddElement's setStringValue:("\"))
(ocidTrElement's addChild:(ocidAddElement))
#キー
set ocidAddElement to (refMe's NSXMLElement's elementWithName:("th"))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("key"))
(ocidAddElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("id") stringValue:("key"))
(ocidAddElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("col"))
(ocidAddElement's addAttribute:(ocidAddNode))
(ocidAddElement's setStringValue:("キー名"))
(ocidTrElement's addChild:(ocidAddElement))
#値
set ocidAddElement to (refMe's NSXMLElement's elementWithName:("th"))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("value"))
(ocidAddElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("id") stringValue:("value"))
(ocidAddElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("col"))
(ocidAddElement's addAttribute:(ocidAddNode))
(ocidAddElement's setStringValue:("値"))
(ocidTrElement's addChild:(ocidAddElement))
#TRをTHEADにセット
(ocidTheadElement's addChild:(ocidTrElement))
#THEADをテーブルにセット
(ocidTableElement's addChild:(ocidTheadElement))
########################################
#【tbody】
set ocidTbodyElement to (refMe's NSXMLElement's elementWithName:("tbody"))
#TRの開始
set ocidTrElement to (refMe's NSXMLElement's elementWithName:("tr"))
####項番処理
set ocidThElement to (refMe's NSXMLElement's elementWithName:("th"))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("項目番号:1"))
(ocidThElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("headers") stringValue:("LineNo"))
(ocidThElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("col"))
(ocidThElement's addAttribute:(ocidAddNode))
(ocidThElement's setStringValue:("項目番号:1"))
(ocidTrElement's addChild:(ocidThElement))
####キー
set ocidThElement to (refMe's NSXMLElement's elementWithName:("th"))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("key"))
(ocidThElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("headers") stringValue:("key"))
(ocidThElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("col"))
(ocidThElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("class") stringValue:("tbody_th_title"))
(ocidThElement's addAttribute:(ocidAddNode))
(ocidThElement's setStringValue:("キーの名称1"))
(ocidTrElement's addChild:(ocidThElement))
####値
set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("value"))
(ocidTdElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("headers") stringValue:("value"))
(ocidTdElement's addAttribute:(ocidAddNode))
(ocidTdElement's setStringValue:("値1"))
(ocidTrElement's addChild:(ocidTdElement))
#
(ocidTbodyElement's addChild:(ocidTrElement))


#TBODYをテーブルにセット
(ocidTableElement's addChild:(ocidTbodyElement))
#【tfoot】 TRで
set ocidTfootElement to (refMe's NSXMLElement's elementWithName:("tfoot"))
set ocidTrElement to (refMe's NSXMLElement's elementWithName:("tr"))
#colspan指定して1行でセット
set ocidThElement to (refMe's NSXMLElement's elementWithName:("th"))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("テーブルの終わり"))
(ocidThElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("colspan") stringValue:(3))
(ocidThElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("row"))
(ocidThElement's addAttribute:(ocidAddNode))
#
(ocidThElement's setStringValue:("項目数 : はXこでした"))
#THをTRにセットして
(ocidTrElement's addChild:(ocidThElement))
#TRをTFOOTにセット
(ocidTfootElement's addChild:(ocidTrElement))
#TFOOTをテーブルにセット
(ocidTableElement's addChild:(ocidTfootElement))
#テーブルをアーティクルにセット
(ocidSetArticleElement's addChild:(ocidTableElement))
##############################
#HTMLにする
##############################
set ocidHTML to doMakeRootElement({ocidSetHeaderElement, ocidSetArticleElement, ocidSetFooterElement, strTitleText})
##############################
#保存
##############################
#読み取りやすい表示
set ocidXMLdata to (ocidHTML's XMLDataWithOptions:(refMe's NSXMLNodePrettyPrint))
#保存
set listDone to (ocidXMLdata's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference))
set aliasFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias
####【6】ブラウザで開く
tell application "Finder"
open location aliasFilePath
end tell




############################################################
# 基本的なHTMLの構造
(*
doMakeRootElement({argHeaderContents, argArticleContents, argFooterContents,argTitleText})
HTMLのBODY部
header
article
footerにそれぞれAddchildするデータをリストで渡す
戻り値はRootエレメントにセットされた
NSXMLDocumentを戻すので 保存すればOK
*)
############################################################
to doMakeRootElement({argHeaderContents, argArticleContents, argFooterContents, argTitleText})
  #XML初期化
  set ocidXMLDoc to refMe's NSXMLDocument's alloc()'s init()
ocidXMLDoc's setDocumentContentKind:(refMe's NSXMLDocumentHTMLKind)
  # DTD付与
  set ocidDTD to refMe's NSXMLDTD's alloc()'s init()
ocidDTD's setName:("html")
ocidXMLDoc's setDTD:(ocidDTD)
  #
  set ocidRootElement to refMe's NSXMLElement's elementWithName:("html")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("lang") stringValue:("ja")
ocidRootElement's addAttribute:(ocidAddNode)
  #
  set ocidHeadElement to refMe's NSXMLElement's elementWithName:("head")
  #
  set ocidAddElement to refMe's NSXMLElement's elementWithName:("title")
ocidAddElement's setStringValue:(argTitleText)
ocidHeadElement's addChild:(ocidAddElement)
  # http-equiv
  set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("http-equiv") stringValue:("Content-Type")
ocidAddElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("text/html; charset=UTF-8")
ocidAddElement's addAttribute:(ocidAddNode)
ocidHeadElement's addChild:(ocidAddElement)
  #
  set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("http-equiv") stringValue:("Content-Style-Type")
ocidAddElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("text/css")
ocidAddElement's addAttribute:(ocidAddNode)
ocidHeadElement's addChild:(ocidAddElement)
  #
  set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("http-equiv") stringValue:("Content-Script-Type")
ocidAddElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("text/javascript")
ocidAddElement's addAttribute:(ocidAddNode)
ocidHeadElement's addChild:(ocidAddElement)
  #
  set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("name") stringValue:("viewport")
ocidAddElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("width=720")
ocidAddElement's addAttribute:(ocidAddNode)
ocidHeadElement's addChild:(ocidAddElement)
  #
  set ocidAddElement to refMe's NSXMLElement's elementWithName:("style")
ocidAddElement's setStringValue:("body { margin: 10px; background-color: #FFFFFF; } table { border-spacing: 0; caption-side: top; font-family: system-ui; } thead th { border: solid 1px #666666; padding: .5ch 1ch; border-block-width: 1px 0; border-inline-width: 1px 0; &:first-of-type { border-start-start-radius: .5em } &:last-of-type { border-start-end-radius: .5em; border-inline-end-width: 1px } } tbody td { word-wrap: break-word;max-width: 360px;border-spacing: 0; border: solid 1px #666666; padding: .5ch 1ch; border-block-width: 1px 0; border-inline-width: 1px 0; &:last-of-type { border-inline-end-width: 1px } } tbody th { border-spacing: 0; border: solid 1px #666666; padding: .5ch 1ch; border-block-width: 1px 0; border-inline-width: 1px 0; } tbody tr:nth-of-type(odd) { background: #F2F2F2; } .kind_string { font-size: 0.75em; } .date_string { font-size: 0.5em; } .tbody_th_title{text-align: left;} tfoot th { border: solid 1px #666666; padding: .5ch 1ch; &:first-of-type { border-end-start-radius: .5em } &:last-of-type { border-end-end-radius: .5em; border-inline-end-width: 1px } }")
ocidHeadElement's addChild:(ocidAddElement)
ocidRootElement's addChild:(ocidHeadElement)
  #
  #ボディエレメント
  set ocidBodyElement to refMe's NSXMLElement's elementWithName:("body")
  #ヘッダー
  set ocidHeaderElement to refMe's NSXMLElement's elementWithName:("header")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("header")
ocidHeaderElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_header")
ocidHeaderElement's addAttribute:(ocidAddNode)
ocidHeaderElement's addChild:(argHeaderContents)
ocidBodyElement's addChild:(ocidHeaderElement)
  #アーティクル
  set ocidArticleElement to refMe's NSXMLElement's elementWithName:("article")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("article")
ocidArticleElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_article")
ocidArticleElement's addAttribute:(ocidAddNode)
ocidArticleElement's addChild:(argArticleContents)
ocidBodyElement's addChild:(ocidArticleElement)
  #フッター
  set ocidFooterElement to refMe's NSXMLElement's elementWithName:("footer")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("footer")
ocidFooterElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_footer")
ocidFooterElement's addAttribute:(ocidAddNode)
ocidFooterElement's addChild:(argFooterContents)
ocidBodyElement's addChild:(ocidFooterElement)
  #ボディをROOTエレメントにセット
ocidRootElement's addChild:(ocidBodyElement)
  #ROOTをXMLにセット
ocidXMLDoc's setRootElement:(ocidRootElement)
  #値を戻す
return ocidXMLDoc
end doMakeRootElement



|

« [exiftool]メタ情報をHTMLで表示(だいぶ改修した) | トップページ | システム設定オープン用のスクリプト作成補助 v5 (アンカーのないパネルに対応した) »

AppleScript HTML」カテゴリの記事