20260409

Microsoft Edge For macOS インストールスクリプト(ユーザー権限でOK)


Microsoft Edge For macOS インストールスクリプト(ユーザー権限でOK)

NOTE記事一覧ですnote.com

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

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