| 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 |
|
| 017 | v1 コンテナ形式のPKG用に作成 |
| 018 | v2 PKGの形式変更に対応 |
| 019 | v2.1 PKG解凍前にアトリビュートを削除してから解凍するように追加 |
| 020 |
|
| 021 |
|
| 022 | com.cocolog-nifty.quicktimer.icefloe *) |
| 023 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 024 | use AppleScript version "2.8" |
| 025 | use framework "Foundation" |
| 026 | use framework "AppKit" |
| 027 | use scripting additions |
| 028 |
|
| 029 | property refMe : a reference to current application |
| 030 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 031 |
|
| 032 |
|
| 033 |
|
| 034 | ############################ |
| 035 | # |
| 036 | set listBinPath to {"/usr/local/bin/exiftool", "~/Library/Application Support/bin/exiftool/exiftool", "~/bin/exiftool/exiftool"} as list |
| 037 | # |
| 038 | set strChkBinPath to ("/usr/local/bin/exiftool") as text |
| 039 | set ocidChkBinPathStr to refMe's NSString's stringWithString:(strChkBinPath) |
| 040 | set ocidChkBinPath to ocidChkBinPathStr's stringByStandardizingPath() |
| 041 | set boolExists to appFileManager's fileExistsAtPath:(ocidChkBinPath) isDirectory:(false) |
| 042 | if 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 |
| 047 | else 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 |
| 059 | end if |
| 060 |
|
| 061 | ############################ |
| 062 | #バージョン番号を取得する |
| 063 | set strVerURL to ("https://exiftool.org/ver.txt") as text |
| 064 | set ocidVerURLStr to refMe's NSString's stringWithString:(strVerURL) |
| 065 | set ocidVerURL to refMe's NSURL's alloc()'s initWithString:(ocidVerURLStr) |
| 066 | set ocidVerText to refMe's NSString's stringWithContentsOfURL:(ocidVerURL) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference) |
| 067 | set strVer to (first item of ocidVerText) as text |
| 068 | if strVer is strStdOut then |
| 069 | log "最新バージョンがインストール済み" |
| 070 | return true |
| 071 | end if |
| 072 |
|
| 073 |
|
| 074 | ############################ |
| 075 | #インストール先 |
| 076 | set strInstrallDirPath to ("~/Library/Application Support/bin/exiftool") as text |
| 077 | set ocidInstrallDirPathStr to refMe's NSString's stringWithString:(strInstrallDirPath) |
| 078 | set ocidInstrallDirPath to ocidInstrallDirPathStr's stringByStandardizingPath() |
| 079 | set ocidInstrallDirPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidInstrallDirPath) isDirectory:(true) |
| 080 | #フォルダを作る |
| 081 |
|
| 082 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init() |
| 083 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
| 084 | set listDone to appFileManager's createDirectoryAtURL:(ocidInstrallDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference) |
| 085 | if (first item of listDone) is true then |
| 086 | log "正常処理" |
| 087 | else 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 |
| 092 | end if |
| 093 | set strInstrallDirPath to (ocidInstrallDirPathURL's |path|()) as text |
| 094 | log strInstrallDirPath |
| 095 |
|
| 096 | ############################ |
| 097 | #ダウンロードディレクトリ |
| 098 | set ocidTempDirURL to appFileManager's temporaryDirectory() |
| 099 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
| 100 | set ocidUUIDString to ocidUUID's UUIDString |
| 101 | set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:(true) |
| 102 | #フォルダを作る |
| 103 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init() |
| 104 | ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions) |
| 105 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference) |
| 106 | if (first item of listDone) is true then |
| 107 | log "正常処理" |
| 108 | else 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 |
| 113 | end if |
| 114 | #解凍先ディレクトリ |
| 115 | set ocidExpandDirPathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("ExifTool") isDirectory:(true) |
| 116 | set strExpandDirPath to (ocidExpandDirPathURL's |path|()) as text |
| 117 |
|
| 118 |
|
| 119 | ############################ |
| 120 | #URLとファイル名の整形 |
| 121 | set strPkgName to ("ExifTool-" & strVer & ".pkg") as text |
| 122 | #保存URL |
| 123 | set ocidSavePkgFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strPkgName) |
| 124 | set strSavePkgFilePath to (ocidSavePkgFilePathURL's |path|()) as text |
| 125 |
|
| 126 | #URL |
| 127 | set strURL to ("https://exiftool.org/" & strPkgName & "") as text |
| 128 | set ocidURLStr to refMe's NSString's stringWithString:(strURL) |
| 129 | set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLStr) |
| 130 |
|
| 131 | ############################ |
| 132 | #NSData'sダウンロード |
| 133 | set ocidOption to (refMe's NSDataReadingMappedAlways) |
| 134 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error|:(reference) |
| 135 | if (item 2 of listResponse) = (missing value) then |
| 136 | log "正常処理" |
| 137 | set ocidPkgData to (item 1 of listResponse) |
| 138 | else 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 |
| 143 | end if |
| 144 |
|
| 145 | ############################ |
| 146 | #NSData's保存 |
| 147 | set ocidOption to (refMe's NSDataWritingAtomic) |
| 148 | set listDone to ocidPkgData's writeToURL:(ocidSavePkgFilePathURL) options:(ocidOption) |error|:(reference) |
| 149 | if (first item of listDone) is true then |
| 150 | log "正常処理" |
| 151 | else 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 |
| 156 | end if |
| 157 |
|
| 158 | ############################ |
| 159 | #アトリビュート削除 |
| 160 | set ocidSavePkgFilePath to (ocidSavePkgFilePathURL's |path|()) |
| 161 | set strCmd to ("/usr/bin/xattr -rc '" & ocidSavePkgFilePath & "'") as text |
| 162 | do shell script strCmd |
| 163 |
|
| 164 | ############################ |
| 165 | #pkg解凍 |
| 166 | set strComandText to "/usr/sbin/pkgutil --expand-full \"" & strSavePkgFilePath & "\" \"" & strExpandDirPath & "\"" as text |
| 167 | log strComandText |
| 168 | try |
| 169 | do shell script strComandText |
| 170 | on error |
| 171 | return "エラーしました パッケージの解凍でエラー" |
| 172 | end try |
| 173 |
|
| 174 | ############################ |
| 175 | #古い方をゴミ箱に |
| 176 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 177 | set listDone to (appFileManager's trashItemAtURL:(ocidInstrallDirPathURL) resultingItemURL:(ocidInstrallDirPathURL) |error|:(reference)) |
| 178 | if (first item of listDone) is true then |
| 179 | log "正常処理" |
| 180 | else 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 |
| 185 | end if |
| 186 |
|
| 187 |
|
| 188 | ###ユーザーディレクトリにコピー |
| 189 | set ocidDittoDirPathURL to ocidExpandDirPathURL's URLByAppendingPathComponent:("Payload/usr/local/bin") isDirectory:(true) |
| 190 | set strDittoDirPath to (ocidDittoDirPathURL's |path|()) as text |
| 191 | set strCoommandText to "/usr/bin/ditto \"" & strDittoDirPath & "\" \"" & strInstrallDirPath & "\"" as text |
| 192 | log strCoommandText |
| 193 | try |
| 194 | do shell script strCoommandText |
| 195 | on error |
| 196 | return "エラーしました dittoでエラー" |
| 197 | end try |
| 198 |
|
| 199 |
|
| 200 | ############################ |
| 201 | #インストール先を開く |
| 202 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
| 203 | set boolDone to appSharedWorkspace's openURL:(ocidInstrallDirPathURL) |
| 204 |
|
| 205 | ############################ |
| 206 | #クリーニング |
| 207 | set listDone to (appFileManager's trashItemAtURL:(ocidSaveDirPathURL) resultingItemURL:(ocidSaveDirPathURL) |error|:(reference)) |
| 208 | if (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 |
| 213 | end if |
| 214 | refMe's NSRunningApplication's terminateAutomaticallyTerminableApplications() |
| 215 | try |
| 216 | tell application "System Events" to quit |
| 217 | end try |
| 218 |
|
| 219 | return boolDone |