20260523

[AppleScript]exiftoolダウンロード+インストール


[AppleScript]exiftoolダウンロード+インストール

NOTE記事一覧ですnote.com
 

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

exiftoolインストール.applescript.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
004(*
005留意事項  
006インストール先が ユーザー権限でアップデート出来る
007 "~/Library/Application Support/bin/exiftool/exiftool"
008
009なっています
010
011
012一般的な/usr/local/binにインストールするには
013管理者権限が必要です
014管理者の方にご相談ください
015
016
017v1 コンテナ形式のPKG用に作成
018v2 PKGの形式変更に対応
019v2.1 PKG解凍前にアトリビュートを削除してから解凍するように追加
020
021
022com.cocolog-nifty.quicktimer.icefloe *)
023----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
024use AppleScript version "2.8"
025use framework "Foundation"
026use framework "AppKit"
027use scripting additions
028
029property refMe : a reference to current application
030set appFileManager to refMe's NSFileManager's defaultManager()
031
032
033
034############################
035#
036set listBinPath to {"/usr/local/bin/exiftool", "~/Library/Application Support/bin/exiftool/exiftool", "~/bin/exiftool/exiftool"} as list
037#
038set strChkBinPath to ("/usr/local/bin/exiftool") as text
039set ocidChkBinPathStr to refMe's NSString's stringWithString:(strChkBinPath)
040set ocidChkBinPath to ocidChkBinPathStr's stringByStandardizingPath()
041set boolExists to appFileManager's fileExistsAtPath:(ocidChkBinPath) isDirectory:(false)
042if boolExists is true then
043   set strCmd to ("\"" & ocidChkBinPath & "\" -ver") as text
044   set strStdOut to (do shell script strCmd) as text
045   log "管理者権限が必要なのでアップデートしません。処理を中止します"
046   return false
047else if boolExists is false then
048   set strChkBinPath to ("~/Library/Application Support/bin/exiftool/exiftool") as text
049   set ocidChkBinPathStr to refMe's NSString's stringWithString:(strChkBinPath)
050   set ocidChkBinPath to ocidChkBinPathStr's stringByStandardizingPath()
051   set boolExists to appFileManager's fileExistsAtPath:(ocidChkBinPath) isDirectory:(false)
052   if boolExists is true then
053      set strCmd to ("'" & ocidChkBinPath & "' -ver") as text
054      set strStdOut to (do shell script strCmd) as text
055   else if boolExists is false then
056      log "想定パスに未インストール"
057      set strStdOut to "0" as text
058   end if
059end if
060
061############################
062#バージョン番号を取得する
063set strVerURL to ("https://exiftool.org/ver.txt") as text
064set ocidVerURLStr to refMe's NSString's stringWithString:(strVerURL)
065set ocidVerURL to refMe's NSURL's alloc()'s initWithString:(ocidVerURLStr)
066set ocidVerText to refMe's NSString's stringWithContentsOfURL:(ocidVerURL) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
067set strVer to (first item of ocidVerText) as text
068if strVer is strStdOut then
069   log "最新バージョンがインストール済み"
070   return true
071end if
072
073
074############################
075#インストール先
076set strInstrallDirPath to ("~/Library/Application Support/bin/exiftool") as text
077set ocidInstrallDirPathStr to refMe's NSString's stringWithString:(strInstrallDirPath)
078set ocidInstrallDirPath to ocidInstrallDirPathStr's stringByStandardizingPath()
079set ocidInstrallDirPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidInstrallDirPath) isDirectory:(true)
080#フォルダを作る
081
082set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
083ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
084set listDone to appFileManager's createDirectoryAtURL:(ocidInstrallDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
085if (first item of listDone) is true then
086   log "正常処理"
087else if (second item of listDone) ≠ (missing value) then
088   set strErrorNO to (second item of listDone)'s code() as text
089   set strErrorMes to (second item of listDone)'s localizedDescription() as text
090   refMe's NSLog("■:" & strErrorNO & strErrorMes)
091   return "エラーしました" & strErrorNO & strErrorMes
092end if
093set strInstrallDirPath to (ocidInstrallDirPathURL's |path|()) as text
094log strInstrallDirPath
095
096############################
097#ダウンロードディレクトリ
098set ocidTempDirURL to appFileManager's temporaryDirectory()
099set ocidUUID to refMe's NSUUID's alloc()'s init()
100set ocidUUIDString to ocidUUID's UUIDString
101set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:(true)
102#フォルダを作る
103set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
104ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
105set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
106if (first item of listDone) is true then
107   log "正常処理"
108else if (second item of listDone) ≠ (missing value) then
109   set strErrorNO to (second item of listDone)'s code() as text
110   set strErrorMes to (second item of listDone)'s localizedDescription() as text
111   refMe's NSLog("■:" & strErrorNO & strErrorMes)
112   return "エラーしました" & strErrorNO & strErrorMes
113end if
114#解凍先ディレクトリ
115set ocidExpandDirPathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("ExifTool") isDirectory:(true)
116set strExpandDirPath to (ocidExpandDirPathURL's |path|()) as text
117
118
119############################
120#URLとファイル名の整形
121set strPkgName to ("ExifTool-" & strVer & ".pkg") as text
122#保存URL
123set ocidSavePkgFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strPkgName)
124set strSavePkgFilePath to (ocidSavePkgFilePathURL's |path|()) as text
125
126#URL
127set strURL to ("https://exiftool.org/" & strPkgName & "") as text
128set ocidURLStr to refMe's NSString's stringWithString:(strURL)
129set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLStr)
130
131############################
132#NSData'sダウンロード
133set ocidOption to (refMe's NSDataReadingMappedAlways)
134set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error|:(reference)
135if (item 2 of listResponse) = (missing value) then
136   log "正常処理"
137   set ocidPkgData to (item 1 of listResponse)
138else if (item 2 of listResponse) ≠ (missing value) then
139   set strErrorNO to (item 2 of listResponse)'s code() as text
140   set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text
141   refMe's NSLog("■:" & strErrorNO & strErrorMes)
142   return "エラーしました" & strErrorNO & strErrorMes
143end if
144
145############################
146#NSData's保存
147set ocidOption to (refMe's NSDataWritingAtomic)
148set listDone to ocidPkgData's writeToURL:(ocidSavePkgFilePathURL) options:(ocidOption) |error|:(reference)
149if (first item of listDone) is true then
150   log "正常処理"
151else if (second item of listDone) ≠ (missing value) then
152   set strErrorNO to (second item of listDone)'s code() as text
153   set strErrorMes to (second item of listDone)'s localizedDescription() as text
154   refMe's NSLog("■:" & strErrorNO & strErrorMes)
155   return "エラーしました" & strErrorNO & strErrorMes
156end if
157
158############################
159#アトリビュート削除
160set ocidSavePkgFilePath to (ocidSavePkgFilePathURL's |path|())
161set strCmd to ("/usr/bin/xattr -rc '" & ocidSavePkgFilePath & "'") as text
162do shell script strCmd
163
164############################
165#pkg解凍
166set strComandText to "/usr/sbin/pkgutil  --expand-full  \"" & strSavePkgFilePath & "\" \"" & strExpandDirPath & "\"" as text
167log strComandText
168try
169   do shell script strComandText
170on error
171   return "エラーしました パッケージの解凍でエラー"
172end try
173
174############################
175#古い方をゴミ箱に
176set appFileManager to refMe's NSFileManager's defaultManager()
177set listDone to (appFileManager's trashItemAtURL:(ocidInstrallDirPathURL) resultingItemURL:(ocidInstrallDirPathURL) |error|:(reference))
178if (first item of listDone) is true then
179   log "正常処理"
180else if (second item of listDone) ≠ (missing value) then
181   set strErrorNO to (second item of listDone)'s code() as text
182   set strErrorMes to (second item of listDone)'s localizedDescription() as text
183   refMe's NSLog("■:" & strErrorNO & strErrorMes)
184   return "エラーしました" & strErrorNO & strErrorMes
185end if
186
187
188###ユーザーディレクトリにコピー
189set ocidDittoDirPathURL to ocidExpandDirPathURL's URLByAppendingPathComponent:("Payload/usr/local/bin") isDirectory:(true)
190set strDittoDirPath to (ocidDittoDirPathURL's |path|()) as text
191set strCoommandText to "/usr/bin/ditto  \"" & strDittoDirPath & "\"  \"" & strInstrallDirPath & "\"" as text
192log strCoommandText
193try
194   do shell script strCoommandText
195on error
196   return "エラーしました dittoでエラー"
197end try
198
199
200############################
201#インストール先を開く
202set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
203set boolDone to appSharedWorkspace's openURL:(ocidInstrallDirPathURL)
204
205############################
206#クリーニング
207set listDone to (appFileManager's trashItemAtURL:(ocidSaveDirPathURL) resultingItemURL:(ocidSaveDirPathURL) |error|:(reference))
208if (second item of listDone) ≠ (missing value) then
209   set strErrorNO to (second item of listDone)'s code() as text
210   set strErrorMes to (second item of listDone)'s localizedDescription() as text
211   refMe's NSLog("■:" & strErrorNO & strErrorMes)
212   return "エラーしました" & strErrorNO & strErrorMes
213end if
214refMe's NSRunningApplication's terminateAutomaticallyTerminableApplications()
215try
216   tell application "System Events" to quit
217end try
218
219return boolDone
AppleScriptで生成しました