« 小数点以下桁揃え | トップページ | [exiftool]Exif2HTMLv3 exiftoolのExif情報をHTMLで表示する。 exiftoolインストール付き(たいぶ修正) »

[exiftool]Exif2HTMLv3 exiftoolのExif情報を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
set appFileManager to refMe's NSFileManager's defaultManager()

########################################
### 設定項目
### exiftoolへのパス
########################################
#一般的にはパスはこちら
# set strExifPath to "/usr/local/bin/exiftool/exiftool" as text
#自分用
set strExifPath to "~/bin/exiftool/exiftool" as text


#exiftoolパス処理
set ocidExifPathStr to refMe's NSString's stringWithString:(strExifPath)
set ocidExifPath to ocidExifPathStr's stringByStandardizingPath()
set ocidExifPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidExifPath) isDirectory:false)
set strExifPath to ocidExifPathURL's |path| as text

########################################
####出力先/var/folders/XX/(再起動時に削除されます)
set ocidTemporaryDirPathURL to appFileManager's temporaryDirectory
set ocidVarFoldersDirURL to ocidTemporaryDirPathURL's URLByDeletingLastPathComponent()
set ocidTemporaryItemsPathURL to ocidVarFoldersDirURL's URLByAppendingPathComponent:"TemporaryItems"
##フォルダを作る
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
set listDone to appFileManager's createDirectoryAtURL:(ocidTemporaryItemsPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference)
set aliasTemporaryItems to (ocidTemporaryItemsPathURL's absoluteURL()) as alias
########################################
#######ファイル選択
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 ocidHomeDirURL to appFileManager's homeDirectoryForCurrentUser()
#デスクトップの場合
set ocidDefaultLocationURL to ocidHomeDirURL's URLByAppendingPathComponent:("Desktop") isDirectory:true
#ダウンロードフォルダの場合
#set ocidDefaultLocationURL to ocidHomeDirURL's URLByAppendingPathComponent:("Downloads") isDirectory:true
#フォントフォルダの場合
#set ocidDefaultLocationURL to ocidHomeDirURL's URLByAppendingPathComponent:("Library/Fonts") isDirectory:true
#エイリアスパスにする
set aliasDefaultLocation to (ocidDefaultLocationURL's absoluteURL()) as alias
set listUTI to {"public.item"}
set strPromptText to "ファイルを選んでください" as text
set strMesText to "ファイルを選んでください" as text
set aliasFilePath to (choose file strMesText with prompt strPromptText default location (aliasDefaultLocation) of type listUTI with invisibles without multiple selections allowed and showing package contents) as alias
####入力ファイル関連
set strFilePath to POSIX path of aliasFilePath as text
###NSストリングに
set ocidFilePath to refMe's NSString's stringWithString:strFilePath
###NSURLに
set ocidFilePathURL to refMe's NSURL's fileURLWithPath:ocidFilePath
##ファイル名取得
set strFileName to (ocidFilePathURL's lastPathComponent) as text

########################################
######出力ファイル
##ファイル名
set strSaveFileName to (strFileName & ".html") as text
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
set aliasSaveFilePath to (choose file name with prompt "htmlのファイル名を付けてください" default name strSaveFileName default location aliasTemporaryItems) as «class furl»
#
set strSaveFilePath to (POSIX path of aliasSaveFilePath) as text
###NSストリングに
set ocidSaveFilePath to refMe's NSString's stringWithString:(strSaveFilePath)
###NSURLに
set ocidSaveFilePathURL to refMe's NSURL's fileURLWithPath:(ocidSaveFilePath)
##拡張子
set strFileExtension to (ocidSaveFilePathURL's pathExtension) as text
###拡張子つけ忘れ対策
if strFileExtension is not "html" then
  set strSaveFilePath to (POSIX path of aliasSaveFilePath & ".html") as text
end if
set strErrorFilePath to (strSaveFilePath & ".json") as text

########################################
###【1】データ取得 JSON出力
try
  set strCommandText to ("\"" & strExifPath & "\" -json \"" & strFilePath & "\"") as text
  set strResponseJosn to (do shell script strCommandText) as text
on error
  try
    set strCommandText to ("\"" & strExifPath & "\" -json \"" & strFilePath & "\" > \"" & strErrorFilePath & "\"") as text
    set strResponseJosn to (do shell script strCommandText) as text
  on error
    set strCommandText to ("/bin/cat \"" & strErrorFilePath & "\"") as text
    set strResponseJosn to (do shell script strCommandText) as text
log "たぶんexiftoolの非対応のファイルタイプですA"
  end try
end try
if strResponseJosn is "" then
return "たぶんexiftoolの非対応のファイルタイプですB"
end if
###【2】NSStringにテキストに格納して
set ocidReadDataJson to refMe's NSString's stringWithString:strResponseJosn
###【3】NSConcreteMutableDataに変換
set ocidReadDataUTF to ocidReadDataJson's dataUsingEncoding:(refMe's NSUTF8StringEncoding)
###【4】NSInlineDataデータ初期化
set ocidReadData to (refMe's NSData's alloc()'s initWithData:(ocidReadDataUTF))
###【5】NSJSONSerialization
set listJSONSerialization to (refMe's NSJSONSerialization's JSONObjectWithData:ocidReadData options:0 |error|:(reference))
set ocidJsonData to (item 1 of listJSONSerialization)
-->(*__NSSingleObjectArrayI*)
####解説
(*
exiftoolの書き出すJSONはrootがArray構造から始まっているので
PlistにするにはDictに変換なりする必要がある
幸い『[{}]』といったシングルArray構造なので
Arrayから最初のOBJECTを取り出せばDICTレコード構造になる
*)
set ocidJsonDict to (ocidJsonData's firstObject())
-->(*__NSDictionaryI*)
###【6】レコードをNSDictionaryとして格納
set ocidJsonDict to (refMe's NSDictionary's alloc()'s initWithDictionary:ocidJsonDict)
#AllKeys
set ocidAllKeyArray to ocidJsonDict's allKeys()
#並び替えソート
set ocidAllKeySortedArray to ocidAllKeyArray's sortedArrayUsingSelector:("localizedStandardCompare:")

##############################
# 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 strTitleText to ("【ExifTool】: " & strFileName & " :メタデータ一覧") as text
set ocidRootElement to doMakeRootElement(strTitleText)
#ボディエレメント
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:(strTitleText)
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
#ファイルのパスの数だけ繰り返し
repeat with itemKeyName in ocidAllKeySortedArray
  #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))
  #【項目名=KEY名】をTDでセット
  set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("項目名"))
(ocidTdElement's setStringValue:(itemKeyName))
(ocidTdElement's addAttribute:(ocidAddNode))
  #TDをTRにセット
(ocidTrElement's addChild:(ocidTdElement))
  #【値=Value】TD
  #値の取得
  set ocidValue to (ocidJsonDict's valueForKey:(itemKeyName))
  set strValueClass to className() of ocidValue as text
  set strValue to "" as text
  if strValueClass contains "Array" then
    set strValue to (ocidValue's componentsJoinedByString:(", "))
  else
    set strValue to ocidValue as text
  end if
  #
  set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("値"))
(ocidTdElement's setStringValue:(strValue))
(ocidTdElement's addAttribute:(ocidAddNode))
  #TDをTRにセット
(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))
#項目キーの数
set numCntKey to (count of ocidAllKeySortedArray) as integer
set strValue to ("メタデータの数は:" & numCntKey & " 件でした") as text
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)

(*
####テキストエディタで開く
log strSaveFilePath
tell application "TextEdit"
activate
open (POSIX file strSaveFilePath as alias)
end tell
*)
####サファリで開く
set strOpenURL to ocidSaveFilePathURL's absoluteString() as text
tell application "Safari"
activate
make new document with properties {name:"JSON2HTMLv3"}
  tell window 1
open location strOpenURL
  end tell
end tell
####
set ocidContainerDirPathURL to ocidSaveFilePathURL's URLByDeletingLastPathComponent()
set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
set boolDone to appSharedWorkspace's openURL:(ocidContainerDirPathURL)


##############################
# 基本的なHTMLの構造
##############################
to doMakeRootElement(argStrTitleText)
  set strTitleText to argStrTitleText as text
  #
  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:(strTitleText)
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

|

« 小数点以下桁揃え | トップページ | [exiftool]Exif2HTMLv3 exiftoolのExif情報をHTMLで表示する。 exiftoolインストール付き(たいぶ修正) »

AppleScript EXIFData」カテゴリの記事

AppleScript HTML」カテゴリの記事