« [exiftool] XML(XML書き出し) | トップページ | [delimiters] delimiters 区切り文字 »

[ExifTool]HTML書き出し

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSMutableString : a reference to refMe's NSMutableString
property refNSData : a reference to refMe's NSData
property refNSURL : a reference to refMe's NSURL
property refNSDictionary : a reference to refMe's NSDictionary
property refNSMutableDictionary : a reference to refMe's NSMutableDictionary
property refNSJSONSerialization : a reference to refMe's NSJSONSerialization

#####設定項目
####exiftoolへのパス
set strExifPath to "$HOME/bin/exiftool/exiftool"
########################################
#######前準備
##このファイルへのパスとコンテナ
tell application "Finder"
set aliasPathToMe to (path to me) as alias
set aliasPathToMeDir to (container of aliasPathToMe) as alias
end tell
set strPathToMePath to (POSIX path of aliasPathToMe) as text
set strPathToMeDirPath to (POSIX path of aliasPathToMeDir) as text
########################################
#######ファイル選択
---プロンプトの文言改行が使えます\nを入れます
set theWithPrompt to "ファイルを選択してください"
---ファイル選択ダイアログのデフォルトのディレクトリ
set aliasDefaultLocation to path to desktop folder from user domain
set theFileTypeList to {"public.item"}
---ダイアログを出して選択されたファイルは「open」に渡す
set aliasFilePath to (choose file default location aliasDefaultLocation ¬
with prompt theWithPrompt ¬
of type theFileTypeList ¬
invisibles false ¬
with showing package contents without multiple selections allowed)
####入力ファイル関連
set strFilePath to POSIX path of aliasFilePath as text
###NSストリングに
set ocidFilePath to refNSString's stringWithString:strFilePath
###NSURL
set ocidFilePathURL to refMe's NSURL's fileURLWithPath:ocidFilePath
##ファイル名取得
set strFileName to (ocidFilePathURL's lastPathComponent) as text
########################################
######出力ファイル
##ファイル名
set strFileName to (strFileName & ".html") as text
set aliasSaveFilePath to choose file name with prompt "htmlのファイル名を付けてください" default name strFileName default location (aliasDefaultLocation)
###パス
set strSaveFilePath to POSIX path of aliasSaveFilePath as text
###NSストリングに
set ocidSaveFilePath to refNSString's stringWithString:strSaveFilePath
###NSURL
set ocidSaveFilePathURL to refMe's NSURL's fileURLWithPath:ocidSaveFilePath
##拡張子
set strFileExtension to (ocidFilePathURL's pathExtension) as text
###拡張子つけ忘れ対策
if strFileExtension is not "html" then
set strSaveFilePath to (POSIX path of aliasSaveFilePath & ".html") as text
end if

########################################
######データ処理
###データ取得
try
set theCommandText to ("\"" & strExifPath & "\" -json \"" & strFilePath & "\"") as text
set strResponseJosn to (do shell script theCommandText) as text
on error
log "たぶんexiftoolの非対応のファイルタイプです"
end try
###テキストに
set ocidReadDataJson to refNSString's stringWithString:strResponseJosn
###UTF8データに
set ocidReadDataUTF to ocidReadDataJson's dataUsingEncoding:(refMe's NSUTF8StringEncoding)
###データ初期化
set ocidReadData to (refNSData's alloc()'s initWithData:ocidReadDataUTF)
###JSON初期化
set listJSONSerialization to (refNSJSONSerialization's JSONObjectWithData:ocidReadData options:0 |error|:(reference))
####レコードを取得します
set ocidJsonData to item 1 of listJSONSerialization
-->(*__NSSingleObjectArrayI*)
set ocidJsonDict to (item 1 of ocidJsonData)
-->(*__NSDictionaryI*)
####レコードをNSDictionaryとして格納
set ocidJsonResponse to (refNSDictionary's alloc()'s initWithDictionary:ocidJsonDict)
########################################
#######HTML 基本構造
###スタイル
set strStylle to "<style>#bordertable {padding: 10px;width: 100%;margin: 0;border-collapse: collapse;border-spacing: 0;word-wrap: break-word;} #bordertable table { width: 580px;margin: 0px;padding: 0px;border: 0px;border-spacing:0px;border-collapse: collapse;} #bordertable caption { font-weight: 900;} #bordertable thead { font-weight: 600;border-spacing:0px;} #bordertable td {border: solid 1px #666666;padding: 3px;margin: 0px;word-wrap: break-word;border-spacing:0px;} #bordertable tr {border: solid 1px #666666;padding: 0px;margin: 0px;border-spacing:0px;} #bordertable th {border: solid 1px #666666;padding: 0px;margin: 0px;border-spacing:0px;}</style>"
###ヘッダー部
set strHead to "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"><title>[Exif]" & strFileName & "</title>" & strStylle & "</head><body>"
###ボディ
set strBody to ""
###最後
set strHtmlEndBody to "</body></html>"
###HTML書き出し用のテキスト初期化
set ocidHTMLString to refNSMutableString's alloc()'s initWithCapacity:0
####
(ocidHTMLString's appendString:strHead)
###テーブル構造
set strTableHeader to "<div id=\"bordertable\"><table><caption>Exif情報:" & strFileName & "</caption><thead title=\"表の項目について\"><tr><th title=\"項目の番号が最初\" style=\"width: 36px;\">項番</th><th title=\"キーの名前が2番目\">キーの名前</th><th title=\"キーの値は3番目になっています\">キーの値</th></thead><tbody title=\"表の始まりです\">" as text
(ocidHTMLString's appendString:strTableHeader)

####JSONの全てのキーを取得して
set ocidAllKeys to ocidJsonResponse's allKeys()
set numLineNO to 0
####キーの種類だけ繰り返し
repeat with objKey in ocidAllKeys
set strKey to objKey as text
####キーの値を取り出し
set strValue to (ocidJsonResponse's valueForKey:strKey) as text
####内容によって表示させるための分岐
if strKey is "ExifToolVersion" then
###キーと値を保存
set strHTMLTagFooterKey to strKey as text
set strHTMLTagFooterValue to strValue as text
####例外処理
else if strKey is "Directory" then
set numLineNO to numLineNO + 1 as number
set strHTMLTag to "<tr><td title=\"項番:" & strKey & "\">" & numLineNO & "</td><td title=\"キー:" & strKey & "について\">" & strKey & "</td><td title=\"キー:" & strKey & "の値\">非表示にしています</td></tr>" as text
else if strKey is "SourceFile" then
set numLineNO to numLineNO + 1 as number
set strHTMLTag to "<tr><td title=\"項番:" & strKey & "\">" & numLineNO & "</td><td title=\"キー:" & strKey & "について\">" & strKey & "</td><td title=\"キー:" & strKey & "の値\">非表示にしています</td></tr>" as text
else
###普通はこの処理
set numLineNO to numLineNO + 1 as number
set strHTMLTag to "<tr><td title=\"項番:" & strKey & "\">" & numLineNO & "</td><td title=\"キー:" & strKey & "について\">" & strKey & "</td><td title=\"キー:" & strKey & "の値\">" & strValue & "</td></tr>" as text
end if
####できたタグを追加
(ocidHTMLString's appendString:strHTMLTag)

end repeat
###最後にバージョンを入れる
set numLineNO to numLineNO + 1 as number
###最後のtfoot項目
set strHTMLTagFooter to "</tbody><tfoot title=\"表の終わりです\"><tr><td title=\"項番:" & numLineNO & "\">" & numLineNO & "</td><td title=\"キーの名前\">ExifToolVersion</td><td title=\"キー:" & strHTMLTagFooterKey & "の値\">ExifTool" & strHTMLTagFooterValue & "で作成しました</td></tr></tfoot></table></div>" as text
###タグを全部追加して
(ocidHTMLString's appendString:strHTMLTagFooter)
ocidHTMLString's appendString:strHtmlEndBody
###ファイルに書き出し
set boolWritetoUrlArray to ocidHTMLString's writeToFile:strSaveFilePath atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)


|

« [exiftool] XML(XML書き出し) | トップページ | [delimiters] delimiters 区切り文字 »

AppleScript EXIFData」カテゴリの記事