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