| 001 | #!/usr/bin/env osascript |
| 002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 003 | (* |
| 004 | Microsoft EdgeのDMGインストーラー最新版をダウンロード |
| 005 | ユーザー・アプリケーションフォルダにコピーインストールします |
| 006 |
|
| 007 | v1初回作成 |
| 008 | v2 com.apple.quarantineを削除するようにした |
| 009 |
|
| 010 |
|
| 011 | com.cocolog-nifty.quicktimer.icefloe *) |
| 012 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 013 | use AppleScript version "2.8" |
| 014 | use framework "Foundation" |
| 015 | use framework "UniformTypeIdentifiers" |
| 016 | use framework "AppKit" |
| 017 | use scripting additions |
| 018 | property refMe : a reference to current application |
| 019 | property strBundleID : ("com.microsoft.edgemac") as text |
| 020 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 021 |
|
| 022 | set strURL to ("https://go.microsoft.com/fwlink/?linkid=2192091") as text |
| 023 | set ocidURLString to (refMe's NSString's stringWithString:(strURL)) |
| 024 | set ocidURL to (refMe's NSURL's alloc()'s initWithString:(ocidURLString)) |
| 025 | #テンポラリーフォルダ |
| 026 | set ocidTempDirURL to appFileManager's temporaryDirectory() |
| 027 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
| 028 | set ocidUUIDString to ocidUUID's UUIDString |
| 029 | set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:(true) |
| 030 | set strSaveDirPath to ocidSaveDirPathURL's |path|() as text |
| 031 | #保存先フォルダ=再起動時に自動削除を作成 |
| 032 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init() |
| 033 | ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions) |
| 034 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference) |
| 035 | #リダイレクト先を取得 |
| 036 | #リクエスト |
| 037 | set appRequest to refMe's NSMutableURLRequest's requestWithURL:(ocidURL) |
| 038 | appRequest's setHTTPMethod:("GET") |
| 039 | appRequest's setTimeoutInterval:(1) |
| 040 | #セッション |
| 041 | set ocidConf to refMe's NSURLSessionConfiguration's defaultSessionConfiguration() |
| 042 | set appQueue to refMe's NSOperationQueue's mainQueue() |
| 043 | set appSession to refMe's NSURLSession's sessionWithConfiguration:(ocidConf) delegate:("self") delegateQueue:(appQueue) |
| 044 | #簡易にこの記述でもいい |
| 045 | #set appSession to refMe's NSURLSession's sharedSession() |
| 046 | #タスク |
| 047 | set appTask to appSession's dataTaskWithRequest:(appRequest) |
| 048 | #タスクスタート |
| 049 | appTask's resume() |
| 050 | set ocidRedirectURL to (missing value) |
| 051 | repeat 20 times |
| 052 | set ocidStatue to appTask's response() |
| 053 | # log ocidStatue |
| 054 | if ocidStatue = (missing value) then |
| 055 | delay 0.2 |
| 056 | else |
| 057 | set ocidStatue to appTask's response() |
| 058 | set ocidRedirectURL to ocidStatue's |URL|() |
| 059 | log ocidRedirectURL's absoluteString() as text |
| 060 | exit repeat |
| 061 | end if |
| 062 | end repeat |
| 063 | #タスク終了 |
| 064 | appTask's cancel() |
| 065 | if ocidRedirectURL = (missing value) then |
| 066 | return "リダイレクトに失敗しています" |
| 067 | end if |
| 068 | #ファイル名 |
| 069 | set ocidFileName to ocidRedirectURL's lastPathComponent() |
| 070 | #ダウンロードの保存先 |
| 071 | set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidFileName) isDirectory:(false) |
| 072 | set strSaveFilePath to ocidSaveFilePathURL's |path|() as text |
| 073 |
|
| 074 | #マウントポイント |
| 075 | set ocidMountPointPathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("Microsoft Edge") isDirectory:(true) |
| 076 | set strMountPointPath to ocidMountPointPathURL's |path|() as text |
| 077 |
|
| 078 | set listDone to appFileManager's createDirectoryAtURL:(ocidMountPointPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference) |
| 079 |
|
| 080 | #DMGマウント時のアプリケーションのURL |
| 081 | set ocidMountAppPathURL to ocidMountPointPathURL's URLByAppendingPathComponent:("Microsoft Edge.app") isDirectory:(false) |
| 082 | #保存先 |
| 083 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSApplicationDirectory) inDomains:(refMe's NSUserDomainMask)) |
| 084 | set ocidApplicationDirPathURL to ocidURLsArray's firstObject() |
| 085 | set ocidDistDirPathURL to ocidApplicationDirPathURL's URLByAppendingPathComponent:("Sites") isDirectory:(true) |
| 086 | #フォルダ作成700 |
| 087 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init() |
| 088 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
| 089 | set listDone to appFileManager's createDirectoryAtURL:(ocidDistDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference) |
| 090 | #localized生成 |
| 091 | set ocidLocalizedPathURL to ocidDistDirPathURL's URLByAppendingPathComponent:(".localized") isDirectory:(false) |
| 092 | set ocidNullText to (refMe's NSString's stringWithString:("")) |
| 093 | set listDone to ocidNullText's writeToURL:(ocidLocalizedPathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference) |
| 094 | #最終的なアプリケーションのインストール先 |
| 095 | set ocidDistAppPathURL to ocidDistDirPathURL's URLByAppendingPathComponent:("Microsoft Edge.app") isDirectory:(false) |
| 096 |
|
| 097 | ##NSDATA ダウンロード |
| 098 | log "ダウンロード開始" |
| 099 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
| 100 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidRedirectURL) options:(ocidOption) |error|:(reference) |
| 101 | if (item 2 of listResponse) = (missing value) then |
| 102 | log "initWithContentsOfURL 正常処理" |
| 103 | set ocidReadData to (item 1 of listResponse) |
| 104 | else if (item 2 of listResponse) ≠ (missing value) then |
| 105 | set strErrorNO to (item 2 of listResponse)'s code() as text |
| 106 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
| 107 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
| 108 | return "initWithContentsOfURL エラーしました" & strErrorNO & strErrorMes |
| 109 | end if |
| 110 | ##NSDATA 保存 |
| 111 | set ocidOption to (refMe's NSDataWritingAtomic) |
| 112 | set listDone to ocidReadData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error|:(reference) |
| 113 | if (item 1 of listDone) is true then |
| 114 | log "ダウンロード 正常処理" |
| 115 | else if (item 2 of listDone) ≠ (missing value) then |
| 116 | set strErrorNO to (item 2 of listDone)'s code() as text |
| 117 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
| 118 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
| 119 | set listDone to ocidReadData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error|:(reference) |
| 120 | log "writeToURL エラーしました" & strErrorNO & strErrorMes |
| 121 | end if |
| 122 |
|
| 123 | ########################## |
| 124 | #アトリビュート削除 |
| 125 | log doZshShellScript("/usr/bin/xattr -d com.apple.provenance \"" & strSaveFilePath & "\"") |
| 126 | log doZshShellScript("/usr/bin/xattr -rc \"" & strSaveFilePath & "\"") |
| 127 | log "アトリビュート削除 OK" |
| 128 | ########################## |
| 129 | #マウント |
| 130 | set strCmd to ("/usr/bin/hdiutil attach \"" & strSaveFilePath & "\" -noverify -nobrowse -noautoopen -mountpoint \"" & strMountPointPath & "\"") as text |
| 131 | log doZshShellScript(strCmd) |
| 132 | log "マウント OK" |
| 133 | ########################## |
| 134 | #終了 |
| 135 | try |
| 136 | tell application id strBundleID to quit |
| 137 | delay 2 |
| 138 | end try |
| 139 |
|
| 140 |
|
| 141 | ########################## |
| 142 | #古いバージョンをゴミ箱へ |
| 143 | set ocidAppPathURL to (missing value) |
| 144 | set ocidAppBundle to (refMe's NSBundle's bundleWithIdentifier:(strBundleID)) |
| 145 | if ocidAppBundle = (missing value) then |
| 146 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
| 147 | set ocidAppApthURL to (appSharedWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID)) |
| 148 | else |
| 149 | #バンドルで見つかる場合 |
| 150 | set ocidAppApthURL to ocidAppBundle's bundleURL() |
| 151 | end if |
| 152 |
|
| 153 | if ocidAppPathURL = (missing value) then |
| 154 | try |
| 155 | tell application "Finder" |
| 156 | set aliasAppApth to (application file id strBundleID) as alias |
| 157 | end tell |
| 158 | set strAppApth to (POSIX path of aliasAppApth) as text |
| 159 | set ocidAppApthStr to refMe's NSString's stringWithString:(strAppApth) |
| 160 | set ocidAppApth to ocidAppApthStr's stringByStandardizingPath() |
| 161 | set ocidAppApthURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidAppApth) isDirectory:(false) |
| 162 | on error strErrMsg number numErrNo |
| 163 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 164 | set ocidAppApthURL to (missing value) |
| 165 | end try |
| 166 | end if |
| 167 | if ocidAppApthURL ≠ (missing value) then |
| 168 | set ocidAppApth to ocidAppApthURL's |path|() |
| 169 | set boolExists to appFileManager's fileExistsAtPath:(ocidAppApth) isDirectory:(false) |
| 170 | if boolExists is true then |
| 171 | #ゴミ箱へ入れる |
| 172 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 173 | set listDone to (appFileManager's trashItemAtURL:(ocidAppApthURL) resultingItemURL:(ocidAppPathURL) |error|:(reference)) |
| 174 | if (item 2 of listDone) ≠ (missing value) then |
| 175 | set strErrorNO to (item 2 of listDone)'s code() as text |
| 176 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
| 177 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
| 178 | log "エラーしました" & strErrorNO & strErrorMes |
| 179 | end if |
| 180 | end if |
| 181 | end if |
| 182 |
|
| 183 |
|
| 184 | ########################## |
| 185 | # コピー |
| 186 | log "インストール開始" |
| 187 | set listDone to (appFileManager's copyItemAtURL:(ocidMountAppPathURL) toURL:(ocidDistAppPathURL) |error|:(reference)) |
| 188 | if (first item of listDone) is false then |
| 189 | log "インストール失敗 コピーできない" |
| 190 | return false |
| 191 | end if |
| 192 |
|
| 193 | log "インストール終了" |
| 194 |
|
| 195 | ########################## |
| 196 | #アンマウント |
| 197 | set ocidDiskPathStr to refMe's NSString's stringWithString:(strMountPointPath) |
| 198 | set ocidDiskPath to ocidDiskPathStr's stringByExpandingTildeInPath() |
| 199 | set ocidDiskPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDiskPath) isDirectory:false) |
| 200 | set appNSWorkspace to refMe's NSWorkspace's sharedWorkspace() |
| 201 | set listResponse to (appNSWorkspace's unmountAndEjectDeviceAtURL:(ocidDiskPathURL) |error|:(reference)) |
| 202 | try |
| 203 | if (item 1 of listResponse) is falset then |
| 204 | |
| 205 | set strCmd to ("/usr/bin/hdiutil detach \"" & strMountPointPath & "\" -force") as text |
| 206 | set boolDone to doZshShellScript(strCmd) |
| 207 | if boolDone is false then |
| 208 | log "DMG アンマウントNG" |
| 209 | return false |
| 210 | end if |
| 211 | end if |
| 212 | end try |
| 213 | log "DMG アンマウントOK" |
| 214 | ########################## |
| 215 | #起動 |
| 216 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
| 217 | set boolDone to appSharedWorkspace's openURL:(ocidDistAppPathURL) |
| 218 |
|
| 219 | ########################## |
| 220 | #ダウンロードしたDMGをゴミ箱へ |
| 221 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 222 | set listDone to (appFileManager's trashItemAtURL:(ocidSaveFilePathURL) resultingItemURL:(missing value) |error|:(reference)) |
| 223 | if (item 2 of listDone) ≠ (missing value) then |
| 224 | set strErrorNO to (item 2 of listDone)'s code() as text |
| 225 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
| 226 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
| 227 | return "エラーしました" & strErrorNO & strErrorMes |
| 228 | end if |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 | # |
| 233 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
| 234 | set boolDone to appSharedWorkspace's openURL:(ocidDistDirPathURL) |
| 235 |
|
| 236 |
|
| 237 | return |
| 238 |
|
| 239 |
|
| 240 | ########################## |
| 241 | # 【通常】ZSH 実行 |
| 242 | to doZshShellScript(argCommandText) |
| 243 | set strCommandText to argCommandText as text |
| 244 | log "\r" & strCommandText & "\r" |
| 245 | set strExec to ("/bin/zsh -c '" & strCommandText & "'") as text |
| 246 | log "\r" & strExec & "\r" |
| 247 | ########## |
| 248 | #コマンド実行 |
| 249 | try |
| 250 | log "コマンド開始" |
| 251 | set strResnponse to (do shell script strExec) as text |
| 252 | log "コマンド終了" |
| 253 | on error |
| 254 | return false |
| 255 | end try |
| 256 | return strResnponse |
| 257 | end doZshShellScript |