AppleScript XML

[NSXMLElement]エレメントのコピー


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

#!/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 "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

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

#############################
###ダイアログを前面に出す
set strName to (name of current application) as text
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
############ デフォルトロケーション
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
tell application "Finder"
  set aliasDefaultLocation to container of (path to me) as alias
  set aliasDefaultLocation to (path to desktop folder from user domain) as alias
end tell

############UTIリスト
set listUTI to {"public.xml", "public.rss", "public.opml", "dyn.ah62d4rv4ge8086drru"}

set strMes to ("ファイルを選んでください") as text
set strPrompt to ("ファイルを選んでください") as text
try
  ### ファイル選択時
  set aliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias
on error
log "エラーしました"
return "エラーしました"
end try

set strFilePath to (POSIX path of aliasFilePath) 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 ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
set ocidSaveFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("edit.opml")
####################
set ocidOption to (refMe's NSXMLDocumentTidyXML)
set listReadXMLDoc to (refMe's NSXMLDocument's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error|:(reference))
###XMLドキュメント
set ocidReadXMLDoc to (item 1 of listReadXMLDoc)
###ROOTエレメント
set ocidRootElement to ocidReadXMLDoc's rootElement()
set ocidHeadElement to (ocidRootElement's elementsForName:("head"))'s firstObject()
set ocidHeadCopy to ocidHeadElement's XMLString()
###bodyエレメント
set ocidBody to (ocidRootElement's elementsForName:("body"))'s firstObject()
set ocidBodyCopy to ocidBody's XMLString()

####################
#【保存】XML初期化
set ocidSaveXMLDoc to refMe's NSXMLDocument's alloc()'s init()
ocidSaveXMLDoc's setVersion:("1.0")
ocidSaveXMLDoc's setCharacterEncoding:("utf-8")
ocidSaveXMLDoc's setDocumentContentKind:(refMe's NSXMLDocumentXMLKind)
#【保存】ROOT
set ocidSaveRootElement to refMe's NSXMLElement's elementWithName:("opml")
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("version") stringValue:("1.0")
ocidSaveRootElement's addAttribute:(ocidAddNode)
#【保存】HEAD
set ocidSaveHeadElement to refMe's NSXMLElement's alloc()'s initWithXMLString:(ocidHeadCopy) |error|:(missing value)
#【保存】BODY
set ocidSaveBodyElement to refMe's NSXMLElement's alloc()'s initWithXMLString:(ocidBodyCopy) |error|:(missing value)




####################
#【保存】
ocidSaveRootElement's addChild:(ocidSaveHeadElement)
ocidSaveRootElement's addChild:(ocidSaveBodyElement)
ocidSaveXMLDoc's setRootElement:(ocidSaveRootElement)
##########
#【保存】読み取りやすい表示
set ocidXMLdata to ocidSaveXMLDoc's XMLDataWithOptions:(refMe's NSXMLNodePrettyPrint)
#保存
set listDone to ocidXMLdata's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference)


|

[XML]CDATA(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 framework "Foundation"
use AppleScript version "2.8"
use scripting additions

property refMe : a reference to current application


set strHTMLtext to ("美しい日本語の\nオープンタイプフォント")
set ocidHTMLtext to refMe's NSString's stringWithString:(strHTMLtext)
set ocidHTML to (ocidHTMLtext's stringByReplacingOccurrencesOfString:("\n") withString:("<br />"))
log ocidHTML as text
--> 美しい日本語の<br />オープンタイプフォント

###XML初期化
set ocidXMLDoc to refMe's NSXMLDocument's alloc()'s init()
ocidXMLDoc's setDocumentContentKind:(refMe's NSXMLDocumentHTMLKind)
set ocidDTD to refMe's NSXMLDTD's alloc()'s init()
###DTD追加
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:("CDATAをXMLにテキスト追加する")
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)
ocidRootElement's addChild:(ocidHeadElement)
###ボディエレメント
set ocidBodyElement to refMe's NSXMLElement's elementWithName:("body")

###########
#本処理
# NODEをCDATAとして新規作成
set ocidAddNode to (refMe's NSXMLNode's alloc()'s initWithKind:(refMe's NSXMLTextKind) options:(refMe's NSXMLNodeIsCDATA))
#NODEにテキストとしてHTMLを追加
(ocidAddNode's setStringValue:(ocidHTML))
#テキストを追加したCDATA用のノードをボディにセット
(ocidBodyElement's addChild:(ocidAddNode))


###ボディをROOTに追加
ocidRootElement's addChild:(ocidBodyElement)
###ROOTをXMLに追加
ocidXMLDoc's setRootElement:(ocidRootElement)
###テキストにする
set ocidXMLdata to ocidXMLDoc's XMLStringWithOptions:(refMe's NSXMLNodePrettyPrint)

log ocidXMLdata as text

(*<!DOCTYPE html>

<html lang="ja">
<head>
<title>CDATAをXMLにテキスト追加する</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body><![CDATA[美しい日本語の<br />オープンタイプフォント]]></body>
</html>*)

|

TSV2AdobeXML タブ区切りテキストからAdobe Illustratorの変数印字用の『変数ライブラリ』を生成する(1ページに複数面付け版)

TSV2AdobeXML タブ区切りテキストからAdobe Illustratorの変数印字用の『変数ライブラリ』を生成する
https://quicktimer.cocolog-nifty.com/icefloe/2024/03/post-bba3c3.html

複数項目を1ページ内に面付けする用

ダウンロード - tsv2adobexml4entries.zip



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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
(*
com.cocolog-nifty.quicktimer.icefloe
TSVタブ区切りテキストから
Adobe Illustrator用の変数用のXMLを生成します

設定項目で1ページに面付けする数を設定してください
デフォルトは10です
-->1ページに10件分のデータを割り付けます

【制限事項】その1
TSVの1行目の項目がデータセット名になります
表の1行目が項目名称になるようなTSVで利用してください
【制限事項】その2
可変テキストのみ設定可能です
他の要素の付与はご相談ください
*)
----+----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
property refNSNotFound : a reference to 9.22337203685477E+18 + 5807
set appFileManager to refMe's NSFileManager's defaultManager()

#################################
### 【1】設定項目
#################################
#1ページの面付け数
set numPageEntries to 10 as integer

#################################
### 【1】入力ファイル
#################################
#ダイアログ
tell current application
  set strName to name as text
end tell
#スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
#デフォルトロケーション
tell application "Finder"
  set aliasPathToMe to (path to me) as alias
  set aliasContainerDir to (container of aliasPathToMe) as alias
end tell
#
set listUTI to {"public.tab-separated-values-text"}
set strMes to ("ファイルを選んでください") as text
set strPrompt to ("ファイルを選んでください") as text
try
  ### ファイル選択時
  set aliasFilePath to (choose file strMes with prompt strPrompt default location aliasContainerDir of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias
on error
log "エラーしました"
return "エラーしました"
end try
if aliasFilePath is (missing value) then
return "選んでください"
end if
#ファイルパス
set strFilePath to (POSIX path of aliasFilePath) 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)
#################################
### 【2】保存先
#################################
set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
set aliasContainerDirPath to (ocidContainerDirPathURL's absoluteURL()) as alias
set strMes to "フォルダを選んでください" as text
set strPrompt to "保存先フォルダを選択してください\n同名ファイルがある場合\n『上書き』になります" as text
try
  ###ダイアログを前面に出す
  set strName to (name of current application) as text
  if strName is "osascript" then
    tell application "Finder" to activate
  else
    tell current application to activate
  end if
  #
  set aliasResponse to (choose folder strMes with prompt strPrompt default location aliasContainerDirPath with invisibles and showing package contents without multiple selections allowed) as alias
on error
log "エラーしました"
return "エラーしました"
end try
#戻り値エイリアスをURLに
set strSaveDirPath to (POSIX path of aliasResponse) as text
set ocidSaveDirPathStr to (refMe's NSString's stringWithString:(strSaveDirPath))
set ocidSaveDirPath to ocidSaveDirPathStr's stringByStandardizingPath()
set ocidSaveDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveDirPath) isDirectory:true)
##出力用ファイルパス
set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("variables.xml")
#################################
### 【3】本処理
#################################
#ファイル読み込み
set listResponse to (refMe's NSString's alloc()'s initWithContentsOfURL:(ocidFilePathURL) usedEncoding:(refMe's NSUTF8StringEncoding) |error|:(reference))
set ocidReadString to (item 1 of listResponse)
if ocidReadString = (missing value) then
return "文字コードを判定出来ませんでした : " & strFileName
end if

#改行コードをUNIXに強制
set ocidCRChar to refMe's NSString's stringWithString:("\r")
set ocidLFChar to refMe's NSString's stringWithString:("\n")
set ocidCRLFChar to refMe's NSString's stringWithString:("\r\n")
set ocidReadStringsA to ocidReadString's stringByReplacingOccurrencesOfString:(ocidCRLFChar) withString:(ocidLFChar)
set ocidReadStrings to ocidReadStringsA's stringByReplacingOccurrencesOfString:(ocidCRChar) withString:(ocidLFChar)

#改行毎でリストにする
set ocidCharSet to (refMe's NSCharacterSet's newlineCharacterSet)
set ocidLineArray to (ocidReadStrings's componentsSeparatedByCharactersInSet:(ocidCharSet))

#出力用のリスト
set ocidEditLineArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)

#読み取ったデータに項目連番を入れておく
set numCntLineNo to 0 as integer
repeat with itemLineArray in ocidLineArray
  set ocidEditLineText to (refMe's NSMutableString's alloc()'s initWithCapacity:(0))
  if numCntLineNo = 0 then
    set strAppendText to ("rowno\t") as text
(ocidEditLineText's appendString:(strAppendText))
(ocidEditLineText's appendString:(itemLineArray))
  else
    if (itemLineArray as text) is "" then
      exit repeat
    end if
    #タブ区切りで挿入
    set strAppendText to (numCntLineNo & "\t") as text
(ocidEditLineText's appendString:(strAppendText))
(ocidEditLineText's appendString:(itemLineArray))
  end if
(ocidEditLineArray's addObject:(ocidEditLineText))
  #カウントアップ
  set numCntLineNo to (numCntLineNo + 1) as integer
end repeat

#最初の1行目だけ別で取得しておく
set ocidFirstObjectString to ocidEditLineArray's firstObject()
set ocidFirstRowArray to ocidFirstObjectString's componentsSeparatedByString:("\t")

################################
###XML初期化
set ocidXMLDoc to refMe's NSXMLDocument's alloc()'s init()
ocidXMLDoc's setVersion:("1.0")
ocidXMLDoc's setCharacterEncoding:("utf-8")
ocidXMLDoc's setDocumentContentKind:(refMe's NSXMLDocumentXMLKind)
###DTD生成
set ocidDTD to refMe's NSXMLDTD's alloc()'s init()
ocidDTD's setName:("svg")
ocidDTD's setPublicID:("-//W3C//DTD SVG 20001102//EN")
ocidDTD's setSystemID:("http://www.w3.org/TR/2000/CR-SVG-20001102/DTD/svg-20001102.dtd")
#Entitry付与
set strEntitry to ("<!ENTITY ns_graphs \"http://ns.adobe.com/Graphs/1.0/\">") as text
set ocidEntitry to refMe's NSString's stringWithString:(strEntitry)
set ocidEntityNode to refMe's NSXMLDTDNode's alloc()'s initWithXMLString:(ocidEntitry)
ocidEntityNode's setDTDKind:(refMe's NSXMLAttributeEntityKind)
ocidDTD's addChild:(ocidEntityNode)
#Entitry付与
set strEntitry to ("<!ENTITY ns_vars \"http://ns.adobe.com/Variables/1.0/\">") as text
set ocidEntitry to refMe's NSString's stringWithString:(strEntitry)
set ocidEntityNode to refMe's NSXMLDTDNode's alloc()'s initWithXMLString:(ocidEntitry)
ocidEntityNode's setDTDKind:(refMe's NSXMLAttributeEntityKind)
ocidDTD's addChild:(ocidEntityNode)
#Entitry付与
set strEntitry to ("<!ENTITY ns_imrep \"http://ns.adobe.com/ImageReplacement/1.0/\">") as text
set ocidEntitry to refMe's NSString's stringWithString:(strEntitry)
set ocidEntityNode to refMe's NSXMLDTDNode's alloc()'s initWithXMLString:(ocidEntitry)
ocidEntityNode's setDTDKind:(refMe's NSXMLAttributeEntityKind)
ocidDTD's addChild:(ocidEntityNode)
#Entitry付与
set strEntitry to ("<!ENTITY ns_custom \"http://ns.adobe.com/GenericCustomNamespace/1.0/\">") as text
set ocidEntitry to refMe's NSString's stringWithString:(strEntitry)
set ocidEntityNode to refMe's NSXMLDTDNode's alloc()'s initWithXMLString:(ocidEntitry)
ocidEntityNode's setDTDKind:(refMe's NSXMLAttributeEntityKind)
ocidDTD's addChild:(ocidEntityNode)
#Entitry付与
set strEntitry to ("<!ENTITY ns_flows \"http://ns.adobe.com/Flows/1.0/\">") as text
set ocidEntitry to refMe's NSString's stringWithString:(strEntitry)
set ocidEntityNode to refMe's NSXMLDTDNode's alloc()'s initWithXMLString:(ocidEntitry)
ocidEntityNode's setDTDKind:(refMe's NSXMLAttributeEntityKind)
ocidDTD's addChild:(ocidEntityNode)
#Entitry付与
set strEntitry to ("<!ENTITY ns_extend \"http://ns.adobe.com/Extensibility/1.0/\">") as text
set ocidEntitry to refMe's NSString's stringWithString:(strEntitry)
set ocidEntityNode to refMe's NSXMLDTDNode's alloc()'s initWithXMLString:(ocidEntitry)
ocidEntityNode's setDTDKind:(refMe's NSXMLAttributeEntityKind)
ocidDTD's addChild:(ocidEntityNode)
##DTDをXMLDOCにセット
ocidXMLDoc's setDTD:(ocidDTD)
###ROOTエレメント
set ocidRootElement to refMe's NSXMLElement's elementWithName:("svg")
#【1】variableSets
set ocidVsetsElement to refMe's NSXMLElement's elementWithName:("variableSets")
set ocidSetNameSpace to refMe's NSXMLNode's namespaceWithName:("") stringValue:("&ns_vars;")
ocidVsetsElement's addNamespace:(ocidSetNameSpace)

