20260523

[applescript] ビデオのpxサイズ取得 Exiftool コマンドを使う

[applescript] ビデオのpxサイズ取得 Exiftool コマンドを使う

NOTE記事一覧ですnote.com
 

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

ビデオのpxサイズ取得Exiftool.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
004(*
005
006Exiftoolを使う方法
007インストールが別途必要
008https://working-penguin.blogspot.com/2026/05/applescriptexiftool.html
009
010
011
012com.cocolog-nifty.quicktimer.icefloe *)
013----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
014use AppleScript version "2.8"
015use framework "Foundation"
016use framework "AppKit"
017use framework "UniformTypeIdentifiers"
018use scripting additions
019property refMe : a reference to current application
020
021#############################
022#
023set strBinFilePath to ("~/Library/Application Support/bin/exiftool/exiftool") as text
024set ocidBinFilePathStr to refMe's NSString's stringWithString:(strBinFilePath)
025set ocidBinFilePath to ocidBinFilePathStr's stringByStandardizingPath()
026set ocidBinFilePath to ocidBinFilePath's stringByExpandingTildeInPath()
027set ocidBinFilePathURL to refMe's NSURL's fileURLWithPath:(ocidBinFilePath) isDirectory:(false)
028
029#############################
030###入力ファイル
031#############################
032tell current application
033   set strName to name as text
034end tell
035if strName is "osascript" then
036   tell application "Finder" to activate
037else
038   tell current application to activate
039end if
040#デスクトップ
041set aliasDesktopDirPath to (path to desktop from user domain) as alias
042set listUTI to {"public.movie"} as list
043set strTitle to ("ファイル選択") as text
044set strPrompt to ("ムービーファイルを選んでください") as text
045tell application "SystemUIServer"
046   activate
047   set listAliasFilePath to (choose file with prompt (strPrompt) default location (aliasDesktopDirPath) of type (listUTI) with invisibles, showing package contents and multiple selections allowed) as list
048end tell
049
050#NSURLのArrayを作成して
051set ocidFileURLArrayM to refMe's NSMutableArray's alloc()'s init()
052
053repeat with itemAliasFilePath in listAliasFilePath
054   set aliasFilePath to itemAliasFilePath as alias
055   set strFilePath to (POSIX path of aliasFilePath) as text
056   set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
057   set ocidFilePathStr to ocidFilePathStr's precomposedStringWithCanonicalMapping()
058   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
059   set ocidFilePath to ocidFilePath's stringByExpandingTildeInPath()
060   set ocidFilePathURL to (refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false))
061   (ocidFileURLArrayM's addObject:(ocidFilePathURL))
062end repeat
063
064#ソート
065set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("path") ascending:(yes) selector:("localizedStandardCompare:")
066set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
067set ocidFileURLArrayS to ocidFileURLArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
068
069#Arrayのアイテム数
070set numCntArray to ocidFileURLArrayS's |count|()
071
072repeat with itemIntNo from 0 to (numCntArray - 1) by 1
073   #開くムービーのエイリアス・パスをエイリアスリストから取得して
074   set ocidItemFileURL to (ocidFileURLArrayS's objectAtIndex:(itemIntNo))
075   set ocidItemFile to ocidItemFileURL's |path|()
076   set ocidBinFilePath to ocidBinFilePathURL's |path|()
077   set ocidItemFile to doPathEscape(ocidItemFile)
078   set ocidBinFilePath to doPathEscape(ocidBinFilePath)
079   
080   set strCmd to ("\"" & ocidBinFilePath & "\" -json   \"" & ocidItemFile & "\"") as text
081   set strStdOut to (do shell script strCmd) as text
082   #
083   set ocidStdOut to (refMe's NSMutableString's stringWithString:(strStdOut))
084   set ocidJsonData to (ocidStdOut's dataUsingEncoding:(refMe's NSUTF8StringEncoding))
085   #JSON ルートがArray
086   set ocidOption to (refMe's NSJSONReadingJSON5Allowed)
087   set listResponse to (refMe's NSJSONSerialization's JSONObjectWithData:(ocidJsonData) options:(ocidOption) |error|:(reference))
088   set ocidJsonDict to (first item of listResponse)'s firstObject()
089   set numWpx to (ocidJsonDict's objectForKey:("SourceImageWidth")) as integer
090   set numHpx to (ocidJsonDict's objectForKey:("SourceImageHeight")) as integer
091   set numWpx to (ocidJsonDict's objectForKey:("ImageWidth")) as integer
092   set numHpx to (ocidJsonDict's objectForKey:("ImageHeight")) as integer
093   
094   log numWpx
095   log numHpx
096   
097end repeat
098
099
100return
101
102
103
104
105########################
106#パスのエスケープ
107to doPathEscape(strFilePath)
108   set strFilePath to strFilePath as text
109   set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
110   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
111   set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)
112   set listEscChar to {"\\", "\"", "$", "`"} as list
113   repeat with itemEscChar in listEscChar
114      set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:(itemEscChar) withString:("\\" & itemEscChar & ""))
115   end repeat
116   set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("!") withString:("\"'!'\""))
117   
118   return ocidFilePath
119end doPathEscape
AppleScriptで生成しました