[zoom]zoomインストール(pkgutilで解凍する)
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # セルフインストールのサンプル zoom |
005 | # pkgutil --expand-full で解凍する方法 |
006 | # |
007 | # com.cocolog-nifty.quicktimer.icefloe |
008 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
009 | use AppleScript version "2.8" |
010 | use framework "Foundation" |
011 | use framework "AppKit" |
012 | use scripting additions |
013 | |
014 | property refMe : a reference to current application |
015 | |
016 | |
017 | ################################## |
018 | ##【1】CPUタイプのよる処理の分岐 |
019 | set recordSysInfo to system info |
020 | set strCpuType to (CPU type of recordSysInfo) as text |
021 | if strCpuType contains "Intel" then |
022 | set strPkgURL to "https://zoom.us/client/latest/Zoom.pkg?archType=x86" as text |
023 | log strCpuType |
024 | else if strCpuType contains "ARM" then |
025 | set strPkgURL to "https://zoom.us/client/latest/Zoom.pkg?archType=arm64" as text |
026 | log strCpuType |
027 | else |
028 | return "不明なCUP処理終了" |
029 | end if |
030 | |
031 | ################################## |
032 | ##【2】ダウンロード |
033 | ##起動時に削除する項目 |
034 | set appFileManager to refMe's NSFileManager's defaultManager() |
035 | set ocidTempDirURL to appFileManager's temporaryDirectory() |
036 | ##フォルダ名はUUIDを使う |
037 | set ocidConcreteUUID to refMe's NSUUID's UUID() |
038 | set ocidUUIDString to ocidConcreteUUID's UUIDString() |
039 | set ocidTempDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true |
040 | ##フォルダを777アクセス権で作成 |
041 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
042 | ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions) |
043 | set boolDone to appFileManager's createDirectoryAtURL:(ocidTempDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error| :(reference) |
044 | log boolDone |
045 | set strTempDirPath to (ocidTempDirPathURL's |path|()) as text |
046 | ##ファイル名を取得 |
047 | try |
048 | set strCommandText to ("/usr/bin/curl -s -L -I -o /dev/null -w '%{url_effective}' \"" & strPkgURL & "\" | /usr/bin/rev | /usr/bin/cut -d'/' -f1 | /usr/bin/rev") as text |
049 | set strSaveFileName to (do shell script strCommandText) as text |
050 | on error |
051 | try |
052 | set strCommandText to ("/usr/bin/curl -s -L -I -o /dev/null -w '%{url_effective}' \"" & strPkgURL & "\" --http1.1 --connect-timeout 20 | /usr/bin/rev | /usr/bin/cut -d'/' -f1 | /usr/bin/rev") as text |
053 | set strSaveFileName to (do shell script strCommandText) as text |
054 | on error |
055 | set strSaveFileName to "install.pkg" |
056 | end try |
057 | end try |
058 | ##ダウンロード |
059 | try |
060 | set strCommandText to "/usr/bin/curl -L -o \"" & strTempDirPath & "/" & strSaveFileName & "\" \"" & strPkgURL & "\" --connect-timeout 20" as text |
061 | do shell script strCommandText |
062 | on error |
063 | try |
064 | set strCommandText to "/usr/bin/curl -L -o \"" & strTempDirPath & "/" & strSaveFileName & "\" \"" & strPkgURL & "\" --http1.1 --connect-timeout 20" as text |
065 | do shell script strCommandText |
066 | on error |
067 | return "インストーラーのダウンロードに失敗しました" |
068 | end try |
069 | end try |
070 | |
071 | |
072 | ################################## |
073 | ##【3】インストール前準備 |
074 | ###Dockのポジションを記憶 "/Library/Managed Preferences" は考慮しない |
075 | set strFilePath to "~/Library/Preferences/com.apple.dock.plist" as text |
076 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
077 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
078 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
079 | set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidFilePathURL) |
080 | set ocidPerAppsArray to ocidPlistDict's objectForKey:("persistent-apps") |
081 | set intArrayNo to (count of ocidPerAppsArray) as integer |
082 | set intCntNo to 0 as integer |
083 | repeat with itemerAppsArray in ocidPerAppsArray |
084 | set ocidTitleDataDict to (itemerAppsArray's objectForKey:("tile-data")) |
085 | set ocidFileDataDict to (ocidTitleDataDict's objectForKey:("file-data")) |
086 | set ocidCFURLString to (ocidFileDataDict's valueForKey:("_CFURLString")) |
087 | if (ocidCFURLString as text) contains ("zoom.us.app") then |
088 | exit repeat |
089 | end if |
090 | set intCntNo to intCntNo + 1 as integer |
091 | end repeat |
092 | if intArrayNo = intCntNo then |
093 | set intDocNo to (intArrayNo) as integer |
094 | log "Dockに登録されていません。一番下を指定します" |
095 | else |
096 | set intDocNo to (intCntNo) as integer |
097 | log "Dockの " & intCntNo & "番目に登録されています" |
098 | end if |
099 | |
100 | ###アプリケーション終了 |
101 | set listUTI to {"us.zoom.caphost", "us.zoom.xos", "us.zoom.CptHost", "us.zoom.Transcode", "us.zoom.ZMScreenshot"} as list |
102 | ###通常終了 |
103 | repeat with itemUTI in listUTI |
104 | ###NSRunningApplication |
105 | set ocidRunningApplication to refMe's NSRunningApplication |
106 | ###起動中のすべてのリスト |
107 | set ocidAppArray to (ocidRunningApplication's runningApplicationsWithBundleIdentifier:(itemUTI)) |
108 | ###複数起動時も順番に終了 |
109 | repeat with itemAppArray in ocidAppArray |
110 | itemAppArray's terminate() |
111 | end repeat |
112 | end repeat |
113 | delay 2 |
114 | ###強制終了 |
115 | repeat with itemUTI in listUTI |
116 | ###NSRunningApplication |
117 | set ocidRunningApplication to refMe's NSRunningApplication |
118 | ###起動中のすべてのリスト |
119 | set ocidAppArray to (ocidRunningApplication's runningApplicationsWithBundleIdentifier:(itemUTI)) |
120 | ###複数起動時も順番に終了 |
121 | repeat with itemAppArray in ocidAppArray |
122 | itemAppArray's forceTerminate() |
123 | end repeat |
124 | end repeat |
125 | ###ユーザーアプリケーションディレクトリ700で |
126 | set ocidURLsPathArray to (appFileManager's URLsForDirectory:(refMe's NSApplicationDirectory) inDomains:(refMe's NSUserDomainMask)) |
127 | set ocidAppDirPathURL to ocidURLsPathArray's firstObject() |
128 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
129 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
130 | set boolDone to appFileManager's createDirectoryAtURL:(ocidAppDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error| :(reference) |
131 | ###ローカライズファイル |
132 | set ocidLocalizedPathURL to ocidAppDirPathURL's URLByAppendingPathComponent:(".localized") |
133 | set ocidLocalizedPath to ocidLocalizedPathURL's |path|() |
134 | set boolDone to appFileManager's createFileAtPath:(ocidLocalizedPath) |contents|:(missing value) attributes:(ocidAttrDict) |
135 | ###Groupsフォルダ |
136 | set ocidGroupsDirPathURL to ocidAppDirPathURL's URLByAppendingPathComponent:("Groups") |
137 | set boolDone to appFileManager's createDirectoryAtURL:(ocidGroupsDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error| :(reference) |
138 | ###ローカライズファイル |
139 | set ocidLocalizedPathURL to ocidGroupsDirPathURL's URLByAppendingPathComponent:(".localized") |
140 | set ocidLocalizedPath to ocidLocalizedPathURL's |path|() |
141 | set boolDone to appFileManager's createFileAtPath:(ocidLocalizedPath) |contents|:(missing value) attributes:(ocidAttrDict) |
142 | |
143 | ###ゴミ箱 |
144 | set ocidURLsPathArray to (appFileManager's URLsForDirectory:(refMe's NSTrashDirectory) inDomains:(refMe's NSUserDomainMask)) |
145 | set ocidTrashDirPathURL to ocidURLsPathArray's firstObject() |
146 | |
147 | ################################################ |
148 | ###クリーニング |
149 | ################################################ |
150 | to doGoToTrash(arg_strFilePath) |
151 | set strFilePath to arg_strFilePath as text |
152 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
153 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
154 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath)) |
155 | set appFileManager to refMe's NSFileManager's defaultManager() |
156 | set boolDone to appFileManager's trashItemAtURL:(ocidFilePathURL) resultingItemURL:(ocidFilePathURL) |error| :(reference) |
157 | end doGoToTrash |
158 | |
159 | doGoToTrash("/Applications/zoom.us.app") |
160 | doGoToTrash("/Applications/ZoomOutlookPlugin") |
161 | doGoToTrash("~/Applications/zoom.us.app") |
162 | doGoToTrash("~/Applications/Groups/zoom.us.app") |
163 | delay 1 --> Dockからの削除待ち |
164 | doGoToTrash("~/Applications/ZoomOutlookPlugin") |
165 | doGoToTrash("~/Library/Caches/us.zoom.xos") |
166 | doGoToTrash("~/Library/WebKit/us.zoom.xos") |
167 | doGoToTrash("~/Library/Saved Application State/us.zoom.xos.savedState") |
168 | doGoToTrash("~/Library/Receipts/ZoomMacOutlookPlugin.pkg.plist") |
169 | doGoToTrash("~/Library/Receipts/ZoomMacOutlookPlugin.pkg.bom") |
170 | doGoToTrash("~/Library/Logs/ZoomPhone") |
171 | doGoToTrash("~/Library/Logs/zoom.us") |
172 | doGoToTrash("~/Library/HTTPStorages/us.zoom.xos.binarycookies") |
173 | doGoToTrash("~/Library/HTTPStorages/us.zoom.xos") |
174 | |
175 | delay 1 --> Dockからの削除待ち |
176 | |
177 | ################################## |
178 | ##【4】インストール本処理(ユーザーインストール) |
179 | ###パッケージがあるか?確認 |
180 | set strPkgPath to ("" & strTempDirPath & "/" & strSaveFileName & "") as text |
181 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
182 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
183 | set boolFileExist to appFileManager's fileExistsAtPath:(ocidFilePath) isDirectory:(false) |
184 | if boolFileExist = true then |
185 | # set strCommandText to "/usr/sbin/installer -pkg \"" & strTempDirPath & "/" & strSaveFileName & "\" -target CurrentUserHomeDirectory -dumplog -allowUntrusted -lang ja" as text |
186 | set strCommandText to ("/usr/sbin/pkgutil --expand-full \"" & strTempDirPath & "/" & strSaveFileName & "\" \"" & strTempDirPath & "/EXPAND" & "\"") as text |
187 | log strCommandText |
188 | do shell script strCommandText |
189 | else |
190 | return "パッケージが見つかりません" |
191 | end if |
192 | ###移動 |
193 | set ocidExpandAppPathURL to ocidTempDirPathURL's URLByAppendingPathComponent:("/EXPAND/zoomus.pkg/Payload/zoom.us.app") isDirectory:(false) |
194 | ###ユーザーインストールした場合のURL |
195 | set strAppPath to "~/Applications/Groups/zoom.us.app" as text |
196 | set ocidAppPathstr to refMe's NSString's stringWithString:(strAppPath) |
197 | set ocidAppPath to ocidAppPathstr's stringByStandardizingPath() |
198 | set ocidAppPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidAppPath) isDirectory:true |
199 | ###移動 |
200 | set ListDone to (appFileManager's moveItemAtURL:(ocidExpandAppPathURL) toURL:(ocidAppPathURL) |error| :(reference)) |
201 | |
202 | ##開く |
203 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
204 | set ocidOpenURLsArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0) |
205 | (ocidOpenURLsArray's addObject:(ocidAppPathURL)) |
206 | appSharedWorkspace's activateFileViewerSelectingURLs:(ocidOpenURLsArray) |
207 | |
208 | ################################## |
209 | ##【5】後処理 |
210 | |
211 | set coidAbsoluteStringPath to ocidAppPathURL's absoluteString() |
212 | |
213 | ################################ |
214 | ###PLIST 読み込み |
215 | set strFilePath to "~/Library/Preferences/com.apple.dock.plist" as text |
216 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
217 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
218 | set ocidPlistPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
219 | set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidPlistPathURL) |
220 | set ocidPersistentArray to ocidPlistDict's objectForKey:"persistent-apps" |
221 | |
222 | ################################ |
223 | ####persistent-appsデータ生成 |
224 | set ocidAddDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
225 | ###GUID実際はランダム番号だがNSFileSystemFileNumberを使う |
226 | set listPathAttar to appFileManager's attributesOfItemAtPath:(ocidAppPath) |error| :(reference) |
227 | set ocidAttar to item 1 of listPathAttar |
228 | set numGUID to ocidAttar's valueForKey:(refMe's NSFileSystemFileNumber) |
229 | set ocidIntValue to (refMe's NSNumber's numberWithInteger:(numGUID)) |
230 | ocidAddDict's setValue:(numGUID) forKey:("GUID") |
231 | ### |
232 | set ocidStringValue to (refMe's NSString's stringWithString:("file-tile")) |
233 | ocidAddDict's setValue:(ocidStringValue) forKey:("tile-type") |
234 | ################################ |
235 | set ocidAddTitleDataDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
236 | ####dock-extra |
237 | set ocidBoolValue to (refMe's NSNumber's numberWithBool:false) |
238 | ocidAddTitleDataDict's setValue:(ocidBoolValue) forKey:("dock-extra") |
239 | ####dock-extra |
240 | set ocidBoolValue to (refMe's NSNumber's numberWithBool:false) |
241 | ocidAddTitleDataDict's setValue:(ocidBoolValue) forKey:("is-beta") |
242 | ####bundle-identifier |
243 | set ocidAppBundle to (refMe's NSBundle's bundleWithURL:(ocidAppPathURL)) |
244 | set ocidBundleID to ocidAppBundle's bundleIdentifier |
245 | set ocidStringValue to ocidBundleID's UTF8String() |
246 | ocidAddTitleDataDict's setValue:(ocidStringValue) forKey:("bundle-identifier") |
247 | ####file-label" |
248 | set listAttributesDict to ocidAppPathURL's resourceValuesForKeys:({refMe's NSURLLocalizedNameKey}) |error| :(reference) |
249 | set ocidAttributesDict to (item 1 of listAttributesDict) |
250 | set ocidLocalizedName to (ocidAttributesDict's objectForKey:(refMe's NSURLLocalizedNameKey)) |
251 | set ocidFileLabel to (ocidLocalizedName's stringByDeletingPathExtension()) |
252 | set ocidStringValue to ocidFileLabel's UTF8String() |
253 | ocidAddTitleDataDict's setValue:(ocidStringValue) forKey:("file-label") |
254 | ####file-mod-date |
255 | set listAttributesDict to ocidAppPathURL's resourceValuesForKeys:({refMe's NSURLContentModificationDateKey}) |error| :(reference) |
256 | set ocidAttributesDict to (item 1 of listAttributesDict) |
257 | set ocidModificationDate to (ocidAttributesDict's objectForKey:(refMe's NSURLContentModificationDateKey)) |
258 | set ocidModDate to ocidModificationDate's timeIntervalSince1970 |
259 | set ocidIntValue to ocidModDate's intValue() |
260 | ocidAddTitleDataDict's setValue:(ocidIntValue) forKey:("file-mod-date") |
261 | ####parent-mod-date |
262 | set ocidNow to refMe's NSDate's now |
263 | set ocidNowNo to ocidNow's timeIntervalSince1970 |
264 | set ocidIntValue to ocidNowNo's intValue |
265 | ocidAddTitleDataDict's setValue:(ocidIntValue) forKey:("parent-mod-date") |
266 | ####book |
267 | set listBookMarkData to (ocidAppPathURL's bookmarkDataWithOptions:(11) includingResourceValuesForKeys:({missing value}) relativeToURL:(missing value) |error| :(reference)) |
268 | set ocidkDataValue to item 1 of listBookMarkData |
269 | (ocidAddTitleDataDict's setObject:(ocidkDataValue) forKey:("book")) |
270 | ####file-type |
271 | # 169 Launchpad とMission Control |
272 | # 41 それ以外はまぁ41で間違いなさそう |
273 | set ocidIntValue to (refMe's NSNumber's numberWithInteger:41) |
274 | (ocidAddTitleDataDict's setValue:(ocidIntValue) forKey:("file-type")) |
275 | ######### |
276 | set ocidAddFileDataDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
277 | (ocidAddFileDataDict's setValue:(coidAbsoluteStringPath) forKey:("_CFURLString")) |
278 | #### |
279 | # 0 /File/Path |
280 | # 15 file:// のURL形式 |
281 | set ocidIntValue to (refMe's NSNumber's numberWithInteger:15) |
282 | (ocidAddFileDataDict's setValue:(ocidIntValue) forKey:("_CFURLStringType")) |
283 | ######### |
284 | (ocidAddTitleDataDict's setObject:(ocidAddFileDataDict) forKey:("file-data")) |
285 | ocidAddDict's setObject:(ocidAddTitleDataDict) forKey:("tile-data") |
286 | ####追加データを元のPLISTに戻す |
287 | ocidPersistentArray's insertObject:(ocidAddDict) atIndex:(intDocNo) |
288 | ####保存 |
289 | set boolDone to ocidPlistDict's writeToURL:(ocidPlistPathURL) atomically:true |
290 | |
291 | |
292 | ###CFPreferencesを再起動 |
293 | try |
294 | set strCommandText to "/usr/bin/killall cfprefsd" as text |
295 | do shell script strCommandText |
296 | on error |
297 | set strPlistPath to "/System/Library/LaunchAgents/com.apple.cfprefsd.xpc.agent.plist" |
298 | set strCommandText to ("/bin/launchctl stop -w \"" & strAgentPath & "\"") |
299 | try |
300 | do shell script strCommandText |
301 | end try |
302 | set strCommandText to ("/bin/launchctl start -w \"" & strAgentPath & "\"") |
303 | try |
304 | do shell script strCommandText |
305 | end try |
306 | end try |
307 | |
308 | ######################## |
309 | ## Dock 再起動 |
310 | ######################## |
311 | |
312 | set strBundleID to "com.apple.dock" |
313 | |
314 | ######################## |
315 | ## 再起動 |
316 | ######################## |
317 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
318 | ##バンドルからアプリケーションのURLを取得 |
319 | set ocidAppBundle to (refMe's NSBundle's bundleWithIdentifier:(strBundleID)) |
320 | if ocidAppBundle ≠ (missing value) then |
321 | set ocidAppPathURL to ocidAppBundle's bundleURL() |
322 | else if ocidAppBundle = (missing value) then |
323 | set ocidAppPathURL to (appSharedWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID)) |
324 | end if |
325 | ##予備(アプリケーションのURL) |
326 | if ocidAppPathURL = (missing value) then |
327 | tell application "Finder" |
328 | try |
329 | set aliasAppApth to (application file id strBundleID) as alias |
330 | set strAppPath to POSIX path of aliasAppApth as text |
331 | set strAppPathStr to refMe's NSString's stringWithString:(strAppPath) |
332 | set strAppPath to strAppPathStr's stringByStandardizingPath() |
333 | set ocidAppPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(strAppPath) isDirectory:true |
334 | on error |
335 | return "アプリケーションが見つかりませんでした" |
336 | end try |
337 | end tell |
338 | end if |
339 | ##Dock終了 |
340 | set ocidRunningApplication to refMe's NSRunningApplication |
341 | ###起動中のすべてのリスト |
342 | set ocidAppArray to (ocidRunningApplication's runningApplicationsWithBundleIdentifier:(strBundleID)) |
343 | ###複数起動時も順番に終了 |
344 | repeat with itemAppArray in ocidAppArray |
345 | ###終了 |
346 | itemAppArray's terminate() |
347 | end repeat |
348 | ##1秒まって終了を確認 |
349 | delay 1 |
350 | ##終了できない場合は強制終了 |
351 | repeat with itemAppArray in ocidAppArray |
352 | set boolTerminate to itemAppArray's terminated |
353 | if boolTerminate = false then |
354 | itemAppArray's forceTerminate() |
355 | end if |
356 | end repeat |
357 | |
358 | ###起動 |
359 | set ocidOpenConfig to refMe's NSWorkspaceOpenConfiguration's configuration |
360 | ###コンフィグ |
361 | (ocidOpenConfig's setActivates:(refMe's NSNumber's numberWithBool:true)) |
362 | ###起動 |
363 | (appSharedWorkspace's openApplicationAtURL:(ocidAppPathURL) configuration:(ocidOpenConfig) completionHandler:(missing value)) |
364 | |
365 | return "再起動" |
AppleScriptで生成しました |
| 固定リンク