#【2】variableSet
set ocidVsetElement to refMe's NSXMLElement's elementWithName:("variableSet")
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("varSetName") stringValue:("binding1"))
(ocidVsetElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("locked") stringValue:("none"))
(ocidVsetElement's addAttribute:(ocidAddNode))

#【3−1】variables
set ocidVariablesElement to refMe's NSXMLElement's elementWithName:("variables")
################
#【3−2】variable
(*
可変テキスト:trait="textcontent" category="&ns_flows;"
フィアルパス:trait="fileref" category="&ns_vars;"
表示有無:trait="visibility" category="&ns_vars;"
表示有無?:trait="graphdata" category="&ns_graphs;"
*)
###########################
##ページレイアウト
###########################
repeat with itemEntries from 1 to numPageEntries by 1
  #【3−2−1】連番用の追加項目
  set ocidVariableElement to (refMe's NSXMLElement's elementWithName:("variable"))
  
  #【3−2−2】読み込みデータの項目
  repeat with itemFirstRowArray in ocidFirstRowArray
    set strFirstRowArray to itemFirstRowArray as text
    set ocidVariableElement to (refMe's NSXMLElement's elementWithName:("variable"))
    #ページのエントリー数を入れる
    set strSetVarName to ("E" & itemEntries & "-" & strFirstRowArray) as text
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("varName") stringValue:(strSetVarName))
(ocidVariableElement's addAttribute:(ocidAddNode))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("category") stringValue:("&ns_flows;"))
(ocidVariableElement's addAttribute:(ocidAddNode))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("trait") stringValue:("textcontent"))
(ocidVariableElement's addAttribute:(ocidAddNode))
    #【3−2−2】variableを【3−1】variablesにセット
(ocidVariablesElement's addChild:(ocidVariableElement))
  end repeat
end repeat

################
#【4−1】DataSets
set ocidDataSetsElement to refMe's NSXMLElement's elementWithName:("v:sampleDataSets")
set ocidSetNameSpace to refMe's NSXMLNode's namespaceWithName:("v") stringValue:("&ns_vars;")
ocidDataSetsElement's addNamespace:(ocidSetNameSpace)
set ocidSetNameSpace to refMe's NSXMLNode's namespaceWithName:("") stringValue:("&ns_custom;")
ocidDataSetsElement's addNamespace:(ocidSetNameSpace)

################
#【4−2】DataSet
#データのリスト行数 先頭行の項目名行を除くので1引く
set numCntEditArray to ((count of ocidEditLineArray) - 1) as integer
#データの行数から何ページ分のデータ=dataSetの数
set numMakePage to (round of (numCntEditArray / numPageEntries) rounding up) as integer
#取り出すデータ行番号初期化
set numCntRowNoAll to 1 as integer
#作成される予定ページ数=dataSetの数ぶん繰り返す
repeat with itemdataSet from 1 to numMakePage by 1
  #【4−2】DataSetのエレメント
  set ocidDataSetElement to (refMe's NSXMLElement's elementWithName:("v:sampleDataSet"))
  #DataSetのアトリビュートに項目番号+2列目のデータで命名する
  set strSetDataSetName to (itemdataSet & ":ページ目") as text
  #↑の内容でdataSetNameをセット
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("dataSetName") stringValue:(strSetDataSetName))
(ocidDataSetElement's addAttribute:(ocidAddNode))
  #1ページに入る行目数分くりかえし
  repeat with itemPageEntries from 1 to numPageEntries by 1
    #指定行数の行データを取得
    set ocidRowStrings to (ocidEditLineArray's objectAtIndex:(numCntRowNoAll))
    #区切り文字タブでリストにして
    set ocidRowArray to (ocidRowStrings's componentsSeparatedByString:("\t"))
    #OS用の0スタートで行の項目数
    set numCntRowItemAll to ((count of ocidRowArray) - 1) as integer
    #行項目のリストから取得するための項目数初期化
    set numCntRowItem to 0 as integer
    #項目の数だけ繰り返し
    repeat with itemFirstRowArray in ocidFirstRowArray
      #ページ内での項目数+項目名でセット
      set strSetVarName to ("E" & itemPageEntries & "-" & itemFirstRowArray) as text
      set ocidDataSetChildElement to (refMe's NSXMLElement's elementWithName:(strSetVarName))
      #対象行列データを取得
      set strSetValieRowItem to (ocidRowArray's objectAtIndex:(numCntRowItem)) as text
      #Pエレメント内にテキストとして値をセット
      set ocidPElement to (refMe's NSXMLElement's elementWithName:("p"))
(ocidPElement's setStringValue:(strSetValieRowItem))
(ocidDataSetChildElement's addChild:(ocidPElement))
(ocidDataSetElement's addChild:(ocidDataSetChildElement))
      #カウントアップ
      set numCntRowItem to (numCntRowItem + 1) as integer
    end repeat
    set numCntRowNoAll to numCntRowNoAll + 1 as integer
    #データ行項目数より行カウンターが多くなったら終わり
    if numCntRowNoAll > (numCntEditArray) then
      exit repeat
    end if
  end repeat
  #【4−2】DataSetを【4−1】DataSetsにセット
(ocidDataSetsElement's addChild:(ocidDataSetElement))
end repeat

#【3−1】variablesを【2】のvariableSetにセット
ocidVsetElement's addChild:(ocidVariablesElement)
#【4−1】DataSetsを【2】のvariableSetにセット
ocidVsetElement's addChild:(ocidDataSetsElement)
#【2】variableSetを【1】のvariableSetsにセット
ocidVsetsElement's addChild:(ocidVsetElement)
#【1】のvariableSetsをROOTにセット
ocidRootElement's addChild:(ocidVsetsElement)
#
################################
###保存
ocidXMLDoc's setRootElement:(ocidRootElement)
###NSXMLNodePrettyPrint形式でデータにして
###NSXMLNodeCompactEmptyElementにすると少しデータサイズが節約できる
set ocidXMLdata to ocidXMLDoc's XMLDataWithOptions:(refMe's NSXMLNodeExpandEmptyElement)
###データをテキストに戻して
set ocidXMLString to refMe's NSString's alloc()'s initWithData:(ocidXMLdata) encoding:(refMe's NSUTF8StringEncoding)
###改行コードをWindowsに
set ocidSaveStrings to ocidXMLString's stringByReplacingOccurrencesOfString:(ocidLFChar) withString:(ocidCRLFChar)
###保存
set listDone to (ocidSaveStrings's writeToURL:(ocidSaveFilePathURL) atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))

return




|

TSV2AdobeXML タブ区切りテキストからAdobe Illustratorの変数印字用の『変数ライブラリ』を生成する

TSV2AdobeXML タブ区切りテキストからAdobe Illustratorの変数印字用の『変数ライブラリ』を生成する(1ページに複数面付け版)
https://quicktimer.cocolog-nifty.com/icefloe/2024/03/post-143f05.html
もあります
このページのスクリプトは1ページに1項目版です

変数パネルを使った可変テキスト
1:変数パネルを出す
2:変数パネル
3:ハンバーガーから『変数ライブラリの読み込み』
4:読み込んだ状態
5:テキストオブジェクトを生成して
6:使用したい変数を選択してから『テキスト動的に設定』を実行
7:動作確認

ダウンロード - makevariables.zip


1:変数パネルを出す
1_20240306130101
2:変数パネル
2_20240306130101
3:ハンバーガーから『変数ライブラリの読み込み』
3_20240306130101
4:読み込んだ状態
4_20240306130101
5:テキストオブジェクトを生成して
5_20240306130101
6:使用したい変数を選択してから『テキスト動的に設定』を実行
6_20240306130101
7:動作確認
7_20240306130101


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
(*
com.cocolog-nifty.quicktimer.icefloe
TSVタブ区切りテキストから
Adobe Illustrator用の変数用のXMLを生成します
【制限事項】その1
TSVの1行目の項目がデータセット名になります
表の1行目が項目名称になるようなTSVで利用してください
【制限事項】その2
可変テキストのみ設定可能です
他の要素の付与はご相談ください
*)
----+----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
property refNSNotFound : a reference to 9.22337203685477E+18 + 5807
set appFileManager to refMe's NSFileManager's defaultManager()


#################################
### 【1】入力ファイル
#################################
#ダイアログ
tell current application
  set strName to name as text
end tell
#スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
#デフォルトロケーション
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias
#
set listUTI to {"public.tab-separated-values-text"}
set strMes to ("ファイルを選んでください") as text
set strPrompt to ("ファイルを選んでください") as text
try
  ### ファイル選択時
  set aliasFilePath to (choose file strMes with prompt strPrompt default location aliasDesktopDirPath of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias
on error
log "エラーしました"
return "エラーしました"
end try
if aliasFilePath is (missing value) then
return "選んでください"
end if
#ファイルパス
set strFilePath to (POSIX path of aliasFilePath) 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)
#################################
### 【2】保存先
#################################
set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
set aliasContainerDirPath to (ocidContainerDirPathURL's absoluteURL()) as alias
set strMes to "フォルダを選んでください" as text
set strPrompt to "保存先フォルダを選択してください\n同名ファイルがある場合\n『上書き』になります" as text
try
  ###ダイアログを前面に出す
  set strName to (name of current application) as text
  if strName is "osascript" then
    tell application "Finder" to activate
  else
    tell current application to activate
  end if
  #
  set aliasResponse to (choose folder strMes with prompt strPrompt default location aliasContainerDirPath with invisibles and showing package contents without multiple selections allowed) as alias
on error
log "エラーしました"
return "エラーしました"
end try
#戻り値エイリアスをURLに
set strSaveDirPath to (POSIX path of aliasResponse) as text
set ocidSaveDirPathStr to (refMe's NSString's stringWithString:(strSaveDirPath))
set ocidSaveDirPath to ocidSaveDirPathStr's stringByStandardizingPath()
set ocidSaveDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveDirPath) isDirectory:true)
##出力用ファイルパス
set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("variables.xml")
#################################
### 【3】本処理
#################################
#ファイル読み込み
set listResponse to (refMe's NSString's alloc()'s initWithContentsOfURL:(ocidFilePathURL) usedEncoding:(refMe's NSUTF8StringEncoding) |error|:(reference))
set ocidReadString to (item 1 of listResponse)
if ocidReadString = (missing value) then
return "文字コードを判定出来ませんでした : " & strFileName
end if

#改行コードをUNIXに強制
set ocidCRChar to refMe's NSString's stringWithString:("\r")
set ocidLFChar to refMe's NSString's stringWithString:("\n")
set ocidCRLFChar to refMe's NSString's stringWithString:("\r\n")
set ocidReadStringsA to ocidReadString's stringByReplacingOccurrencesOfString:(ocidCRLFChar) withString:(ocidLFChar)
set ocidReadStrings to ocidReadStringsA's stringByReplacingOccurrencesOfString:(ocidCRChar) withString:(ocidLFChar)
#改行毎でリストにする
set ocidCharSet to (refMe's NSCharacterSet's newlineCharacterSet)
set ocidLineArray to (ocidReadStrings's componentsSeparatedByCharactersInSet:(ocidCharSet))
#出力用のリスト
set ocidEditLineArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
#読み取ったデータに項目連番を入れておく
set numCntLineNo to 0 as integer
repeat with itemLineArray in ocidLineArray
  set ocidEditLineText to (refMe's NSMutableString's alloc()'s initWithCapacity:(0))
  if numCntLineNo = 0 then
    set strAppendText to ("rowno\t") as text
(ocidEditLineText's appendString:(strAppendText))
(ocidEditLineText's appendString:(itemLineArray))
  else
    if (itemLineArray as text) is "" then
      exit repeat
    end if
    set strAppendText to (numCntLineNo & "\t") as text
(ocidEditLineText's appendString:(strAppendText))
(ocidEditLineText's appendString:(itemLineArray))
  end if
(ocidEditLineArray's addObject:(ocidEditLineText))
  #カウントアップ
  set numCntLineNo to (numCntLineNo + 1) as integer
end repeat

#最初の1行目だけ別で取得しておく
set ocidFirstObjectString to ocidEditLineArray's firstObject()
set ocidFirstRowArray to ocidFirstObjectString's componentsSeparatedByString:("\t")

################################
###XML初期化
set ocidXMLDoc to refMe's NSXMLDocument's alloc()'s init()
ocidXMLDoc's setVersion:("1.0")
ocidXMLDoc's setCharacterEncoding:("utf-8")
ocidXMLDoc's setDocumentContentKind:(refMe's NSXMLDocumentXMLKind)
###DTD生成
set ocidDTD to refMe's NSXMLDTD's alloc()'s init()
ocidDTD's setName:("svg")
ocidDTD's setPublicID:("-//W3C//DTD SVG 20001102//EN")
ocidDTD's setSystemID:("http://www.w3.org/TR/2000/CR-SVG-20001102/DTD/svg-20001102.dtd")
#Entitry付与
set strEntitry to ("<!ENTITY ns_graphs \"http://ns.adobe.com/Graphs/1.0/\">") as text
set ocidEntitry to refMe's NSString's stringWithString:(strEntitry)
set ocidEntityNode to refMe's NSXMLDTDNode's alloc()'s initWithXMLString:(ocidEntitry)
ocidEntityNode's setDTDKind:(refMe's NSXMLAttributeEntityKind)
ocidDTD's addChild:(ocidEntityNode)
#Entitry付与
set strEntitry to ("<!ENTITY ns_vars \"http://ns.adobe.com/Variables/1.0/\">") as text
set ocidEntitry to refMe's NSString's stringWithString:(strEntitry)
set ocidEntityNode to refMe's NSXMLDTDNode's alloc()'s initWithXMLString:(ocidEntitry)
ocidEntityNode's setDTDKind:(refMe's NSXMLAttributeEntityKind)
ocidDTD's addChild:(ocidEntityNode)
#Entitry付与
set strEntitry to ("<!ENTITY ns_imrep \"http://ns.adobe.com/ImageReplacement/1.0/\">") as text
set ocidEntitry to refMe's NSString's stringWithString:(strEntitry)
set ocidEntityNode to refMe's NSXMLDTDNode's alloc()'s initWithXMLString:(ocidEntitry)
ocidEntityNode's setDTDKind:(refMe's NSXMLAttributeEntityKind)
ocidDTD's addChild:(ocidEntityNode)
#Entitry付与
set strEntitry to ("<!ENTITY ns_custom \"http://ns.adobe.com/GenericCustomNamespace/1.0/\">") as text
set ocidEntitry to refMe's NSString's stringWithString:(strEntitry)
set ocidEntityNode to refMe's NSXMLDTDNode's alloc()'s initWithXMLString:(ocidEntitry)
ocidEntityNode's setDTDKind:(refMe's NSXMLAttributeEntityKind)
ocidDTD's addChild:(ocidEntityNode)
#Entitry付与
set strEntitry to ("<!ENTITY ns_flows \"http://ns.adobe.com/Flows/1.0/\">") as text
set ocidEntitry to refMe's NSString's stringWithString:(strEntitry)
set ocidEntityNode to refMe's NSXMLDTDNode's alloc()'s initWithXMLString:(ocidEntitry)
ocidEntityNode's setDTDKind:(refMe's NSXMLAttributeEntityKind)
ocidDTD's addChild:(ocidEntityNode)
#Entitry付与
set strEntitry to ("<!ENTITY ns_extend \"http://ns.adobe.com/Extensibility/1.0/\">") as text
set ocidEntitry to refMe's NSString's stringWithString:(strEntitry)
set ocidEntityNode to refMe's NSXMLDTDNode's alloc()'s initWithXMLString:(ocidEntitry)
ocidEntityNode's setDTDKind:(refMe's NSXMLAttributeEntityKind)
ocidDTD's addChild:(ocidEntityNode)
##DTDをXMLDOCにセット
ocidXMLDoc's setDTD:(ocidDTD)
###ROOTエレメント
set ocidRootElement to refMe's NSXMLElement's elementWithName:("svg")
#【1】variableSets
set ocidVsetsElement to refMe's NSXMLElement's elementWithName:("variableSets")
set ocidSetNameSpace to refMe's NSXMLNode's namespaceWithName:("") stringValue:("&ns_vars;")
ocidVsetsElement's addNamespace:(ocidSetNameSpace)

#【2】variableSet
set ocidVsetElement to refMe's NSXMLElement's elementWithName:("variableSet")
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("varSetName") stringValue:("binding1"))
(ocidVsetElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("locked") stringValue:("none"))
(ocidVsetElement's addAttribute:(ocidAddNode))

#【3−1】variables
set ocidVariablesElement to refMe's NSXMLElement's elementWithName:("variables")
################
#【3−2】variable
(*
可変テキスト:trait="textcontent" category="&ns_flows;"
フィアルパス:trait="fileref" category="&ns_vars;"
表示有無:trait="visibility" category="&ns_vars;"
表示有無?:trait="graphdata" category="&ns_graphs;"
*)
#【3−2−1】連番用の追加項目
set ocidVariableElement to (refMe's NSXMLElement's elementWithName:("variable"))

#【3−2−2】読み込みデータの項目
repeat with itemFirstRowArray in ocidFirstRowArray
  set strFirstRowArray to itemFirstRowArray as text
  set ocidVariableElement to (refMe's NSXMLElement's elementWithName:("variable"))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("varName") stringValue:(strFirstRowArray))
(ocidVariableElement's addAttribute:(ocidAddNode))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("category") stringValue:("&ns_flows;"))
(ocidVariableElement's addAttribute:(ocidAddNode))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("trait") stringValue:("textcontent"))
(ocidVariableElement's addAttribute:(ocidAddNode))
  #【3−2−2】variableを【3−1】variablesにセット
(ocidVariablesElement's addChild:(ocidVariableElement))
end repeat

################
#【4−1】DataSets
set ocidDataSetsElement to refMe's NSXMLElement's elementWithName:("v:sampleDataSets")
set ocidSetNameSpace to refMe's NSXMLNode's namespaceWithName:("v") stringValue:("&ns_vars;")
ocidDataSetsElement's addNamespace:(ocidSetNameSpace)
set ocidSetNameSpace to refMe's NSXMLNode's namespaceWithName:("") stringValue:("&ns_custom;")
ocidDataSetsElement's addNamespace:(ocidSetNameSpace)
################
#【4−2】DataSet
#データのリストの数
set numCntEditArray to (count of ocidEditLineArray) as integer
#データリストの2行目から最後まで繰り返す
repeat with itemIntNo from 1 to (numCntEditArray - 1) by 1
  #【4−2】DataSetのエレメント
  set ocidDataSetElement to (refMe's NSXMLElement's elementWithName:("v:sampleDataSet"))
  #DataSetのアトリビュートに項目番号+2列目のデータで命名する
  set ocidRowStrings to (ocidEditLineArray's objectAtIndex:(itemIntNo))
  set ocidRowArray to (ocidRowStrings's componentsSeparatedByString:("\t"))
  #ここが行番号
  set strRowNo to (ocidRowArray's objectAtIndex:(0)) as text
  #ここが2列目
  set strRowName to (ocidRowArray's objectAtIndex:(1)) as text
  #データセット名にする
  set strSetName to (strRowNo & ":" & strRowName) as text
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("dataSetName") stringValue:(strSetName))
(ocidDataSetElement's addAttribute:(ocidAddNode))
  #【4−2】の子要素の生成
  set numCntFirstRowArray to (count of ocidFirstRowArray) as integer
  repeat with itemIntNo from 0 to (numCntFirstRowArray - 1) by 1
    set ocidFirstRowValue to (ocidFirstRowArray's objectAtIndex:(itemIntNo))
    set ocidDataSetChildElement to (refMe's NSXMLElement's elementWithName:(ocidFirstRowValue))
    #Pパラグラフのエレメントのテキストの値として生成する
    set strSetRowItem to (ocidRowArray's objectAtIndex:(itemIntNo))
    set ocidPElement to (refMe's NSXMLElement's elementWithName:("p"))
    #テキストの値を入れる
(ocidPElement's setStringValue:(strSetRowItem))
(ocidDataSetChildElement's addChild:(ocidPElement))
(ocidDataSetElement's addChild:(ocidDataSetChildElement))
  end repeat
(ocidDataSetsElement's addChild:(ocidDataSetElement))
end repeat

#【3−1】のvariablesを【2】のvariableSetにセット
ocidVsetElement's addChild:(ocidVariablesElement)
#【4−1】DataSetsを【2】のvariableSetにセット
ocidVsetElement's addChild:(ocidDataSetsElement)
#【2】variableSetを【1】のvariableSetsにセット
ocidVsetsElement's addChild:(ocidVsetElement)
#【1】のvariableSetsをROOTにセット
ocidRootElement's addChild:(ocidVsetsElement)
#
################################
###保存
ocidXMLDoc's setRootElement:(ocidRootElement)
###NSXMLNodePrettyPrint形式でデータにして
###NSXMLNodeCompactEmptyElementにすると少しデータサイズが節約できる
set ocidXMLdata to ocidXMLDoc's XMLDataWithOptions:(refMe's NSXMLNodeExpandEmptyElement)
###データをテキストに戻して
set ocidXMLString to refMe's NSString's alloc()'s initWithData:(ocidXMLdata) encoding:(refMe's NSUTF8StringEncoding)
###改行コードをWindowsに
set ocidSaveStrings to ocidXMLString's stringByReplacingOccurrencesOfString:(ocidLFChar) withString:(ocidCRLFChar)
###保存
set listDone to (ocidSaveStrings's writeToURL:(ocidSaveFilePathURL) atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))

return




|

[XML]XML編集

1:SystemEventを利用する
2:AppleScript ObjectiveCを利用する
3:コマンドラインツールを利用する
4:あくまでもテキストとして編集してしまう



1:SystemEventを利用する
[System Events]RSSを取得してテキスト表示する
https://quicktimer.cocolog-nifty.com/icefloe/2024/01/post-3a79ab.html
[XML]シンプルな構造のXMLの処理 その1
https://quicktimer.cocolog-nifty.com/icefloe/2022/05/post-c1a917.html
[XML]シンプルな構造のXMLの処理 その2
https://quicktimer.cocolog-nifty.com/icefloe/2022/05/post-c38bdb.html
2:AppleScript ObjectiveCを利用する
A:XMLとして処理する(通常はこちら)
B:Dictionaryにして処理する(Plistがこれ)
3:コマンドラインツールを利用する
A:Plistなら「/usr/bin/defaults」や『/usr/libexec/PlistBuddy』を利用する
B:通常XMLなら『/usr/bin/xmllint』を利用する
4:あくまでもテキストとして編集してしまう
ケースによるとは思いますが、NGではないので、テキストとしてやっつけるのもアリ



1:XML
2:RSS
3:OPML
4:SVG
5:PLIST
6:TTML
7:HTML
8:EPUB


1:XML
カテゴリ
https://quicktimer.cocolog-nifty.com/icefloe/cat76050243/index.html
2:RSS
カテゴリ
https://quicktimer.cocolog-nifty.com/icefloe/cat76057197/index.html
3:OPML
[OPML]値の取得と表示(feedly.comのエキスポートしたOPML)
https://quicktimer.cocolog-nifty.com/icefloe/2023/09/post-83af41.html
[OPML]Adobe Community Opml 全カテゴリ
https://quicktimer.cocolog-nifty.com/icefloe/2024/01/post-0ea28a.html
[Opml]YahooNewsのRSSのOPML(入力にJSONを使う)
https://quicktimer.cocolog-nifty.com/icefloe/2024/01/post-48ab6a.html
[OPML]NHKのRSSをOPMLにする(入力にレコードを使う)
https://quicktimer.cocolog-nifty.com/icefloe/2024/01/post-9d6fce.html
4:SVG
カテゴリ
https://quicktimer.cocolog-nifty.com/icefloe/cat76056528/index.html
[XML]XMLとしてSVGを生成する
https://quicktimer.cocolog-nifty.com/icefloe/2024/01/post-b107e9.html
5:PLIST
カテゴリ
https://quicktimer.cocolog-nifty.com/icefloe/cat76047482/index.html
[XML]PlistをXMLとして作成する
https://quicktimer.cocolog-nifty.com/icefloe/2023/12/post-06637b.html
[Webloc]ロケーションファイル(com.apple.generic-internet-location)もPLIST
インターネット・ロケーションファイルまとめ
https://quicktimer.cocolog-nifty.com/icefloe/2024/02/post-e0ec42.html
6:TTML
カテゴリ
https://quicktimer.cocolog-nifty.com/icefloe/cat76053036/index.html
[TTML]テキストデータからTTMLを作成する
https://quicktimer.cocolog-nifty.com/icefloe/2024/01/post-1976ff.html
7:HTML
カテゴリ
https://quicktimer.cocolog-nifty.com/icefloe/cat76054619/index.html
[NSXMLDocument]XMLからHTMLを生成する
https://quicktimer.cocolog-nifty.com/icefloe/2024/01/post-997c28.html
8:EPUB
カテゴリ
https://quicktimer.cocolog-nifty.com/icefloe/cat76056702/index.html
[ドロップレット]EPUB用zip圧縮(半角スペース対応)
https://quicktimer.cocolog-nifty.com/icefloe/2023/11/post-c6729c.html


保存
A:データとして保存する
XMLDataWithOptionsからwriteToURL:options:errorで保存する
https://quicktimer.cocolog-nifty.com/icefloe/2023/12/post-06637b.html
B:テキストで保存する(改行コードを指定したい場合)
XMLStringでテキストにした後で 改行コードを付与して
writeToURL:atomically:encoding:error:で保存する
https://quicktimer.cocolog-nifty.com/icefloe/2023/11/post-86e929.html




ツール類
Visual Studio Code
https://code.visualstudio.com/
Visual Studio Code用機能拡張 
XML Tools
https://marketplace.visualstudio.com/items?itemName=DotJoshJohnson.xml
Red HatのXMLはソースの閲覧には良いが 編集には向かない(執筆時点)
https://marketplace.visualstudio.com/items?itemName=redhat.vscode-xml


BBEdit
無料ではないが、XMLのカラーリングが良くできている
https://www.barebones.com/products/bbedit/

Screen_20240210121801

|

フォルダ内のファイルリストをHTMLで作成する(最下層まで)画像のCMYK判定修正


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

#!/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 framework "UniformTypeIdentifiers"
use scripting additions
property refMe : a reference to current application


###Wクリックで起動した場合
on run
  ###ダイアログを前面に出す
  tell current application
    set strName to name as text
  end tell
  ####スクリプトメニューから実行したら
  if strName is "osascript" then
    tell application "Finder" to activate
  else
    tell current application to activate
  end if
  ##デフォルト=デスクトップ
  set appFileManager to refMe's NSFileManager's defaultManager()
  set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
  set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
  set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias
  set strPromptText to "フォルダをえらんでください"
  set strMesText to "フォルダをえらんでください"
  try
    set aliasDirPath to (choose folder strMesText with prompt strPromptText default location aliasDesktopDirPath with invisibles and showing package contents without multiple selections allowed) as alias
  on error
log "エラーしました"
return "エラーしました"
  end try
open aliasDirPath
end run


on open aliasDirPath
  ##パス
  set aliasDirPath to aliasDirPath as alias
  set strDirPath to (POSIX path of aliasDirPath) as text
  set ocidDirPathStr to refMe's NSString's stringWithString:(strDirPath)
  set ocidDirPath to ocidDirPathStr's stringByStandardizingPath()
  set ocidDirPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDirPath)
  set strDirPathURL to ocidDirPathURL's absoluteString() as text
  ##フォルダ判定
  set listBoole to (ocidDirPathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error|:(reference))
  set boolIsDir to (item 2 of listBoole) as boolean
  if boolIsDir is false then
return "フォルダ以外です処理を終了します"
  end if
  ##フォルダ名→保存先HTMLパス
  set ocidDirName to ocidDirPathURL's lastPathComponent()
  set ocidSaveFileName to refMe's NSMutableString's alloc()'s initWithCapacity:0
ocidSaveFileName's appendString:("_ファイルリスト")
ocidSaveFileName's appendString:(ocidDirName)
  set ocidBaseFilePathURL to ocidDirPathURL's URLByAppendingPathComponent:(ocidSaveFileName)
  set ocidSaveFilePathURL to ocidBaseFilePathURL's URLByAppendingPathExtension:("html")
  
  ##ファイルの各種プロパティを取得
  set appFileManager to refMe's NSFileManager's defaultManager()
  set ocidOption to refMe's NSDirectoryEnumerationSkipsHiddenFiles
  set ocidPropertieArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
ocidPropertieArray's addObject:(refMe's NSURLPathKey)
ocidPropertieArray's addObject:(refMe's NSURLFileSizeKey)
ocidPropertieArray's addObject:(refMe's NSURLCreationDateKey)
ocidPropertieArray's addObject:(refMe's NSURLContentModificationDateKey)
ocidPropertieArray's addObject:(refMe's NSURLNameKey)
ocidPropertieArray's addObject:(refMe's NSURLContentTypeKey)
ocidPropertieArray's addObject:(refMe's NSURLFileAllocatedSizeKey)
ocidPropertieArray's addObject:(refMe's NSURLIsRegularFileKey)
  ########################################
  ##コンテンツの収集 A 第一階層のみの場合
  ########################################
  (*
set listResponse to (appFileManager's contentsOfDirectoryAtURL:(ocidDirPathURL) includingPropertiesForKeys:(ocidPropertieArray) options:(ocidOption) |error|:(reference))
set ocidFilePathURLArray to item 1 of listResponse
#パスリストをファイル名でソート並び替え
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("lastPathComponent") ascending:(yes) selector:("localizedStandardCompare:")
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidFilePathURLArray's sortedArrayUsingDescriptors:(ocidDescriptorArray)
*)
  ########################################
  ##コンテンツの収集 B 最下層までの場合
  ########################################
  set ocidEmuDict to appFileManager's enumeratorAtURL:(ocidDirPathURL) includingPropertiesForKeys:(ocidPropertieArray) options:(ocidOption) errorHandler:(reference)
  set ocidEmuFileURLArray to ocidEmuDict's allObjects()
  set ocidFilePathURLAllArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0)
ocidFilePathURLAllArray's addObjectsFromArray:(ocidEmuFileURLArray)
  #
  set ocidFilePathURLArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0)
  repeat with itemFilePathURL in ocidFilePathURLAllArray
    set listResult to (itemFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsRegularFileKey) |error|:(reference))
    set boolIsRegularFileKey to item 2 of listResult
    if boolIsRegularFileKey is (refMe's NSNumber's numberWithBool:true) then
      ####リストにする
(ocidFilePathURLArray's addObject:(itemFilePathURL))
    end if
  end repeat
  #パスリストをファイル名でソート並び替え absoluteString localizedStandardCompare
  set ocidDescriptor to (refMe's NSSortDescriptor's sortDescriptorWithKey:"path" ascending:(true) selector:"localizedStandardCompare:")
  set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
  set ocidSortedArray to ocidFilePathURLArray's sortedArrayUsingDescriptors:(ocidDescriptorArray)
  
  ##############################
  # XML 生成開始
  ##############################
  #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)
  # XML主要部分を生成
  set ocidRootElement to doMakeRootElement()
  #ボディエレメント
  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)
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)
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)
  #リンク付与(不要なら削除可)
  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))
ocidFooterElement's addChild:(ocidAElement)
ocidBodyElement's addChild:(ocidFooterElement)
  ##############################
  # TABLE コンテンツ部分生成開始
  ##############################
  #テーブル部生成開始
  set ocidTableElement to refMe's NSXMLElement's elementWithName:("table")
  #【caption】
  set ocidCaptionElement to refMe's NSXMLElement's elementWithName:("caption")
ocidCaptionElement's setStringValue:("【ファイルリスト】: 行番号に※表示はCMYKデータ(PDFを除く)")
ocidTableElement's addChild:(ocidCaptionElement)
  #【colgroup】
  set ocidColgroupElement to refMe's NSXMLElement's elementWithName:("colgroup")
  #テーブルのタイトル部
  set listColName to {"行番号", "ファイル名", "サイズ", "種類", "作成日", "修正日"} as list
  #タイトル部の数だけ繰り返し
  repeat with itemColName in listColName
    #【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 listColName
    #ここは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))
    #値を入れる
(ocidAddElement's setStringValue:(itemColName))
    #TH→TRにセット
(ocidTrElement's addChild:(ocidAddElement))
  end repeat
  #TRをTHEADにセット
ocidTheadElement's addChild:(ocidTrElement)
  #THEADをテーブルにセット
ocidTableElement's addChild:(ocidTheadElement)
  #【tbody】
  set ocidTbodyElement to refMe's NSXMLElement's elementWithName:("tbody")
  #行番号
  set numCntLineNo to 1 as integer
  #合計ファイルサイズ用
  set numAllFileSize to 0 as integer
  #ファイルのパスの数だけ繰り返し
  repeat with itemFilePathURL in ocidSortedArray
    ##UTIを求めて
    set listResourceValue to (itemFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error|:(reference))
    set ocidContentType to (item 2 of listResourceValue)
    set strUTI to (ocidContentType's identifier) as text
    set strUTILIST to "public.jpeg, com.adobe.photoshop-image,public.tiff,com.adobe.pd" as text
    if strUTILIST contains strUTI then
log "CMYKチェック対象ファイル"
      set ocidReadImage to (refMe's NSImage's alloc()'s initWithContentsOfURL:(itemFilePathURL))
      set ocidImageRepArray to ocidReadImage's representations()
      set ocidImageRep to ocidImageRepArray's firstObject()
      set ocidColorSpace to ocidImageRep's colorSpace()
      #シンプルにコンポーネント番号で判定する場合
      # 1 = GS 3=RGB 4=CMYK
      set numColorCompNo to ocidColorSpace's numberOfColorComponents as integer
      #colorSpaceModelで判定したい場合
      # set ocidColorModel to ocidColorSpace's colorSpaceModel()
      # if ocidColorModel = (refMe's NSColorSpaceModelCMYK) then
log numColorCompNo
      if numColorCompNo = 4 then
log "CMYYKデータです"
        set ocidReadImage to (missing value)
        set ocidImageRep to (missing value)
        set boolCMYK to true as boolean
      else
        set boolCMYK to false as boolean
      end if
    else
      set boolCMYK to false as boolean
    end if
    ##
    #ファイルのリソースを取得
    set listResponse to (itemFilePathURL's resourceValuesForKeys:(ocidPropertieArray) |error|:(reference))
    set ocidValueDict to (item 1 of listResponse)
    #TRの開始
    set ocidTrElement to (refMe's NSXMLElement's elementWithName:("tr"))
    #【行番号】をTHでセット
    set strZeroSupp to ("00") as text
    set strZeroSupp to ("00" & numCntLineNo) as text
    set strLineNO to (text -3 through -1 of strZeroSupp) as text
    set ocidThElement to (refMe's NSXMLElement's elementWithName:("th"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("行番号"))
(ocidThElement's addAttribute:(ocidAddNode))
    if boolCMYK is true then
      set strSetLineNO to (strLineNO & "*") as text
    else if boolCMYK is false then
      set strSetLineNO to (strLineNO) as text
    end if
(ocidThElement's setStringValue:(strSetLineNO))
(ocidTrElement's addChild:(ocidThElement))
    #【ファイル名】をTDでセット
    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLNameKey))
    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("ファイル名"))
(ocidTdElement's addAttribute:(ocidAddNode))
    ####
    set itemFilePath to itemFilePathURL's |path|
    set ocidRange to (itemFilePath's rangeOfString:(ocidDirPath))
    set numLength to (|length| of ocidRange) + 1 as integer
    set ocidRlativePath to (itemFilePath's substringFromIndex:(numLength))
    
    #↑のTDの内容=ファイル名にリンクを付与
    set ocidAElement to (refMe's NSXMLElement's elementWithName:("a"))
    set strHref to ("./" & ocidRlativePath) as text
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:(strHref))
(ocidAElement's addAttribute:(ocidAddNode))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank"))
(ocidAElement's addAttribute:(ocidAddNode))
    set strHref to (ocidRlativePath) as text
(ocidAElement's setStringValue:(strHref))
    #リンクをTDにセット
(ocidTdElement's addChild:(ocidAElement))
    #TDをTRにセット
(ocidTrElement's addChild:(ocidTdElement))
    #【ファイルサイズ】TD
    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLFileSizeKey))
    #ファイルサイズの合計に加算
    set numAllFileSize to (numAllFileSize + (ocidValue as integer))
    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("ファイルサイズ"))
(ocidTdElement's addAttribute:(ocidAddNode))
    #単位による分岐
    set numFileSize to ocidValue as integer
    if (numFileSize) < 102400 then
      set numValue to (numFileSize / 1024) as integer
      set strValue to numValue & " kb" as text
    else if (numFileSize) > (10240 * 100000) then
      set numValue to (numFileSize / (1024 * 1000000))
      set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
(ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle))
(ocidFormatter's setMinimumFractionDigits:(2))
(ocidFormatter's setMaximumFractionDigits:(2))
      set strValue to (ocidFormatter's stringFromNumber:(numValue)) as text
      set strValue to strValue & " gb" as text
    else
      set numValue to (numFileSize / (1024 * 1000))
      set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
(ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle))
(ocidFormatter's setMinimumFractionDigits:(2))
(ocidFormatter's setMaximumFractionDigits:(2))
      set strValue to (ocidFormatter's stringFromNumber:(numValue)) as text
      set strValue to strValue & " mb" as text
    end if
(ocidTdElement's setStringValue:(strValue))
(ocidTrElement's addChild:(ocidTdElement))
    #【種類】TDでセット
    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLContentTypeKey))'s localizedDescription()
    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("種類"))
(ocidTdElement's addAttribute:(ocidAddNode))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("class") stringValue:("kind_string"))
(ocidTdElement's addAttribute:(ocidAddNode))
(ocidTdElement's setStringValue:(ocidValue))
(ocidTrElement's addChild:(ocidTdElement))
    #【作成日】TDでセット
    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLCreationDateKey))
    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("作成"))
(ocidTdElement's addAttribute:(ocidAddNode))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("class") stringValue:("date_string"))
(ocidTdElement's addAttribute:(ocidAddNode))
    set strDate to (ocidValue as date) as text
(ocidTdElement's setStringValue:(strDate))
(ocidTrElement's addChild:(ocidTdElement))
    #【修正日】TDでセット
    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLContentModificationDateKey))
    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("修正日"))
(ocidTdElement's addAttribute:(ocidAddNode))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("class") stringValue:("date_string"))
(ocidTdElement's addAttribute:(ocidAddNode))
    set strDate to (ocidValue as date) as text
(ocidTdElement's setStringValue:(strDate))
(ocidTrElement's addChild:(ocidTdElement))
    #出来上がったTRをTBODYにセット
(ocidTbodyElement's addChild:(ocidTrElement))
    #項目番号のカウントアップ
    set numCntLineNo to (numCntLineNo + 1) as integer
  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 listColName) 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))
  #
  if (numAllFileSize) < 102400 then
    set numValue to (numAllFileSize / 1024) as integer
    set strValue to "フォルダ合計:" & numValue & " kb" as text
  else if (numAllFileSize) > (10240 * 100000) then
    set numValue to (numAllFileSize / (10240 * 100000))
    set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
(ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle))
(ocidFormatter's setMinimumFractionDigits:(2))
(ocidFormatter's setMaximumFractionDigits:(2))
    set strValue to (ocidFormatter's stringFromNumber:(numValue)) as text
    set strValue to "フォルダ合計:" & strValue & " gb" as text
  else
    set numValue to (numAllFileSize / 1024000) as integer
    set strValue to "フォルダ合計:" & numValue & " mb" as text
  end if
ocidThElement's setStringValue:(strValue)
  
  #THをTRにセットして
ocidTrElement's addChild:(ocidThElement)
  #TRをTFOOTにセット
ocidTfootElement's addChild:(ocidTrElement)
  #TFOOTをテーブルにセット
ocidTableElement's addChild:(ocidTfootElement)
  # 出来上がったテーブルをArticleエレメントにセット
ocidArticleElement's addChild:(ocidTableElement)
  #
ocidRootElement's addChild:(ocidBodyElement)
  ##############################
  # TABLE
  ##############################
  #ROOTエレメントをXMLにセット
ocidXMLDoc's setRootElement:(ocidRootElement)
  #読み取りやすい表示
  set ocidXMLdata to ocidXMLDoc's XMLDataWithOptions:(refMe's NSXMLNodePrettyPrint)
  #保存
  set listDone to ocidXMLdata's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference)
  set ocidSaveFilePath to ocidSaveFilePathURL's |path|
  set ocidContainerDirPathURL to ocidSaveFilePathURL's URLByDeletingLastPathComponent()
  set ocidContainerDirPath to ocidContainerDirPathURL's |path|
  set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
  set boolDone to appSharedWorkspace's selectFile:(ocidSaveFilePath) inFileViewerRootedAtPath:(ocidContainerDirPath)
  set boolDone to appSharedWorkspace's openURL:(ocidSaveFilePathURL)
  if (boolDone as boolean) is false then
    #ファイルを開く
    set aliasSaveFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias
    tell application "Finder"
open file aliasSaveFilePath
    end tell
  end if
  ##処理終了
return "処理終了"
end open
##############################
# 基本的なHTMLの構造
##############################
to doMakeRootElement()
  #
  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:("ファイル一覧")
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; } 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)
  #
return ocidRootElement
end doMakeRootElement

|

フォルダ内のファイルリストを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 framework "AppKIt"
use framework "UniformTypeIdentifiers"
use scripting additions
property refMe : a reference to current application


###Wクリックで起動した場合
on run
  ###ダイアログを前面に出す
  tell current application
    set strName to name as text
  end tell
  ####スクリプトメニューから実行したら
  if strName is "osascript" then
    tell application "Finder" to activate
  else
    tell current application to activate
  end if
  ##デフォルト=デスクトップ
  set appFileManager to refMe's NSFileManager's defaultManager()
  set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
  set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
  set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias
  set strPromptText to "フォルダをえらんでください"
  set strMesText to "フォルダをえらんでください"
  try
    set aliasDirPath to (choose folder strMesText with prompt strPromptText default location aliasDesktopDirPath with invisibles and showing package contents without multiple selections allowed) as alias
  on error
log "エラーしました"
return "エラーしました"
  end try
open aliasDirPath
end run


on open aliasDirPath
  ##パス
  set aliasDirPath to aliasDirPath as alias
  set strDirPath to (POSIX path of aliasDirPath) as text
  set ocidDirPathStr to refMe's NSString's stringWithString:(strDirPath)
  set ocidDirPath to ocidDirPathStr's stringByStandardizingPath()
  set ocidDirPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDirPath)
  set strDirPathURL to ocidDirPathURL's absoluteString() as text
  ##フォルダ判定
  set listBoole to (ocidDirPathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error|:(reference))
  set boolIsDir to (item 2 of listBoole) as boolean
  if boolIsDir is false then
return "フォルダ以外です処理を終了します"
  end if
  ##フォルダ名→保存先HTMLパス
  set ocidDirName to ocidDirPathURL's lastPathComponent()
  set ocidSaveFileName to refMe's NSMutableString's alloc()'s initWithCapacity:0
ocidSaveFileName's appendString:("_ファイルリスト")
ocidSaveFileName's appendString:(ocidDirName)
  set ocidBaseFilePathURL to ocidDirPathURL's URLByAppendingPathComponent:(ocidSaveFileName)
  set ocidSaveFilePathURL to ocidBaseFilePathURL's URLByAppendingPathExtension:("html")
  
  ##ファイルの各種プロパティを取得
  set appFileManager to refMe's NSFileManager's defaultManager()
  set ocidOption to refMe's NSDirectoryEnumerationSkipsHiddenFiles
  set ocidPropertieArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
ocidPropertieArray's addObject:(refMe's NSURLPathKey)
ocidPropertieArray's addObject:(refMe's NSURLFileSizeKey)
ocidPropertieArray's addObject:(refMe's NSURLCreationDateKey)
ocidPropertieArray's addObject:(refMe's NSURLContentModificationDateKey)
ocidPropertieArray's addObject:(refMe's NSURLNameKey)
ocidPropertieArray's addObject:(refMe's NSURLContentTypeKey)
ocidPropertieArray's addObject:(refMe's NSURLFileAllocatedSizeKey)
ocidPropertieArray's addObject:(refMe's NSURLIsRegularFileKey)
  ########################################
  ##コンテンツの収集 A 第一階層のみの場合
  ########################################
  (*
set listResponse to (appFileManager's contentsOfDirectoryAtURL:(ocidDirPathURL) includingPropertiesForKeys:(ocidPropertieArray) options:(ocidOption) |error|:(reference))
set ocidFilePathURLArray to item 1 of listResponse
#パスリストをファイル名でソート並び替え
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("lastPathComponent") ascending:(yes) selector:("localizedStandardCompare:")
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidFilePathURLArray's sortedArrayUsingDescriptors:(ocidDescriptorArray)
*)
  ########################################
  ##コンテンツの収集 B 最下層までの場合
  ########################################
  set ocidEmuDict to appFileManager's enumeratorAtURL:(ocidDirPathURL) includingPropertiesForKeys:(ocidPropertieArray) options:(ocidOption) errorHandler:(reference)
  set ocidEmuFileURLArray to ocidEmuDict's allObjects()
  set ocidFilePathURLAllArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0)
ocidFilePathURLAllArray's addObjectsFromArray:(ocidEmuFileURLArray)
  #
  set ocidFilePathURLArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0)
  repeat with itemFilePathURL in ocidFilePathURLAllArray
    set listResult to (itemFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsRegularFileKey) |error|:(reference))
    set boolIsRegularFileKey to item 2 of listResult
    if boolIsRegularFileKey is (refMe's NSNumber's numberWithBool:true) then
      ####リストにする
(ocidFilePathURLArray's addObject:(itemFilePathURL))
    end if
  end repeat
  #パスリストをファイル名でソート並び替え absoluteString localizedStandardCompare
  set ocidDescriptor to (refMe's NSSortDescriptor's sortDescriptorWithKey:"path" ascending:(true) selector:"localizedStandardCompare:")
  set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
  set ocidSortedArray to ocidFilePathURLArray's sortedArrayUsingDescriptors:(ocidDescriptorArray)
  
  ##############################
  # XML 生成開始
  ##############################
  #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)
  # XML主要部分を生成
  set ocidRootElement to doMakeRootElement()
  #ボディエレメント
  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)
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)
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)
  #リンク付与(不要なら削除可)
  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))
ocidFooterElement's addChild:(ocidAElement)
ocidBodyElement's addChild:(ocidFooterElement)
  ##############################
  # TABLE コンテンツ部分生成開始
  ##############################
  #テーブル部生成開始
  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")
  #テーブルのタイトル部
  set listColName to {"行番号", "ファイル名", "サイズ", "種類", "作成日", "修正日"} as list
  #タイトル部の数だけ繰り返し
  repeat with itemColName in listColName
    #【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 listColName
    #ここは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))
    #値を入れる
(ocidAddElement's setStringValue:(itemColName))
    #TH→TRにセット
(ocidTrElement's addChild:(ocidAddElement))
  end repeat
  #TRをTHEADにセット
ocidTheadElement's addChild:(ocidTrElement)
  #THEADをテーブルにセット
ocidTableElement's addChild:(ocidTheadElement)
  #【tbody】
  set ocidTbodyElement to refMe's NSXMLElement's elementWithName:("tbody")
  #行番号
  set numCntLineNo to 1 as integer
  #合計ファイルサイズ用
  set numAllFileSize to 0 as integer
  #ファイルのパスの数だけ繰り返し
  repeat with itemFilePathURL in ocidSortedArray
    #ファイルのリソースを取得
    set listResponse to (itemFilePathURL's resourceValuesForKeys:(ocidPropertieArray) |error|:(reference))
    set ocidValueDict to (item 1 of listResponse)
    #TRの開始
    set ocidTrElement to (refMe's NSXMLElement's elementWithName:("tr"))
    #【行番号】をTHでセット
    set strZeroSupp to ("00") as text
    set strZeroSupp to ("00" & numCntLineNo) as text
    set strLineNO to (text -3 through -1 of strZeroSupp) as text
    set ocidThElement to (refMe's NSXMLElement's elementWithName:("th"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("行番号"))
(ocidThElement's addAttribute:(ocidAddNode))
(ocidThElement's setStringValue:(strLineNO))
(ocidTrElement's addChild:(ocidThElement))
    #【ファイル名】をTDでセット
    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLNameKey))
    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("ファイル名"))
(ocidTdElement's addAttribute:(ocidAddNode))
    ####
    set itemFilePath to itemFilePathURL's |path|
    set ocidRange to (itemFilePath's rangeOfString:(ocidDirPath))
    set numLength to (|length| of ocidRange) + 1 as integer
    set ocidRlativePath to (itemFilePath's substringFromIndex:(numLength))
    
    #↑のTDの内容=ファイル名にリンクを付与
    set ocidAElement to (refMe's NSXMLElement's elementWithName:("a"))
    set strHref to ("./" & ocidRlativePath) as text
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:(strHref))
(ocidAElement's addAttribute:(ocidAddNode))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank"))
(ocidAElement's addAttribute:(ocidAddNode))
    set strHref to (ocidRlativePath) as text
(ocidAElement's setStringValue:(strHref))
    #リンクをTDにセット
(ocidTdElement's addChild:(ocidAElement))
    #TDをTRにセット
(ocidTrElement's addChild:(ocidTdElement))
    #【ファイルサイズ】TD
    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLFileSizeKey))
    #ファイルサイズの合計に加算
    set numAllFileSize to (numAllFileSize + (ocidValue as integer))
    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("ファイルサイズ"))
(ocidTdElement's addAttribute:(ocidAddNode))
    #単位による分岐
    set numFileSize to ocidValue as integer
    if (numFileSize) < 102400 then
      set numValue to (numFileSize / 1024) as integer
      set strValue to numValue & " kb" as text
    else if (numFileSize) > (10240 * 100000) then
      set numValue to (numFileSize / (1024 * 1000000))
      set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
(ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle))
(ocidFormatter's setMinimumFractionDigits:(2))
(ocidFormatter's setMaximumFractionDigits:(2))
      set strValue to (ocidFormatter's stringFromNumber:(numValue)) as text
      set strValue to strValue & " gb" as text
    else
      set numValue to (numFileSize / (1024 * 1000))
      set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
(ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle))
(ocidFormatter's setMinimumFractionDigits:(2))
(ocidFormatter's setMaximumFractionDigits:(2))
      set strValue to (ocidFormatter's stringFromNumber:(numValue)) as text
      set strValue to strValue & " mb" as text
    end if
(ocidTdElement's setStringValue:(strValue))
(ocidTrElement's addChild:(ocidTdElement))
    #【種類】TDでセット
    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLContentTypeKey))'s localizedDescription()
    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("種類"))
(ocidTdElement's addAttribute:(ocidAddNode))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("class") stringValue:("kind_string"))
(ocidTdElement's addAttribute:(ocidAddNode))
(ocidTdElement's setStringValue:(ocidValue))
(ocidTrElement's addChild:(ocidTdElement))
    #【作成日】TDでセット
    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLCreationDateKey))
    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("作成"))
(ocidTdElement's addAttribute:(ocidAddNode))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("class") stringValue:("date_string"))
(ocidTdElement's addAttribute:(ocidAddNode))
    set strDate to (ocidValue as date) as text
(ocidTdElement's setStringValue:(strDate))
(ocidTrElement's addChild:(ocidTdElement))
    #【修正日】TDでセット
    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLContentModificationDateKey))
    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("修正日"))
(ocidTdElement's addAttribute:(ocidAddNode))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("class") stringValue:("date_string"))
(ocidTdElement's addAttribute:(ocidAddNode))
    set strDate to (ocidValue as date) as text
(ocidTdElement's setStringValue:(strDate))
(ocidTrElement's addChild:(ocidTdElement))
    #出来上がったTRをTBODYにセット
(ocidTbodyElement's addChild:(ocidTrElement))
    #項目番号のカウントアップ
    set numCntLineNo to (numCntLineNo + 1) as integer
  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 listColName) 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))
  #
  if (numAllFileSize) < 102400 then
    set numValue to (numAllFileSize / 1024) as integer
    set strValue to "フォルダ合計:" & numValue & " kb" as text
  else if (numAllFileSize) > (10240 * 100000) then
    set numValue to (numAllFileSize / (10240 * 100000))
    set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
(ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle))
(ocidFormatter's setMinimumFractionDigits:(2))
(ocidFormatter's setMaximumFractionDigits:(2))
    set strValue to (ocidFormatter's stringFromNumber:(numValue)) as text
    set strValue to "フォルダ合計:" & strValue & " gb" as text
  else
    set numValue to (numAllFileSize / 1024000) as integer
    set strValue to "フォルダ合計:" & numValue & " mb" as text
  end if
ocidThElement's setStringValue:(strValue)
  
  #THをTRにセットして
ocidTrElement's addChild:(ocidThElement)
  #TRをTFOOTにセット
ocidTfootElement's addChild:(ocidTrElement)
  #TFOOTをテーブルにセット
ocidTableElement's addChild:(ocidTfootElement)
  # 出来上がったテーブルをArticleエレメントにセット
ocidArticleElement's addChild:(ocidTableElement)
  #
ocidRootElement's addChild:(ocidBodyElement)
  ##############################
  # TABLE
  ##############################
  #ROOTエレメントをXMLにセット
ocidXMLDoc's setRootElement:(ocidRootElement)
  #読み取りやすい表示
  set ocidXMLdata to ocidXMLDoc's XMLDataWithOptions:(refMe's NSXMLNodePrettyPrint)
  #保存
  set listDone to ocidXMLdata's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference)
  set ocidSaveFilePath to ocidSaveFilePathURL's |path|
  set ocidContainerDirPathURL to ocidSaveFilePathURL's URLByDeletingLastPathComponent()
  set ocidContainerDirPath to ocidContainerDirPathURL's |path|
  set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
  set boolDone to appSharedWorkspace's selectFile:(ocidSaveFilePath) inFileViewerRootedAtPath:(ocidContainerDirPath)
  set boolDone to appSharedWorkspace's openURL:(ocidSaveFilePathURL)
  if (boolDone as boolean) is false then
    #ファイルを開く
    set aliasSaveFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias
    tell application "Finder"
open file aliasSaveFilePath
    end tell
  end if
  ##処理終了
return "処理終了"
end open
##############################
# 基本的なHTMLの構造
##############################
to doMakeRootElement()
  #
  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:("ファイル一覧")
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; } 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)
  #
return ocidRootElement
end doMakeRootElement

|

フォルダ内のファイルリストをHTMLで作成する(第一階層まで)

ダウンロード - makefilelist2html.zip


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

#!/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 framework "UniformTypeIdentifiers"
use scripting additions
property refMe : a reference to current application


###Wクリックで起動した場合
on run
  ###ダイアログを前面に出す
  tell current application
    set strName to name as text
  end tell
  ####スクリプトメニューから実行したら
  if strName is "osascript" then
    tell application "Finder" to activate
  else
    tell current application to activate
  end if
  ##デフォルト=デスクトップ
  set appFileManager to refMe's NSFileManager's defaultManager()
  set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
  set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
  set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias
  set strPromptText to "フォルダをえらんでください"
  set strMesText to "フォルダをえらんでください"
  try
    set aliasDirPath to (choose folder strMesText with prompt strPromptText default location aliasDesktopDirPath with invisibles and showing package contents without multiple selections allowed) as alias
  on error
log "エラーしました"
return "エラーしました"
  end try
open aliasDirPath
end run


on open aliasDirPath
  ##パス
  set aliasDirPath to aliasDirPath as alias
  set strDirPath to (POSIX path of aliasDirPath) as text
  set ocidDirPathStr to refMe's NSString's stringWithString:(strDirPath)
  set ocidDirPath to ocidDirPathStr's stringByStandardizingPath()
  set ocidDirPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDirPath)
  set strDirPathURL to ocidDirPathURL's absoluteString() as text
  ##フォルダ判定
  set listBoole to (ocidDirPathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error|:(reference))
  set boolIsDir to (item 2 of listBoole) as boolean
  if boolIsDir is false then
return "フォルダ以外です処理を終了します"
  end if
  ##フォルダ名→保存先HTMLパス
  set ocidDirName to ocidDirPathURL's lastPathComponent()
  set ocidSaveFileName to refMe's NSMutableString's alloc()'s initWithCapacity:0
ocidSaveFileName's appendString:("_ファイルリスト")
ocidSaveFileName's appendString:(ocidDirName)
  set ocidBaseFilePathURL to ocidDirPathURL's URLByAppendingPathComponent:(ocidSaveFileName)
  set ocidSaveFilePathURL to ocidBaseFilePathURL's URLByAppendingPathExtension:("html")
  ##ファイルの各種プロパティを取得
  set appFileManager to refMe's NSFileManager's defaultManager()
  set ocidOption to refMe's NSDirectoryEnumerationSkipsHiddenFiles
  set ocidPropertieArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
ocidPropertieArray's addObject:(refMe's NSURLPathKey)
ocidPropertieArray's addObject:(refMe's NSURLFileSizeKey)
ocidPropertieArray's addObject:(refMe's NSURLCreationDateKey)
ocidPropertieArray's addObject:(refMe's NSURLContentModificationDateKey)
ocidPropertieArray's addObject:(refMe's NSURLNameKey)
ocidPropertieArray's addObject:(refMe's NSURLContentTypeKey)
ocidPropertieArray's addObject:(refMe's NSURLFileAllocatedSizeKey)
  set listResponse to (appFileManager's contentsOfDirectoryAtURL:(ocidDirPathURL) includingPropertiesForKeys:(ocidPropertieArray) options:(ocidOption) |error|:(reference))
  set ocidFilePathURLArray to item 1 of listResponse
  #パスリストをファイル名でソート並び替え
  set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("lastPathComponent") ascending:(yes) selector:("localizedStandardCompare:")
  set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
  set ocidSortedArray to ocidFilePathURLArray's sortedArrayUsingDescriptors:(ocidDescriptorArray)
  ##############################
  # XML 生成開始
  ##############################
  #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)
  # XML主要部分を生成
  set ocidRootElement to doMakeRootElement()
  #ボディエレメント
  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)
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)
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)
  #リンク付与(不要なら削除可)
  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))
ocidFooterElement's addChild:(ocidAElement)
ocidBodyElement's addChild:(ocidFooterElement)
  ##############################
  # TABLE コンテンツ部分生成開始
  ##############################
  #テーブル部生成開始
  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")
  #テーブルのタイトル部
  set listColName to {"行番号", "ファイル名", "サイズ", "種類", "作成日", "修正日"} as list
  #タイトル部の数だけ繰り返し
  repeat with itemColName in listColName
    #【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 listColName
    #ここは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))
    #値を入れる
(ocidAddElement's setStringValue:(itemColName))
    #TH→TRにセット
(ocidTrElement's addChild:(ocidAddElement))
  end repeat
  #TRをTHEADにセット
ocidTheadElement's addChild:(ocidTrElement)
  #THEADをテーブルにセット
ocidTableElement's addChild:(ocidTheadElement)
  #【tbody】
  set ocidTbodyElement to refMe's NSXMLElement's elementWithName:("tbody")
  #行番号
  set numCntLineNo to 1 as integer
  #合計ファイルサイズ用
  set numAllFileSize to 0 as integer
  #ファイルのパスの数だけ繰り返し
  repeat with itemFilePathURl in ocidSortedArray
    #ファイルのリソースを取得
    set listResponse to (itemFilePathURl's resourceValuesForKeys:(ocidPropertieArray) |error|:(reference))
    set ocidValueDict to (item 1 of listResponse)
    #TRの開始
    set ocidTrElement to (refMe's NSXMLElement's elementWithName:("tr"))
    #【行番号】をTHでセット
    set strZeroSupp to ("00") as text
    set strZeroSupp to ("00" & numCntLineNo) as text
    set strLineNO to (text -3 through -1 of strZeroSupp) as text
    set ocidThElement to (refMe's NSXMLElement's elementWithName:("th"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("行番号"))
(ocidThElement's addAttribute:(ocidAddNode))
(ocidThElement's setStringValue:(strLineNO))
(ocidTrElement's addChild:(ocidThElement))
    #【ファイル名】をTDでセット
    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLNameKey))
    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("ファイル名"))
(ocidTdElement's addAttribute:(ocidAddNode))
    #↑のTDの内容=ファイル名にリンクを付与
    set ocidAElement to (refMe's NSXMLElement's elementWithName:("a"))
    set strHref to ("./" & ocidValue) as text
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:(strHref))
(ocidAElement's addAttribute:(ocidAddNode))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank"))
(ocidAElement's addAttribute:(ocidAddNode))
(ocidAElement's setStringValue:(ocidValue))
    #リンクをTDにセット
(ocidTdElement's addChild:(ocidAElement))
    #TDをTRにセット
(ocidTrElement's addChild:(ocidTdElement))
    #【ファイルサイズ】TD
    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLFileSizeKey))
    #ファイルサイズの合計に加算
    set numAllFileSize to (numAllFileSize + (ocidValue as integer))
    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("ファイルサイズ"))
(ocidTdElement's addAttribute:(ocidAddNode))
    #単位による分岐
    set numFileSize to ocidValue as integer
    if (numFileSize) < 102400 then
      set numValue to (numFileSize / 1024) as integer
      set strValue to numValue & " kb" as text
    else if (numFileSize) > (10240 * 100000) then
      set numValue to (numFileSize / (10240 * 100000))
      set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
(ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle))
(ocidFormatter's setMinimumFractionDigits:(2))
(ocidFormatter's setMaximumFractionDigits:(2))
      set strValue to (ocidFormatter's stringFromNumber:(numValue)) as text
      set strValue to strValue & " gb" as text
    else
      set numValue to (numFileSize / 1024000) as integer
      set strValue to numValue & " mb" as text
    end if
(ocidTdElement's setStringValue:(strValue))
(ocidTrElement's addChild:(ocidTdElement))
    #【種類】TDでセット
    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLContentTypeKey))'s localizedDescription()
    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("種類"))
(ocidTdElement's addAttribute:(ocidAddNode))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("class") stringValue:("kind_string"))
(ocidTdElement's addAttribute:(ocidAddNode))
(ocidTdElement's setStringValue:(ocidValue))
(ocidTrElement's addChild:(ocidTdElement))
    #【作成日】TDでセット
    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLCreationDateKey))
    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("作成"))
(ocidTdElement's addAttribute:(ocidAddNode))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("class") stringValue:("date_string"))
(ocidTdElement's addAttribute:(ocidAddNode))
    set strDate to (ocidValue as date) as text
(ocidTdElement's setStringValue:(strDate))
(ocidTrElement's addChild:(ocidTdElement))
    #【修正日】TDでセット
    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLContentModificationDateKey))
    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("修正日"))
(ocidTdElement's addAttribute:(ocidAddNode))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("class") stringValue:("date_string"))
(ocidTdElement's addAttribute:(ocidAddNode))
    set strDate to (ocidValue as date) as text
(ocidTdElement's setStringValue:(strDate))
(ocidTrElement's addChild:(ocidTdElement))
    #出来上がったTRをTBODYにセット
(ocidTbodyElement's addChild:(ocidTrElement))
    #項目番号のカウントアップ
    set numCntLineNo to (numCntLineNo + 1) as integer
  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 listColName) 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))
  #
  if (numAllFileSize) < 102400 then
    set numValue to (numAllFileSize / 1024) as integer
    set strValue to "フォルダ合計:" & numValue & " kb" as text
  else if (numAllFileSize) > (10240 * 100000) then
    set numValue to (numAllFileSize / (10240 * 100000))
    set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
(ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle))
(ocidFormatter's setMinimumFractionDigits:(2))
(ocidFormatter's setMaximumFractionDigits:(2))
    set strValue to (ocidFormatter's stringFromNumber:(numValue)) as text
    set strValue to "フォルダ合計:" & strValue & " gb" as text
  else
    set numValue to (numAllFileSize / 1024000) as integer
    set strValue to "フォルダ合計:" & numValue & " mb" as text
  end if
ocidThElement's setStringValue:(strValue)
  
  #THをTRにセットして
ocidTrElement's addChild:(ocidThElement)
  #TRをTFOOTにセット
ocidTfootElement's addChild:(ocidTrElement)
  #TFOOTをテーブルにセット
ocidTableElement's addChild:(ocidTfootElement)
  # 出来上がったテーブルをArticleエレメントにセット
ocidArticleElement's addChild:(ocidTableElement)
  #
ocidRootElement's addChild:(ocidBodyElement)
  ##############################
  # TABLE
  ##############################
  #ROOTエレメントをXMLにセット
ocidXMLDoc's setRootElement:(ocidRootElement)
  #読み取りやすい表示
  set ocidXMLdata to ocidXMLDoc's XMLDataWithOptions:(refMe's NSXMLNodePrettyPrint)
  #保存
  set listDone to ocidXMLdata's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference)
  set ocidSaveFilePath to ocidSaveFilePathURL's |path|
  set ocidContainerDirPathURL to ocidSaveFilePathURL's URLByDeletingLastPathComponent()
  set ocidContainerDirPath to ocidContainerDirPathURL's |path|
  set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
  set boolDone to appSharedWorkspace's selectFile:(ocidSaveFilePath) inFileViewerRootedAtPath:(ocidContainerDirPath)
  set boolDone to appSharedWorkspace's openURL:(ocidSaveFilePathURL)
  if (boolDone as boolean) is false then
    #ファイルを開く
    set aliasSaveFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias
    tell application "Finder"
open file aliasSaveFilePath
    end tell
  end if
  ##処理終了
return "処理終了"
end open
##############################
# 基本的なHTMLの構造
##############################
to doMakeRootElement()
  #
  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:("ファイル一覧")
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 { 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; } 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)
  #
return ocidRootElement
end doMakeRootElement

|

[NSXMLDocument] NSXMLDocument で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 framework "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()


###【1】保存先ファイルパス
###デスクトップ
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
###保存ファイルURL
set strFileName to ("test.html") as text
set ocidSaveFilePathURL to ocidDesktopDirPathURL's URLByAppendingPathComponent:(strFileName)

###【2】XML生成
set ocidXMLDoc to refMe's NSXMLDocument's alloc()'s init()
(*
NSXMLDocumentXMLKind: XML形式
NSXMLDocumentXHTMLKind: XHTML形式の
NSXMLDocumentHTMLKind: HTML形式の
NSXMLDocumentTextKind: プレーンテキスト形式
*)
ocidXMLDoc's setDocumentContentKind:(refMe's NSXMLDocumentHTMLKind)


###【4】ROOTエレメントを生成
set ocidRootElement to refMe's NSXMLElement's elementWithName:("html")
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("lang") stringValue:("ja")
ocidRootElement's addAttribute:(ocidAddNode)

###【3】DTD生成
set ocidDTD to refMe's NSXMLDTD's alloc()'s init()
ocidDTD's setName:("html")
####【3-1】DTDをXMLにセットする
ocidXMLDoc's setDTD:(ocidDTD)

###【4】 head
set ocidHeadElement to refMe's NSXMLElement's elementWithName:("head")
#
set ocidAddElement to refMe's NSXMLElement's elementWithName:("title")
ocidAddElement's setStringValue:("ドキュメントのタイトル")
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)
###### viewport
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:("meta")
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("name") stringValue:("description")
ocidAddElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("説明")
ocidAddElement's addAttribute:(ocidAddNode)
ocidHeadElement's addChild:(ocidAddElement)
#####
set ocidAddElement to refMe's NSXMLElement's elementWithName:("link")
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("rel") stringValue:("icon")
ocidAddElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("href") stringValue:("https://quicktimer.cocolog-nifty.com//favicon.png")
ocidAddElement's addAttribute:(ocidAddNode)
ocidHeadElement's addChild:(ocidAddElement)
#
set ocidAddElement to refMe's NSXMLElement's elementWithName:("link")
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("rel") stringValue:("stylesheet")
ocidAddElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("href") stringValue:("https://quicktimer.cocolog-nifty.com/icefloe/styles.css")
ocidAddElement's addAttribute:(ocidAddNode)
ocidHeadElement's addChild:(ocidAddElement)
#####
set ocidAddElement to refMe's NSXMLElement's elementWithName:("style")
ocidAddElement's setStringValue:("body{margin:0;background-color: #FFFFFF;}")
ocidHeadElement's addChild:(ocidAddElement)
#
ocidRootElement's addChild:(ocidHeadElement)

###【5】 body
set ocidBodyElement to refMe's NSXMLElement's elementWithName:("body")
#【5−1】header
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)
ocidBodyElement's addChild:(ocidHeaderElement)
#【5−2】nav
set ocidNavElement to refMe's NSXMLElement's elementWithName:("nav")
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("nav")
ocidNavElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_nav")
ocidNavElement's addAttribute:(ocidAddNode)
ocidBodyElement's addChild:(ocidNavElement)
#【5−3】section
set ocidSectionElement to refMe's NSXMLElement's elementWithName:("section")
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("section")
ocidSectionElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_section")
ocidSectionElement's addAttribute:(ocidAddNode)
ocidBodyElement's addChild:(ocidSectionElement)
#【5−4】article
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 setStringValue:("美しい日本語")
ocidBodyElement's addChild:(ocidArticleElement)
#【5−5】footer
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)
ocidBodyElement's addChild:(ocidFooterElement)


#【5−8】Bodyエレメントをrootエレメントにセット
ocidRootElement's addChild:(ocidBodyElement)
#【5-9】ROOTエレメントXMLにセットする
ocidXMLDoc's setRootElement:(ocidRootElement)
###【6】XMLデータにして(ここでテキスト形式のXMLに)
set ocidXMLdata to ocidXMLDoc's XMLDataWithOptions:(refMe's NSXMLNodePrettyPrint)
###【7】ファイルに保存
set listDone to ocidXMLdata's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference)


return


|

[XML]XMLとしてSVGを生成する


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

#!/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 "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()


###設定
set strWidth to ("360") as text
set strHight to ("720") as text
set strText to ("美しい日本語") as text
set strFontSize to ("36") as text
#
set strTextRange to (strFontSize * (count of strText))

###【1】保存先ファイルパス
###デスクトップ
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
###保存ファイルURL
set strFileName to ("test.svg") as text
set ocidSaveFilePathURL to ocidDesktopDirPathURL's URLByAppendingPathComponent:(strFileName)

###【2】XML生成
set ocidXMLDoc to refMe's NSXMLDocument's alloc()'s init()
ocidXMLDoc's setVersion:("1.0")
ocidXMLDoc's setCharacterEncoding:("UTF-8")
ocidXMLDoc's setStandalone:("no")
(*
NSXMLDocumentXMLKind: XML形式
NSXMLDocumentXHTMLKind: XHTML形式の
NSXMLDocumentHTMLKind: HTML形式の
NSXMLDocumentTextKind: プレーンテキスト形式
*)
ocidXMLDoc's setDocumentContentKind:(refMe's NSXMLDocumentXMLKind)

###【3】DTD生成
set ocidDTD to refMe's NSXMLDTD's alloc()'s init()
ocidDTD's setName:("svg")
ocidDTD's setPublicID:("-//W3C//DTD SVG 1.1//EN")
ocidDTD's setSystemID:("http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd")
####【3-1】DTDをXMLにセットする
ocidXMLDoc's setDTD:(ocidDTD)

###【4】ROOTエレメントを生成
set ocidRootElement to refMe's NSXMLElement's elementWithName:("svg")
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("version") stringValue:("1.1")
ocidRootElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("xmlns") stringValue:("http://www.w3.org/2000/svg")
ocidRootElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("xmlns:xlink") stringValue:("http://www.w3.org/1999/xlink")
ocidRootElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("xml:space") stringValue:("preserve")
ocidRootElement's addAttribute:(ocidAddNode)
#背景色を指定する場合はここ
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("style") stringValue:("fill-rule:nonzero;clip-rule:evenodd;stroke-linecap:round;stroke-linejoin:round;")
ocidRootElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("width") stringValue:("" & strWidth & ".0px")
ocidRootElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("height") stringValue:("" & strHight & ".0px")
ocidRootElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("viewBox") stringValue:("0 0 " & strWidth & " " & strHight & "")
ocidRootElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("stroke-miterlimit") stringValue:("10")
ocidRootElement's addAttribute:(ocidAddNode)
###【4-1】defs ここは空で
set ocidDefsElement to refMe's NSXMLElement's elementWithName:("defs")
ocidRootElement's addChild:(ocidDefsElement)
###【4-2】path一番下のレイヤー
set ocidBackgroundElement to refMe's NSXMLElement's elementWithName:("path")
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("d") stringValue:("M0 0L" & strWidth & " 0L" & strWidth & " " & strHight & "L0 " & strHight & "L0 0Z")
ocidBackgroundElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("fill") stringValue:("#AAAAAA")
ocidBackgroundElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("fill-rule") stringValue:("nonzero")
ocidBackgroundElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("opacity") stringValue:("1")
ocidBackgroundElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("stroke") stringValue:("none")
ocidBackgroundElement's addAttribute:(ocidAddNode)
#【4-2】pathをrootエレメントにセット
ocidRootElement's addChild:(ocidBackgroundElement)
###【4-3】g
set ocidGElement to refMe's NSXMLElement's elementWithName:("g")
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("text")
ocidGElement's addAttribute:(ocidAddNode)
###【4-3-1】text
set ocidTextElement to refMe's NSXMLElement's elementWithName:("text")
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("fill") stringValue:("#000000")
ocidTextElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("font-family") stringValue:("mono")
ocidTextElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("font-size") stringValue:("" & strFontSize & "")
ocidTextElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("opacity") stringValue:("1")
ocidTextElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("stroke") stringValue:("none")
ocidTextElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("x") stringValue:("0")
ocidTextElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("y") stringValue:("0")
ocidTextElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("text-anchor") stringValue:("start")
ocidTextElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("transform") stringValue:("matrix(1 0 0 1 0 360)")
ocidTextElement's addAttribute:(ocidAddNode)
###【4-3-2】tspan
set ocidTSpanElement to refMe's NSXMLElement's elementWithName:("tspan")
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("x") stringValue:("0")
ocidTSpanElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("y") stringValue:("0")
ocidTSpanElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("textLength") stringValue:(strTextRange)
ocidTSpanElement's addAttribute:(ocidAddNode)
ocidTSpanElement's setStringValue:(strText)

#######
#【4-3-2】tspan を【4-3-1】textの子要素にセット
ocidTextElement's addChild:(ocidTSpanElement)
#【4-3-1】textを 【4-3】gの子要素にセット
ocidGElement's addChild:(ocidTextElement)
#【4-3】gをrootエレメントの子要素としてセット
ocidRootElement's addChild:(ocidGElement)
#【4-3】ROOTエレメントXMLにセットする
ocidXMLDoc's setRootElement:(ocidRootElement)
###【5】XMLデータにして(ここでテキスト形式のXMLに)
set ocidXMLdata to ocidXMLDoc's XMLDataWithOptions:(refMe's NSXMLNodePrettyPrint)
###【6】ファイルに保存
set listDone to ocidXMLdata's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference)


return


|

その他のカテゴリー

Acrobat Acrobat Annotation Acrobat Dialog Acrobat Form Acrobat JS Acrobat Open Acrobat Plugin Acrobat python Acrobat Reader Acrobat Sequ Acrobat Stamps Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin System Information Admin Tools Admin Users Admin Volumes Adobe Apple AppleScript AppleScript Guide AppleScript Accessibility AppleScript AppKit AppleScript Applications AppleScript AppStore AppleScript Archive AppleScript Attributes AppleScript Automator AppleScript AVAsset AppleScript AVconvert AppleScript AVFoundation AppleScript AVURLAsset AppleScript BackUp AppleScript Barcode AppleScript Bash AppleScript Basic AppleScript Basic Path AppleScript Bluetooth AppleScript BOX AppleScript Browser AppleScript Calendar AppleScript CD/DVD AppleScript Choose AppleScript Chrome AppleScript CIImage AppleScript CloudStorage AppleScript Color AppleScript com.apple.LaunchServices.OpenWith AppleScript Console AppleScript Contacts AppleScript CotEditor AppleScript CURL AppleScript current application AppleScript Date&Time AppleScript delimiters AppleScript Desktop AppleScript Device AppleScript Disk AppleScript do shell script AppleScript Dock AppleScript DropBox AppleScript eMail AppleScript Encode Decode AppleScript Error AppleScript EXIFData AppleScript ffmpeg AppleScript File AppleScript Finder AppleScript Firefox AppleScript Folder AppleScript Fonts AppleScript GIF AppleScript HTML AppleScript Icon AppleScript Illustrator AppleScript Image Events AppleScript Image2PDF AppleScript ImageOptim Applescript iWork AppleScript Javascript AppleScript Jedit AppleScript Json AppleScript Label AppleScript List AppleScript locationd AppleScript LRC AppleScript LSSharedFileList AppleScript m3u8 AppleScript MakePDF AppleScript Map AppleScript Math AppleScript Microsoft AppleScript Microsoft Edge AppleScript Microsoft Excel AppleScript Mobileconfig AppleScript Mouse AppleScript Movie AppleScript Music AppleScript NetWork AppleScript Notes AppleScript NSArray AppleScript NSBitmapImageRep AppleScript NSBundle AppleScript NSCFBoolean AppleScript NSCharacterSet AppleScript NSColor AppleScript NSColorList AppleScript NSData AppleScript NSDictionary AppleScript NSError AppleScript NSEvent AppleScript NSFileAttributes AppleScript NSFileManager AppleScript NSFont AppleScript NSFontManager AppleScript NSImage AppleScript NSIndex AppleScript NSKeyedArchiver AppleScript NSKeyedUnarchiver AppleScript NSLocale AppleScript NSMutableArray AppleScript NSMutableDictionary AppleScript NSMutableString AppleScript NSNotFound AppleScript NSNumber AppleScript NSOpenPanel AppleScript NSPasteboard AppleScript NSPDFImageRep AppleScript NSpoint AppleScript NSPredicate AppleScript NSRange AppleScript NSRegularExpression AppleScript NSRunningApplication AppleScript NSScreen AppleScript NSSize AppleScript NSString AppleScript NSStringCompareOptions AppleScript NSTask AppleScript NSTimeZone AppleScript NSURL AppleScript NSURL File AppleScript NSURLBookmark AppleScript NSURLComponents AppleScript NSURLResourceKey AppleScript NSUserDefaults AppleScript NSUUID AppleScript NSView AppleScript NSWorkspace AppleScript Numbers AppleScript OAuth AppleScript ObjC AppleScript OneDrive AppleScript Osax AppleScript PDF AppleScript PDFAnnotationWidget AppleScript PDFContext AppleScript PDFDisplayBox AppleScript PDFDocumentPermissions AppleScript PDFKit AppleScript PDFnUP AppleScript PDFOutline AppleScript Photos AppleScript Photoshop AppleScript Plist System Events AppleScript PostScript AppleScript prefPane AppleScript Preview AppleScript Python AppleScript QR AppleScript QR Decode AppleScript QuickLook AppleScript QuickTime AppleScript record AppleScript Regular Expression AppleScript Reminders AppleScript ReName AppleScript Repeat AppleScript RTF AppleScript Safari AppleScript SaveFile AppleScript ScreenCapture AppleScript ScreenSaver AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript Sort AppleScript Sound AppleScript Spotlight AppleScript SRT AppleScript StandardAdditions AppleScript stringByApplyingTransform AppleScript System Events AppleScript System Settings AppleScript TemporaryItems AppleScript Terminal AppleScript Text AppleScript Text CSV AppleScript Text TSV AppleScript TextEdit AppleScript Translate AppleScript Trash AppleScript Twitter AppleScript UI AppleScript Unit Conversion AppleScript UTType AppleScript valueForKeyPath AppleScript Video AppleScript VisionKit AppleScript Visual Studio Code AppleScript webarchive AppleScript webp AppleScript Wifi AppleScript XML AppleScript XML EPUB AppleScript XML OPML AppleScript XML Plist AppleScript XML RSS AppleScript XML SVG AppleScript XML TTML AppleScript XML webloc AppleScript YouTube AppleScript Zero Suppress Applications Barcode OnLine Tools CityCode Design iPhone List Logs lsappinfo Memo Music perl PlistBuddy pluginkit postalcode ReadMe SF Symbols character id SF Symbols Entity sips Skype Slack sqlite TCC Tools Typography Video Wacom zoom