20260527

[7zz]インストール・アップデートヘルパースクリプトAS

[7zz]インストール・アップデートヘルパースクリプトAS

NOTE記事一覧ですnote.com

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

7zzインストール(Application 7zzSupport).applescript.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
004(*
005
006ユーザーの ~/Library/Application Support/bin にインストールする
007
008githubのRSSから対象のバイナリーをダウンロード展開する
0097zz インストール
010
011v2 20250422 XMLとHTMLで処理をわけた
012
013com.cocolog-nifty.quicktimer.icefloe *)
014----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
015use AppleScript version "2.8"
016use framework "Foundation"
017use framework "AppKit"
018use framework "UniformTypeIdentifiers"
019use scripting additions
020property refMe : a reference to current application
021
022
023###################
024#【設定項目1】展開(解凍)先
025#インストール場所はお好みで
026#   set str7zPath to ("~/bin/7zip/7zz") as text
027set str7zPath to ("~/Library/Application Support/bin/7zip/7zz") as text
028
029###################
030#【設定項目2】GitHubURL
031set strRepositoryURL to ("https://github.com/ip7z/7zip") as text
032
033
034###################
035#なければインストール
036set ocid7zPathStr to refMe's NSString's stringWithString:(str7zPath)
037set ocid7zPath to ocid7zPathStr's stringByStandardizingPath()
038set ocid7zPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocid7zPath) isDirectory:(false)
039set appFileManager to refMe's NSFileManager's defaultManager()
040set boolDirExists to appFileManager's fileExistsAtPath:(ocid7zPath) isDirectory:(true)
041if boolDirExists is true then
042   log "インストール済み"
043else
044   log "インストールします"
045end if
046
047##########################
048# インストール先フォルダを確保
049set ocidBinDirPathURL to ocid7zPathURL's URLByDeletingLastPathComponent()
050set strBinDirPath to ocidBinDirPathURL's |path|() as text
051#BinDirインストール先
052set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
053ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
054set listDone to appFileManager's createDirectoryAtURL:(ocidBinDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference)
055
056##########################
057# GitHub RSSのURL
058set strRssURL to ("" & strRepositoryURL & "/tags.atom") as text
059set ocidURLString to refMe's NSString's stringWithString:(strRssURL)
060set ocidRssURL to refMe's NSURL's alloc()'s initWithString:(ocidURLString)
061log ocidRssURL's absoluteString() as text
062
063##########################
064#XML取得
065set ocidReadXML to doGetRootElementXML(ocidRssURL)
066if ocidReadXML is false then
067   return "XMLの取得に失敗しました"
068end if
069
070
071##########################
072#バージョン取得 linkのURLを使う
073set ocidRootElement to ocidReadXML's rootElement()
074#リンクの最初の項目が最新
075set listResponse to (ocidRootElement's nodesForXPath:("//entry/link/@href") |error|:(reference))
076set ocidLinkElementArray to (item 1 of listResponse)
077#▼▼▼▼【要カスタマイズ】Arrayの何コ目が対象か?
078set ocidFirstElement to ocidLinkElementArray's objectAtIndex:(0)
079set ocidFirstElement to ocidFirstElement's stringValue()
080#リリースページのURL
081set ocidLinkURL to refMe's NSURL's alloc()'s initWithString:(ocidFirstElement)
082log ocidLinkURL's absoluteString() as text
083#ラストパス=バージョン
084set ocidVerNo to ocidLinkURL's lastPathComponent()
085#【要customize】ここはアプリケーションによってカスタマイズが必要
086set ocidVerNo to (ocidVerNo's stringByReplacingOccurrencesOfString:("v") withString:(""))
087#カンマ有りのバージョン番号
088set strVerNo to ocidVerNo as text
089#カンマ無しのバージョン番号
090set ocidVerNoStr to (ocidVerNo's stringByReplacingOccurrencesOfString:(".") withString:(""))
091set strVerNoStr to ocidVerNoStr as text
092
093##########################
094#リリースアセットのHTMLパーツ部分
095set strAssetsURL to ("" & strRepositoryURL & "/releases/expanded_assets/" & strVerNo & "") as text
096set ocidAssetsURL to (refMe's NSURL's alloc()'s initWithString:(strAssetsURL))
097log ocidAssetsURL's absoluteString() as text
098
099##########################
100#HTML取得
101set ocidReadXML to doGetRootElementHTML(ocidAssetsURL)
102
103##########################
104#バージョン取得 linkのURLを使う
105set ocidRootElement to ocidReadXML's rootElement()
106#   log ocidRootElement's XMLString() as text
107#リンクの最初の項目が最新
108set listResponse to (ocidRootElement's nodesForXPath:("//div/ul/li/div/a/@href") |error|:(reference))
109set ocidReleaseNodeArray to (item 1 of listResponse)
110#▼▼▼▼【要カスタマイズ】 アセットの何番目が対象か?
111set ocidReleaseNode to ocidReleaseNodeArray's objectAtIndex:(7)
112set ocidReleasePath to ocidReleaseNode's stringValue()
113
114#URLコンポーネント初期化
115set ocidURLComponents to refMe's NSURLComponents's alloc()'s init()
116###スキーム を追加
117ocidURLComponents's setScheme:("https")
118###ホストを追加
119ocidURLComponents's setHost:("github.com")
120###パスを追加
121ocidURLComponents's setPath:(ocidReleasePath)
122##URLに戻して テキストにしておく
123set ocidReleaseURL to ocidURLComponents's |URL|()
124log ocidReleaseURL's absoluteString() as text
125#
126set ocidReleaseFileName to ocidReleaseURL's lastPathComponent()
127
128
129##########################
130#保存先テンポラリーディレクトリ
131set ocidTempDirURL to appFileManager's temporaryDirectory()
132set ocidUUID to refMe's NSUUID's alloc()'s init()
133set ocidUUIDString to ocidUUID's UUIDString
134set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:(true)
135
136#フォルダ作成
137set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
138ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
139set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference)
140
141###パス
142set ocidDownloadFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidReleaseFileName) isDirectory:(false)
143set strDownloadFilePath to ocidDownloadFilePathURL's |path|() as text
144
145log "ダウンロード開始"
146##########################
147#NSDATA
148set ocidOption to (refMe's NSDataReadingMappedIfSafe)
149set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidReleaseURL) options:(ocidOption) |error|:(reference)
150if (item 2 of listResponse) = (missing value) then
151   log "initWithContentsOfURL 正常処理"
152   set ocidReadData to (item 1 of listResponse)
153else if (item 2 of listResponse) ≠ (missing value) then
154   set strErrorNO to (item 2 of listResponse)'s code() as text
155   set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text
156   refMe's NSLog("■:" & strErrorNO & strErrorMes)
157   return "initWithContentsOfURL エラーしました" & strErrorNO & strErrorMes
158end if
159
160##NSDATA
161set ocidOption to (refMe's NSDataWritingAtomic)
162set listDone to ocidReadData's writeToURL:(ocidDownloadFilePathURL) options:(ocidOption) |error|:(reference)
163if (item 1 of listDone) is true then
164   log "writeToURL ダウンロード正常処理"
165else if (item 2 of listDone) ≠ (missing value) then
166   set strErrorNO to (item 2 of listDone)'s code() as text
167   set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
168   refMe's NSLog("■:" & strErrorNO & strErrorMes)
169   return "writeToURL エラーしました" & strErrorNO & strErrorMes
170end if
171
172##########################
173#▼▼▼▼【要カスタマイズ】
174#提供ファイル形式によってカスタマイズ
175set strCommandText to ("/usr/bin/bsdtar -xJf \"" & strDownloadFilePath & "\" -C \"" & strBinDirPath & "\"")
176log doZshShellScript(strCommandText)
177
178##########################
179#拡張属性Extended Attributes (EA)削除
180set strCommandText to ("/usr/bin/xattr -cr \"" & strBinDirPath & "\"")
181log doZshShellScript(strCommandText)
182
183##########################
184#インストール先を開く
185set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
186set boolDone to appSharedWorkspace's openURL:(ocidBinDirPathURL)
187
188return
189
190##########################
191# 【通常】ZSH 実行
192to doZshShellScript(argCommandText)
193   set strCommandText to argCommandText as text
194   log return & strCommandText & return
195   set strExec to ("/bin/zsh -c '" & strCommandText & "'") as text
196   log return & strExec & return
197   ##########
198   #コマンド実行
199   try
200      log "コマンド開始"
201      set strResnponse to (do shell script strExec) as text
202      log "コマンド終了"
203   on error
204      return false
205   end try
206   return true
207end doZshShellScript
208
209
210
211##########################
212#XML取得
213to doGetRootElementXML(argURL)
214   ##########################
215   #NSDATA
216   set ocidOption to (refMe's NSXMLNodePreserveAll) + (refMe's NSDataReadingMappedIfSafe)
217   set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(argURL) options:(ocidOption) |error|:(reference)
218   if (item 2 of listResponse) = (missing value) then
219      #      log "initWithContentsOfURL 正常処理"
220      set ocidReadData to (item 1 of listResponse)
221   else if (item 2 of listResponse) ≠ (missing value) then
222      set strErrorNO to (item 2 of listResponse)'s code() as text
223      set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text
224      refMe's NSLog("■:" & strErrorNO & strErrorMes)
225      log "initWithContentsOfURL エラーしました" & strErrorNO & strErrorMes
226      return false
227   end if
228   ##########################
229   #NSXML
230   set ocidOption to (refMe's NSXMLDocumentTidyXML)
231   set listResponse to refMe's NSXMLDocument's alloc()'s initWithData:(ocidReadData) options:(ocidOption) |error|:(reference)
232   if (item 2 of listResponse) = (missing value) then
233      #      log "initWithData 正常処理"
234      set ocidReadXML to (item 1 of listResponse)
235   else if (item 2 of listResponse) ≠ (missing value) then
236      set strErrorNO to (item 2 of listResponse)'s code() as text
237      set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text
238      refMe's NSLog("■:" & strErrorNO & strErrorMes)
239      log "initWithData エラーしました" & strErrorNO & strErrorMes
240   end if
241   return ocidReadXML
242end doGetRootElementXML
243
244
245
246##########################
247#HTML取得
248to doGetRootElementHTML(argURL)
249   ##########################
250   #NSDATA
251   set ocidOption to (refMe's NSDataReadingMappedIfSafe)
252   set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(argURL) options:(ocidOption) |error|:(reference)
253   if (item 2 of listResponse) = (missing value) then
254      log "initWithContentsOfURL 正常処理"
255      set ocidReadData to (item 1 of listResponse)
256   else if (item 2 of listResponse) ≠ (missing value) then
257      set strErrorNO to (item 2 of listResponse)'s code() as text
258      set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text
259      refMe's NSLog("■:" & strErrorNO & strErrorMes)
260      log "initWithContentsOfURL エラーしました" & strErrorNO & strErrorMes
261      return false
262   end if
263   ##########################
264   #NSXML
265   set ocidOption to (refMe's NSXMLNodePreserveAll) + (refMe's NSXMLDocumentTidyHTML)
266   set listResponse to refMe's NSXMLDocument's alloc()'s initWithData:(ocidReadData) options:(ocidOption) |error|:(reference)
267   if (item 2 of listResponse) = (missing value) then
268      log "initWithData 正常処理"
269      set ocidReadHTML to (item 1 of listResponse)
270   else if (item 2 of listResponse) ≠ (missing value) then
271      set ocidReadHTML to (item 1 of listResponse)
272      #HTMLの場合エラーのログは出すけどエラーで終了にはしない
273      set strErrorNO to (item 2 of listResponse)'s code() as text
274      set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text
275      refMe's NSLog("■:" & strErrorNO & strErrorMes)
276      log "initWithData エラーしました" & strErrorNO & strErrorMes
277   end if
278   return ocidReadHTML
279end doGetRootElementHTML
AppleScriptで生成しました