20260223

[AppleScript]Githubリポジトリーバージョンチェッカー


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

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