
Githubバージョンチェッカー
【Safari・FireFox用Script Editorで開く】 |
| ソース | |
|---|---|
| 001 | #!/usr/bin/env osascript |
| 002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 003 | (* |
| 004 | githubのATOM RSSを取得してバージョン番号を取得します |
| 005 | v1 初回作成 |
| 006 | v1.1少し直した |
| 007 | v2 公開用 |
| 008 | v2.1 パスエスケープ処理を入れた |
| 009 | |
| 010 | com.cocolog-nifty.quicktimer.icefloe *) |
| 011 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 012 | use AppleScript version "2.8" |
| 013 | use framework "Foundation" |
| 014 | use framework "AppKit" |
| 015 | use scripting additions |
| 016 | property refMe : a reference to current application |
| 017 | |
| 018 | ########################## |
| 019 | #設定項目 |
| 020 | #Githubのtags.atom RSS |
| 021 | set strURL to ("https://github.com/swiftDialog/swiftDialog") as text |
| 022 | |
| 023 | ########################## |
| 024 | #URL |
| 025 | #set strRssURL to ("" & strURL & "/releases.atom") as text |
| 026 | set strRssURL to ("" & strURL & "/tags.atom") as text |
| 027 | #↑どっちのAtomを使うかはお好みで |
| 028 | set ocidURLString to refMe's NSString's stringWithString:(strRssURL) |
| 029 | set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLString) |
| 030 | set ocidRepoURL to ocidURL's URLByDeletingLastPathComponent() |
| 031 | set ocidRepoName to ocidRepoURL's lastPathComponent() |
| 032 | |
| 033 | ########################## |
| 034 | #NSDATA |
| 035 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
| 036 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error|:(reference) |
| 037 | if (2nd item of listResponse) = (missing value) then |
| 038 | log "initWithContentsOfURL 正常処理" |
| 039 | set ocidReadData to (item 1 of listResponse) |
| 040 | else 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 |
| 045 | end if |
| 046 | |
| 047 | ########################## |
| 048 | #NSXML |
| 049 | set ocidOption to (refMe's NSXMLDocumentTidyXML) |
| 050 | set listResponse to refMe's NSXMLDocument's alloc()'s initWithData:(ocidReadData) options:(ocidOption) |error|:(reference) |
| 051 | if (2nd item of listResponse) = (missing value) then |
| 052 | log "initWithData 正常処理" |
| 053 | set ocidReadXML to (item 1 of listResponse) |
| 054 | else 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 |
| 059 | end if |
| 060 | |
| 061 | ########################## |
| 062 | #バージョン取得 linkのURLを使う |
| 063 | set ocidRootElement to ocidReadXML's rootElement() |
| 064 | #リンクの最初の項目が最新 |
| 065 | set listResponse to (ocidRootElement's nodesForXPath:("//entry/link/@href") |error|:(reference)) |
| 066 | #【要カスタマイズ】ベータ版を含む場合 |
| 067 | set ocidLinkNode to (first item of listResponse)'s firstObject()'s stringValue() |
| 068 | set ocidLinkURL to refMe's NSURL's alloc()'s initWithString:(ocidLinkNode) |
| 069 | set ocidVerNo to ocidLinkURL's lastPathComponent() |
| 070 | set ocidVerNo to (ocidVerNo's stringByReplacingOccurrencesOfString:("v") withString:("")) |
| 071 | |
| 072 | #【要カスタマイズ】リリースバージョンの場合 |
| 073 | set ocidLinkNodeArray to (first item of listResponse) |
| 074 | repeat 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 |
| 083 | end repeat |
| 084 | set strVerNo to ocidVerNo as text |
| 085 | |
| 086 | ########################## |
| 087 | # |
| 088 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 089 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDocumentDirectory) inDomains:(refMe's NSUserDomainMask)) |
| 090 | set ocidDocumentDirPathURL to ocidURLsArray's firstObject() |
| 091 | set strSetSubDirName to ("githubバージョンチェッカ/" & ocidRepoName & "") as text |
| 092 | set ocidSaveDirPathURL to ocidDocumentDirPathURL's URLByAppendingPathComponent:(strSetSubDirName) isDirectory:(true) |
| 093 | # |
| 094 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init() |
| 095 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
| 096 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference) |
| 097 | # |
| 098 | set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("version.plist") isDirectory:(false) |
| 099 | set ocidSaveFilePath to ocidSaveFilePathURL's |path|() |
| 100 | # |
| 101 | set ocidSaveFilePathESC to doPathEscape(ocidSaveFilePath) |
| 102 | set strCmd to ("/usr/bin/defaults read \"" & ocidSaveFilePathESC & "\" version") as text |
| 103 | try |
| 104 | set strRecentVersion to (do shell script strCmd) as text |
| 105 | on 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 |
| 112 | end try |
| 113 | if 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 "新しいリリースがありました" |
| 141 | end if |
| 142 | |
| 143 | return strVerNo |
| 144 | |
| 145 | |
| 146 | ######################## |
| 147 | #パスのエスケープ |
| 148 | to 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 |
| 162 | end doPathEscape |
| 163 | ######################## |
| 164 | #置換 |
| 165 | to 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 |
| 173 | end doReplace |
| AppleScriptで生成しました | |
