« [FONT]文字イメージ作成(途中) | トップページ | [exiftool] htmlDump(イメージデータ専用) »

[exiftool]JSONデータをPLISTに変換する

#!/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 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 & ".plist") as text
set aliasSaveFilePath to choose file name with prompt "Plistのファイル名を付けてください" 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 "plist" then
set strSaveFilePath to (POSIX path of aliasSaveFilePath & ".plist") as text
end if
########################################
######データ処理
###データ取得
set theCommandText to ("\"" & strExifPath & "\" -json \"" & strFilePath & "\"") as text
set strResponseJosn to (do shell script theCommandText) as text
###テキストに
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)

######################strSaveFilePath
###定型どちらか選ぶ
###バイナリー形式
set ocidBinplist to refMe's NSPropertyListBinaryFormat_v1_0
###XML形式
set ocidXmlplist to refMe's NSPropertyListXMLFormat_v1_0
####書き込み用にバイナリーデータに変換
set ocidPlistEditData to refMe's NSPropertyListSerialization's dataWithPropertyList:ocidJsonResponse format:ocidXmlplist options:0 |error|:(missing value)
####書き込み
####ocidPlistEditData's writeToFile:ocidPosixFullPath atomically:true
set boolWritetoUrlArray to ocidPlistEditData's writeToFile:strSaveFilePath options:0 |error|:(reference)
set ocidWriteToUrlResults to item 1 of boolWritetoUrlArray
set ocidErrorResults to item 2 of boolWritetoUrlArray
if ocidWriteToUrlResults is true then

####JSONの全てのキーを取得して
set ocidAllKeys to ocidJsonResponse's allKeys()
####キーの種類だけ繰り返し
repeat with objKey in ocidAllKeys
####キーの値を取り出し
set ocidValue to (ocidJsonResponse's valueForKey:(objKey as text))
log "" & (objKey as text) & " : " & (ocidValue as text)
###フォント名を取得
set ocidFontName to (ocidJsonResponse's valueForKey:"FontName-ja")
if ocidFontName is (missing value) then
set ocidFontName to (ocidJsonResponse's valueForKey:"FontName")
end if
end repeat
set ocidPlistEditData to ""
set listJSONSerialization to ""
set ocidReadData to ""
set ocidJsonResponse to ""
else
try
log ocidWriteToUrlResults's localizedDescription() as text
log ocidWriteToUrlResults's localizedFailureReason() as text
log ocidWriteToUrlResults's localizedRecoverySuggestion() as text
log ocidWriteToUrlResults's localizedRecoveryOptions() as text
log ocidWriteToUrlResults's recoveryAttempter() as text
log ocidWriteToUrlResults's helpAnchor() as text
end try
log "ファイルの書き込みに失敗しました"
end if

|

« [FONT]文字イメージ作成(途中) | トップページ | [exiftool] htmlDump(イメージデータ専用) »

AppleScript XML Plist」カテゴリの記事

AppleScript Json」カテゴリの記事

AppleScript EXIFData」カテゴリの記事