#!/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 framework "AppKit"
use scripting additions
property refMe : a reference to current application
set strCommandText to ("/usr/sbin/systemstats --concise-dashboard tsv") as text
set strH3Text to ("SystemStats一覧") as text
set strCaption to ("SystemStats") as text
set strArticleH3 to ("SystemStatsコマンドで収集したデータ") as text
#書類フォルダ
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDocumentDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDocumentDirPathURL to ocidURLsArray's firstObject()
set ocidSaveDirPathURL to ocidDocumentDirPathURL's URLByAppendingPathComponent:("Apple/SystemStats")
#フォルダを作る
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
# 777-->511 755-->493 700-->448 766-->502
ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
#
set strDate to doGetDateNo("yyyyMMdd")
set ocidBasePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strDate) isDirectory:(false)
set ocidSaveFilePathURL to ocidBasePathURL's URLByAppendingPathExtension:("tsv")
set ocidHTMLFilePathURL to ocidBasePathURL's URLByAppendingPathExtension:("html")
#
set strResponse to (do shell script strCommandText) as text
#
set ocidReadString to refMe's NSString's stringWithString:(strResponse)
set ocidSaveString to doChgColRowTSV(ocidReadString)
##
set listDone to ocidSaveString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
### ファイルのテキストを読み込み
set listResponse to (refMe's NSString's alloc()'s initWithContentsOfURL:(ocidSaveFilePathURL) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))
set ocidReadString to (item 1 of listResponse)
#可変にして
set ocidLineString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0))
(ocidLineString's setString:(ocidReadString))
#改行をUNIXに強制
set ocidLineStringLF to (ocidLineString's stringByReplacingOccurrencesOfString:("\r\n") withString:("\n"))
set ocidLineString to (ocidLineStringLF's stringByReplacingOccurrencesOfString:("\r") withString:("\n"))
#改行毎でリストにする
set ocidCharSet to (refMe's NSCharacterSet's newlineCharacterSet)
set ocidLineArray to (ocidLineString's componentsSeparatedByCharactersInSet:(ocidCharSet))
#最初の1行目だけ別で取得しておく
set ocidFirstObjectString to ocidLineArray's firstObject()
set ocidFirstLineArray to ocidFirstObjectString's componentsSeparatedByString:("\t")
###データをHTMLに整形
########################################
#headerに渡すエレメント
set ocidSetHeaderElement to (refMe's NSXMLElement's elementWithName:("div"))
set ocidH3Element to refMe's NSXMLElement's elementWithName:("h3")
(ocidH3Element's setStringValue:(strH3Text))
(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:("h3"))
set strSetValue to (strArticleH3) as text
(ocidH3Element's setStringValue:(strSetValue))
(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:(strCaption)
ocidTableElement's addChild:(ocidCaptionElement)
#【colgroup】
set ocidColgroupElement to refMe's NSXMLElement's elementWithName:("colgroup")
#タイトル部の数だけ繰り返し
#項番部を追加する
set strFirstCol to ("LineNo") as text
ocidFirstLineArray's insertObject:(strFirstCol) atIndex:(0)
repeat with itemColName in ocidFirstLineArray
#【col】col生成
set ocidAddElement to (refMe's NSXMLElement's elementWithName:("col"))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:(itemColName))
(ocidAddElement's addAttribute:(ocidAddNode))
(ocidColgroupElement's addChild:(ocidAddElement))
end repeat
#テーブルエレメントに追加
ocidTableElement's addChild:(ocidColgroupElement)
#【thead】
set ocidTheadElement to refMe's NSXMLElement's elementWithName:("thead")
#TR
set ocidTrElement to refMe's NSXMLElement's elementWithName:("tr")
#タイトル部の数だけ繰り返し
repeat with itemColName in ocidFirstLineArray
if (itemColName as text) is "LineNo" then
#ここはTDではなくてTHを利用
set ocidAddElement to (refMe's NSXMLElement's elementWithName:("th"))
####項番処理
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:(itemColName))
(ocidAddElement's addAttribute:(ocidAddNode))
#
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("id") stringValue:(itemColName))
(ocidAddElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("col"))
(ocidAddElement's addAttribute:(ocidAddNode))
#値を入れる
(ocidAddElement's setStringValue:("\"))
#TH→TRにセット
(ocidTrElement's addChild:(ocidAddElement))
else
#ここはTDではなくてTHを利用
set ocidAddElement to (refMe's NSXMLElement's elementWithName:("th"))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:(itemColName))
(ocidAddElement's addAttribute:(ocidAddNode))
#
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("id") stringValue:(itemColName))
(ocidAddElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("col"))
(ocidAddElement's addAttribute:(ocidAddNode))
#値を入れる
(ocidAddElement's setStringValue:(itemColName))
#TH→TRにセット
(ocidTrElement's addChild:(ocidAddElement))
end if
end repeat
#TRをTHEADにセット
ocidTheadElement's addChild:(ocidTrElement)
#THEADをテーブルにセット
ocidTableElement's addChild:(ocidTheadElement)
########################################
#【tbody】
set ocidTbodyElement to refMe's NSXMLElement's elementWithName:("tbody")
###【3-4】:item
set numCntContents to (count of ocidLineArray) - 1 as integer
repeat with itemIntNo from 1 to numCntContents by 1
set ocidLineItem to (ocidLineArray's objectAtIndex:(itemIntNo))
#空行で終わり(TSVが改行で終わるタイプ)
if (ocidLineItem as text) is "" then
exit repeat
end if
#
set ocidItemLineArray to (ocidLineItem's componentsSeparatedByString:("\t"))
set numCntItemLineArray to (count of ocidItemLineArray) as integer
##############
#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:("項目番号:" & itemIntNo))
(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:(itemIntNo as text))
(ocidTrElement's addChild:(ocidThElement))
repeat with itemLineNo from 0 to (numCntItemLineArray - 1) by 1
set coidFieldValue to (ocidItemLineArray's objectAtIndex:(itemLineNo))
if itemLineNo = 0 then
set ocidThElement to (refMe's NSXMLElement's elementWithName:("th"))
set strTitle to (ocidFirstLineArray's objectAtIndex:(itemLineNo + 1))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:(strTitle))
(ocidThElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("headers") stringValue:(strTitle))
(ocidThElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("col"))
(ocidThElement's addAttribute:(ocidAddNode))
(ocidThElement's setStringValue:(coidFieldValue))
(ocidTrElement's addChild:(ocidThElement))
else
set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
set strTitle to (ocidFirstLineArray's objectAtIndex:(itemLineNo + 1))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:(strTitle))
(ocidTdElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("headers") stringValue:(strTitle))
(ocidTdElement's addAttribute:(ocidAddNode))
(ocidTdElement's setStringValue:(coidFieldValue))
(ocidTrElement's addChild:(ocidTdElement))
end if
end repeat
(ocidTbodyElement's addChild:(ocidTrElement))
end repeat
#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")
#項目数を取得して
set numCntCol to (count of ocidFirstLineArray) as integer
#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:(numCntCol as text))
(ocidThElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("row"))
(ocidThElement's addAttribute:(ocidAddNode))
#
set strContents to ("項目数 : " & (numCntContents - 1)) as text
(ocidThElement's setStringValue:(strContents))
#THをTRにセットして
ocidTrElement's addChild:(ocidThElement)
#TRをTFOOTにセット
ocidTfootElement's addChild:(ocidTrElement)
#TFOOTをテーブルにセット
ocidTableElement's addChild:(ocidTfootElement)
#テーブルをArticleにセット
ocidSetArticleElement's addChild:(ocidTableElement)
##############################
#HTMLにする
##############################
set ocidHTML to doMakeRootElement({ocidSetHeaderElement, ocidSetArticleElement, ocidSetFooterElement})
##############################
#保存
##############################
#####保存
#読み取りやすい表示
set ocidXMLdata to ocidHTML's XMLDataWithOptions:(refMe's NSXMLNodePrettyPrint)
#保存
set listDone to ocidXMLdata's writeToURL:(ocidHTMLFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference)
####ブラウザで開く
set aliasFilePath to (ocidHTMLFilePathURL's absoluteURL()) as alias
tell application "Finder"
open location aliasFilePath
end tell
############################################################
# 基本的なHTMLの構造
(*
doMakeRootElement({argHeaderContents, argArticleContents, argFooterContents})
HTMLのBODY部
header
article
footerにそれぞれAddchildするデータをリストで渡す
戻り値はRootエレメントにセットされた
NSXMLDocumentを戻すので 保存すればOK
*)
############################################################
to doMakeRootElement({argHeaderContents, argArticleContents, argFooterContents})
#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:("TSV2HTML")
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=device-width, initial-scale=1.0")
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;word-wrap: break-word;white-space: pre-wrap;&: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;border-spacing: 0;border: solid 1px #666666;padding: .5ch 1ch;border-block-width: 1px 0;border-inline-width: 1px 0;word-wrap: break-word;white-space: pre-wrap;&: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;word-wrap: break-word;white-space: pre-wrap;text-align: left;}tbody tr:nth-of-type(odd) {background: #F2F2F2;}.kind_string {font-size: 0.75em;}.date_string {font-size: 0.5em;}tfoot th {border: solid 1px #666666;padding: .5ch 1ch;word-wrap: break-word;white-space: pre-wrap;&: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
##############################
### 今の日付日間 テキスト
##############################
to doGetDateNo(argDateFormat)
####日付情報の取得
set ocidDate to current application's NSDate's |date|()
###日付のフォーマットを定義
set ocidNSDateFormatter to current application's NSDateFormatter's alloc()'s init()
ocidNSDateFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
set ocidTimeZone to refMe's NSTimeZone's alloc()'s initWithName:"Asia/Tokyo"
ocidNSDateFormatter's setTimeZone:(ocidTimeZone)
ocidNSDateFormatter's setDateFormat:(argDateFormat)
set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate
set strDateAndTime to ocidDateAndTime as text
return strDateAndTime
end doGetDateNo
##############################
### 行列入替
##############################
to doChgColRowTSV(argNsstring)
#可変にして
set ocidLineString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0))
(ocidLineString's setString:(argNsstring))
#改行をUNIXに強制
set ocidLineStringLF to (ocidLineString's stringByReplacingOccurrencesOfString:("\r\n") withString:("\n"))
set ocidLineString to (ocidLineStringLF's stringByReplacingOccurrencesOfString:("\r") withString:("\n"))
#改行毎でリストにする
set ocidCharSet to (refMe's NSCharacterSet's newlineCharacterSet)
set ocidTextArray to (ocidLineString's componentsSeparatedByCharactersInSet:(ocidCharSet))
#リストの数
set numCntArray to (count of ocidTextArray) as integer
#出力用のテキスト
set ocidSaveString to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
set ocidColArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
#1行目のテキストを取り出して
set ocidFirsText to ocidTextArray's firstObject()
#タブ区切りテキストにする
set ocidFirstRowArray to ocidFirsText's componentsSeparatedByString:("\t")
#1行目のリストの数
set numCntFirstArray to (count of ocidFirstRowArray) as integer
#2行目からの処理
repeat with itemRowNo from 0 to (numCntFirstArray - 1) by 1
#まずは1行目のデータを取り出して
set ocidFirstItemText to (ocidFirstRowArray's objectAtIndex:(itemRowNo))
#出力用テキストに追加しておく
(ocidSaveString's appendString:(ocidFirstItemText))
(ocidSaveString's appendString:("\t"))
#2列目以降のデータを順に取り出して
repeat with itemColNo from 1 to (numCntArray - 1) by 1
#2行目以降を順番に取り出して
set ocidLineText to (ocidTextArray's objectAtIndex:(itemColNo))
#リストにする
set ocidRowArray to (ocidLineText's componentsSeparatedByString:("\t"))
#対象の列の値を取り出して
set ocidLineItemText to (ocidRowArray's objectAtIndex:(itemRowNo))
#追加していく
(ocidSaveString's appendString:(ocidLineItemText))
#最後のデータにはタブ入れない
if itemColNo < (numCntArray - 1) then
(ocidSaveString's appendString:("\t"))
end if
end repeat
(ocidSaveString's appendString:("\n"))
end repeat
return ocidSaveString
end doChgColRowTSV