20260607

[Applescript]ショートカット(Shortcuts)用 Githubバージョンチェッカー

Githubバージョンチェッカー

NOTE記事一覧ですnote.com
 

【Safari・FireFox用Script Editorで開く】 |

githubバージョンチェックv2.scpt.scpt
ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004githubのATOM RSSを取得してバージョン番号を取得します
005v1 初回作成
006v1.1少し直した
007v2 公開用
008v2.1 パスエスケープ処理を入れた
009
010com.cocolog-nifty.quicktimer.icefloe *)
011----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
012use AppleScript version "2.8"
013use framework "Foundation"
014use framework "AppKit"
015use scripting additions
016property refMe : a reference to current application
017
018##########################
019#設定項目 
020#Githubのtags.atom RSS
021set strURL to ("https://github.com/swiftDialog/swiftDialog") as text
022
023##########################
024#URL
025#set strRssURL to ("" & strURL & "/releases.atom") as text
026set strRssURL to ("" & strURL & "/tags.atom") as text
027#↑どっちのAtomを使うかはお好みで
028set ocidURLString to refMe's NSString's stringWithString:(strRssURL)
029set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLString)
030set ocidRepoURL to ocidURL's URLByDeletingLastPathComponent()
031set ocidRepoName to ocidRepoURL's lastPathComponent()
032
033##########################
034#NSDATA
035set ocidOption to (refMe's NSDataReadingMappedIfSafe)
036set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error|:(reference)
037if (2nd item of listResponse) = (missing value) then
038   log "initWithContentsOfURL 正常処理"
039   set ocidReadData to (item 1 of listResponse)
040else if (2nd item of listResponse) ≠ (missing value) then
041   set strErrorNO to (2nd item of listResponse)'s code() as text
042   set strErrorMes to (2nd item of listResponse)'s localizedDescription() as text
043   refMe's NSLog("■:" & strErrorNO & strErrorMes)
044   return "initWithContentsOfURL エラーしました" & strErrorNO & strErrorMes
045end if
046
047##########################
048#NSXML
049set ocidOption to (refMe's NSXMLDocumentTidyXML)
050set listResponse to refMe's NSXMLDocument's alloc()'s initWithData:(ocidReadData) options:(ocidOption) |error|:(reference)
051if (2nd item of listResponse) = (missing value) then
052   log "initWithData 正常処理"
053   set ocidReadXML to (item 1 of listResponse)
054else if (2nd item of listResponse) ≠ (missing value) then
055   set strErrorNO to (2nd item of listResponse)'s code() as text
056   set strErrorMes to (2nd item of listResponse)'s localizedDescription() as text
057   refMe's NSLog("■:" & strErrorNO & strErrorMes)
058   return "initWithData エラーしました" & strErrorNO & strErrorMes
059end if
060
061##########################
062#バージョン取得 linkのURLを使う
063set ocidRootElement to ocidReadXML's rootElement()
064#リンクの最初の項目が最新
065set listResponse to (ocidRootElement's nodesForXPath:("//entry/link/@href") |error|:(reference))
066#【要カスタマイズ】ベータ版を含む場合
067set ocidLinkNode to (first item of listResponse)'s firstObject()'s stringValue()
068set ocidLinkURL to refMe's NSURL's alloc()'s initWithString:(ocidLinkNode)
069set ocidVerNo to ocidLinkURL's lastPathComponent()
070set ocidVerNo to (ocidVerNo's stringByReplacingOccurrencesOfString:("v") withString:(""))
071
072#【要カスタマイズ】リリースバージョンの場合
073set ocidLinkNodeArray to (first item of listResponse)
074repeat with itemNode in ocidLinkNodeArray
075   set ocidLinkNode to itemNode's stringValue()
076   set ocidLinkURL to (refMe's NSURL's alloc()'s initWithString:(ocidLinkNode))
077   set ocidVerNo to ocidLinkURL's lastPathComponent()
078   set boolContainBeta to (ocidVerNo's containsString:("b"))
079   if boolContainBeta is false then
080      set ocidVerNo to (ocidVerNo's stringByReplacingOccurrencesOfString:("v") withString:(""))
081      exit repeat
082   end if
083end repeat
084set strVerNo to ocidVerNo as text
085
086##########################
087#
088set appFileManager to refMe's NSFileManager's defaultManager()
089set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDocumentDirectory) inDomains:(refMe's NSUserDomainMask))
090set ocidDocumentDirPathURL to ocidURLsArray's firstObject()
091set strSetSubDirName to ("githubバージョンチェッカ/" & ocidRepoName & "") as text
092set ocidSaveDirPathURL to ocidDocumentDirPathURL's URLByAppendingPathComponent:(strSetSubDirName) isDirectory:(true)
093#
094set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
095ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
096set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference)
097#
098set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("version.plist") isDirectory:(false)
099set ocidSaveFilePath to ocidSaveFilePathURL's |path|()
100#
101set ocidSaveFilePathESC to doPathEscape(ocidSaveFilePath)
102set strCmd to ("/usr/bin/defaults read \"" & ocidSaveFilePathESC & "\" version") as text
103try
104   set strRecentVersion to (do shell script strCmd) as text
105on error strErrMsg number numErrNo
106   log "No: " & numErrNo & linefeed & "Error:" & strErrMsg
107   log "たぶん初回作成時エラー"
108   set ocidSaveFilePathESC to doPathEscape(ocidSaveFilePath)
109   set strCmd to ("/usr/bin/defaults write \"" & ocidSaveFilePath & "\" version -string " & ocidVerNo & "") as text
110   do shell script strCmd
111   return false
112end try
113if strRecentVersion is not strVerNo then
114   set strMsg to ("" & ocidRepoName & "に新しいリリースがありました") as text
115   set strOK to "OK" as text
116   set strCancel to "終了" as text
117   set strOpenURL to "URLを開く" as text
118   set listBottons to {strOK, strCancel, strOpenURL} as list
119   tell application "System Events"
120      activate
121      try
122         set recordResponse to (display alert strMsg buttons listBottons default button strOK cancel button strCancel as critical giving up after 20) as record
123      on error strErrMsg number numErrNo
124         log strErrMsg & numErrNo
125         return false
126      end try
127   end tell
128   if (gave up of recordResponse) is true then
129      return "時間切れですやりなおしてください"
130      error number -128
131   else if strOK is equal to (button returned of recordResponse) then
132      set strResponse to (button returned of recordResponse) as text
133   else if strOpenURL is equal to (button returned of recordResponse) then
134      set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
135      set boolDone to appSharedWorkspace's openURL:(ocidRepoURL)
136   end if
137   set ocidSaveFilePathESC to doPathEscape(strFilePath)
138   set strCmd to ("/usr/bin/defaults write \"" & ocidSaveFilePathESC & "\" version -string " & ocidVerNo & "") as text
139   do shell script strCmd
140   return "新しいリリースがありました"
141end if
142
143return strVerNo
144
145
146########################
147#パスのエスケープ
148to doPathEscape(strFilePath)
149   set strFilePath to strFilePath as text
150   set listEscChar to {"\\", "\"", "$", "`"} as list
151   repeat with itemEscChar in listEscChar
152      set strSearchText to (itemEscChar) as text
153      set strReplaceText to ("\\" & itemEscChar & "") as text
154      set strFilePath to doReplace(strFilePath, strSearchText, strReplaceText)
155   end repeat
156   
157   set strSearchText to ("!") as text
158   set strReplaceText to ("\"'!'\"") as text
159   set strFilePath to doReplace(strFilePath, strSearchText, strReplaceText)
160   
161   return strFilePath
162end doPathEscape
163########################
164#置換
165to doReplace(argOrignalText, argSearchText, argReplaceText)
166   set strDelim to AppleScript's text item delimiters
167   set AppleScript's text item delimiters to argSearchText
168   set listDelim to every text item of argOrignalText
169   set AppleScript's text item delimiters to argReplaceText
170   set strReturn to listDelim as text
171   set AppleScript's text item delimiters to strDelim
172   return strReturn
173end doReplace
AppleScriptで生成しました