[Node]NodeJSアップデート 上書きインストール(PKG利用)
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | #com.cocolog-nifty.quicktimer.icefloe |
004 | #NODEJSアップデートインストール pkgを使います |
005 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
006 | use AppleScript version "2.8" |
007 | use framework "Foundation" |
008 | use framework "AppKit" |
009 | use scripting additions |
010 | |
011 | property refMe : a reference to current application |
012 | set appFileManager to refMe's NSFileManager's defaultManager() |
013 | |
014 | ###URL(どれでも好きなので) |
015 | set strURL to "https://nodejs.org/dist/latest/" as text |
016 | set strURL to "https://nodejs.org/dist/latest-v23.x/" as text |
017 | set strURL to "https://nodejs.org/dist/latest-v22.x/" as text |
018 | # |
019 | set ocidURLString to refMe's NSString's stringWithString:(strURL) |
020 | set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLString) |
021 | ##NSDATA |
022 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
023 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error| :(reference) |
024 | if (item 2 of listResponse) = (missing value) then |
025 | log "initWithContentsOfURL 正常処理" |
026 | set ocidReadData to (item 1 of listResponse) |
027 | else if (item 2 of listResponse) ≠ (missing value) then |
028 | set strErrorNO to (item 2 of listResponse)'s code() as text |
029 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
030 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
031 | return "initWithContentsOfURL エラーしました" & strErrorNO & strErrorMes |
032 | end if |
033 | ##XML |
034 | set ocidOption to (refMe's NSXMLNodePreserveAll) + (refMe's NSXMLDocumentTidyHTML) |
035 | set listResponse to refMe's NSXMLDocument's alloc()'s initWithData:(ocidReadData) options:(ocidOption) |error| :(reference) |
036 | if (item 2 of listResponse) = (missing value) then |
037 | log "正常処理" |
038 | set ocidXMLDoc to (item 1 of listResponse) |
039 | else if (item 2 of listResponse) ≠ (missing value) then |
040 | log (item 2 of listResponse)'s code() as text |
041 | log (item 2 of listResponse)'s localizedDescription() as text |
042 | log "NSXMLDocumentエラー 警告がありました" |
043 | set ocidXMLDoc to (item 1 of listResponse) |
044 | end if |
045 | #NODE |
046 | set listResponse to (ocidXMLDoc's nodesForXPath:("//*[@href[contains(., 'pkg')]]/attribute::href") |error| :(reference)) |
047 | set ocidPkgSubPathXML to (item 1 of listResponse)'s firstObject() |
048 | set ocidPkgSubPathString to ocidPkgSubPathXML's stringValue() |
049 | # |
050 | set strBaseURL to "https://nodejs.org" as text |
051 | set ocidBaseURLString to refMe's NSString's stringWithString:(strBaseURL) |
052 | set ocidBaseURL to refMe's NSURL's alloc()'s initWithString:(ocidBaseURLString) |
053 | set ocidPkgURL to ocidBaseURL's URLByAppendingPathComponent:(ocidPkgSubPathString) |
054 | #パッケージのURL |
055 | log ocidPkgURL's absoluteString() as text |
056 | #パッケージのファイル名 |
057 | set ocidPkgFileName to ocidPkgURL's lastPathComponent() |
058 | #PKGのバージョン |
059 | set ocidBasePkgFileName to ocidPkgFileName's stringByDeletingPathExtension()'s stringValue() |
060 | set ocidBasePkgFileName to ocidBasePkgFileName's mutableCopy() |
061 | set ocidPkgVer to (ocidBasePkgFileName's stringByReplacingOccurrencesOfString:("node-") withString:("")) |
062 | |
063 | ################################################ |
064 | ###### インストール済みのパージョン |
065 | ################################################ |
066 | set strShellPath to doZshShellScript("/bin/echo $PATH:/usr/local/bin:/opt/homebrew/bin/") |
067 | set strNodePath to doZshShellScript("PATH=$PATH:" & strShellPath & ";/usr/bin/which node") |
068 | set strNodeVer to doZshShellScript("\"" & strNodePath & "\" -v") |
069 | |
070 | if (ocidPkgVer as text) is (strNodeVer as text) then |
071 | display alert "最新版を利用中です" |
072 | return "最新版を利用中です" |
073 | end if |
074 | |
075 | ################################################ |
076 | ###### ダウンロードからインストールまで |
077 | ################################################ |
078 | set ocidTempDirURL to appFileManager's temporaryDirectory() |
079 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
080 | set ocidUUIDString to ocidUUID's UUIDString |
081 | set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:(true) |
082 | # |
083 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init() |
084 | ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions) |
085 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error| :(reference) |
086 | set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidPkgFileName) isDirectory:(false) |
087 | ##NSDATA |
088 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
089 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidPkgURL) options:(ocidOption) |error| :(reference) |
090 | if (item 2 of listResponse) = (missing value) then |
091 | log "initWithContentsOfURL 正常処理" |
092 | set ocidPkgData to (item 1 of listResponse) |
093 | else if (item 2 of listResponse) ≠ (missing value) then |
094 | set strErrorNO to (item 2 of listResponse)'s code() as text |
095 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
096 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
097 | return "initWithContentsOfURL エラーしました" & strErrorNO & strErrorMes |
098 | end if |
099 | ##NSDATA |
100 | set ocidOption to (refMe's NSDataWritingAtomic) |
101 | set listDone to ocidPkgData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error| :(reference) |
102 | if (item 1 of listDone) is true then |
103 | log "writeToURL 正常処理" |
104 | else if (item 2 of listDone) ≠ (missing value) then |
105 | set strErrorNO to (item 2 of listDone)'s code() as text |
106 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
107 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
108 | return "writeToURL エラーしました" & strErrorNO & strErrorMes |
109 | end if |
110 | # |
111 | set strPkgFilePath to ocidSaveFilePathURL's |path|() as text |
112 | log doZshShellScriptSudo("/usr/sbin/installer -pkg \"" & strPkgFilePath & "\" -target / -dumplog -allowUntrusted -lang ja") |
113 | |
114 | return |
115 | ########################## |
116 | # 【非ログイン】ZSH 実行 |
117 | to doZshShellScript(argCommandText) |
118 | set strCommandText to argCommandText as text |
119 | log "コマンド開始\r" & strCommandText & "\r" |
120 | set strExec to ("/bin/zsh -c '" & strCommandText & "'") as text |
121 | ########## |
122 | #コマンド実行 |
123 | try |
124 | set strResnponse to (do shell script strExec) as text |
125 | log "コマンド終了" |
126 | on error |
127 | return false |
128 | end try |
129 | return strResnponse |
130 | end doZshShellScript |
131 | |
132 | |
133 | |
134 | ########################## |
135 | # 【sudo】ZSH 実行 |
136 | to doZshShellScriptSudo(argCommandText) |
137 | set strCommandText to argCommandText as text |
138 | log "コマンド開始\r" & strCommandText & "\r" |
139 | set strExec to ("/bin/zsh -l -c '/usr/bin/sudo " & strCommandText & "'") as text |
140 | ########## |
141 | #コマンド実行 |
142 | try |
143 | set strResnponse to (do shell script strExec) as text |
144 | log "コマンド終了" |
145 | on error |
146 | return false |
147 | end try |
148 | return strResnponse |
149 | end doZshShellScriptSudo |
150 | |
AppleScriptで生成しました |
| 固定リンク