001 | #!/usr/bin/env osascript |
---|
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
---|
003 | (* |
---|
004 | com.cocolog-nifty.quicktimer.icefloe |
---|
005 | VMware Fusion最新版をダウンロード |
---|
006 | ダウンロードフォルダに保存します |
---|
007 | *) |
---|
008 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
---|
009 | use AppleScript version "2.8" |
---|
010 | use framework "Foundation" |
---|
011 | use framework "AppKit" |
---|
012 | use scripting additions |
---|
013 | property refMe : a reference to current application |
---|
014 | |
---|
015 | ### 【1】XMLのURL |
---|
016 | #サイトURL |
---|
017 | set strSiteURL to ("https://softwareupdate.vmware.com/cds/vmw-desktop/") as text |
---|
018 | set ocidSiteURL to refMe's NSURL's alloc()'s initWithString:(strSiteURL) |
---|
019 | #XMLのURL |
---|
020 | set strURL to ("https://softwareupdate.vmware.com/cds/vmw-desktop/fusion-universal.xml") as text |
---|
021 | #NSURLにして |
---|
022 | set ocidURL to refMe's NSURL's alloc()'s initWithString:(strURL) |
---|
023 | |
---|
024 | ### 【2】XML読み込み |
---|
025 | set ocidOption to (refMe's NSXMLDocumentTidyXML) |
---|
026 | set listResponse to refMe's NSXMLDocument's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error| :(reference) |
---|
027 | if (item 2 of listResponse) = (missing value) then |
---|
028 | set ocidReadXMLDoc to (item 1 of listResponse) |
---|
029 | else if (item 2 of listResponse) ≠ (missing value) then |
---|
030 | set strErrorNO to (item 2 of listResponse)'s code() as text |
---|
031 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
---|
032 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
---|
033 | log "エラーしました" & strErrorNO & strErrorMes |
---|
034 | set ocidReadXMLDoc to (item 1 of listResponse) |
---|
035 | end if |
---|
036 | #格納用のARRAY |
---|
037 | set ocidXMLURLArrayM to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
---|
038 | ###【3】XMLから必要なデータを取得 |
---|
039 | #【3-1】:ROOT |
---|
040 | set ocidRootElement to ocidReadXMLDoc's rootElement() |
---|
041 | #【3-2】:metadata |
---|
042 | ##metadataを要素として取得してから処理する |
---|
043 | set ocidElementArray to ocidRootElement's elementsForName:("metadata") |
---|
044 | repeat with itemElement in ocidElementArray |
---|
045 | set ocidUrlPath to (itemElement's elementsForName:("url"))'s firstObject() |
---|
046 | set ocidSitePath to ocidUrlPath's stringValue() |
---|
047 | set ocidItemURL to (ocidSiteURL's URLByAppendingPathComponent:(ocidSitePath) isDirectory:(false)) |
---|
048 | set ocidItemURLstring to ocidItemURL's absoluteString() as text |
---|
049 | (ocidXMLURLArrayM's addObject:(ocidItemURLstring)) |
---|
050 | end repeat |
---|
051 | |
---|
052 | ##Arrayをソート |
---|
053 | set ocidSortedArray to ocidXMLURLArrayM's sortedArrayUsingSelector:("localizedStandardCompare:") |
---|
054 | #バージョン数のもっとも大きい数字が最新だから |
---|
055 | set ocidFileURL to ocidSortedArray's lastObject() |
---|
056 | #最新版のXMLのURL |
---|
057 | set ocidXMLURL to refMe's NSURL's alloc()'s initWithString:(ocidFileURL) |
---|
058 | #XMLのファイル名 |
---|
059 | set ocidFileName to ocidXMLURL's lastPathComponent() |
---|
060 | #NSDATAに読み込む |
---|
061 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
---|
062 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidXMLURL) options:(ocidOption) |error| :(reference) |
---|
063 | if (item 2 of listResponse) = (missing value) then |
---|
064 | set ocidSetImageData to (item 1 of listResponse) |
---|
065 | else if (item 2 of listResponse) ≠ (missing value) then |
---|
066 | set strErrorNO to (item 2 of listResponse)'s code() as text |
---|
067 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
---|
068 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
---|
069 | return "エラーしました" & strErrorNO & strErrorMes |
---|
070 | end if |
---|
071 | |
---|
072 | ##保存 |
---|
073 | #テンポラリに保存 |
---|
074 | set appFileManager to refMe's NSFileManager's defaultManager() |
---|
075 | set ocidTempDirURL to appFileManager's temporaryDirectory() |
---|
076 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
---|
077 | set ocidUUIDString to ocidUUID's UUIDString |
---|
078 | set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:(true) |
---|
079 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
---|
080 | ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions) |
---|
081 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
---|
082 | if (item 1 of listDone) is true then |
---|
083 | #パスを整形しておく |
---|
084 | set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidFileName) isDirectory:(false) |
---|
085 | set ocidExtractXMLFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("metadata.xml") isDirectory:(false) |
---|
086 | ##NSDataで保存 |
---|
087 | set ocidOption to (refMe's NSDataWritingAtomic) |
---|
088 | set listDone to ocidSetImageData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error| :(reference) |
---|
089 | if (item 1 of listDone) is true then |
---|
090 | log "正常処理" |
---|
091 | else if (item 2 of listDone) ≠ (missing value) then |
---|
092 | set strErrorNO to (item 2 of listDone)'s code() as text |
---|
093 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
---|
094 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
---|
095 | return "エラーしました" & strErrorNO & strErrorMes |
---|
096 | end if |
---|
097 | set strSaveFilePath to ocidSaveFilePathURL's |path|() as text |
---|
098 | else if (item 2 of listDone) ≠ (missing value) then |
---|
099 | set strErrorNO to (item 2 of listDone)'s code() as text |
---|
100 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
---|
101 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
---|
102 | return "エラーしました" & strErrorNO & strErrorMes |
---|
103 | end if |
---|
104 | #保存したファイルを解凍して |
---|
105 | set strCommandText to ("/bin/zsh -c '/usr/bin/gzip -d \"" & strSaveFilePath & "\"'") as text |
---|
106 | log strCommandText |
---|
107 | try |
---|
108 | do shell script strCommandText |
---|
109 | on error |
---|
110 | return "gzipでエラーになりました" |
---|
111 | end try |
---|
112 | |
---|
113 | ###### |
---|
114 | #解凍したXMLを読み込む |
---|
115 | set ocidOption to (refMe's NSXMLDocumentTidyXML) |
---|
116 | set listResponse to refMe's NSXMLDocument's alloc()'s initWithContentsOfURL:(ocidExtractXMLFilePathURL) options:(ocidOption) |error| :(reference) |
---|
117 | if (item 2 of listResponse) = (missing value) then |
---|
118 | set ocidReadXMLDoc to (item 1 of listResponse) |
---|
119 | else if (item 2 of listResponse) ≠ (missing value) then |
---|
120 | set strErrorNO to (item 2 of listResponse)'s code() as text |
---|
121 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
---|
122 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
---|
123 | log "エラーしました" & strErrorNO & strErrorMes |
---|
124 | set ocidReadXMLDoc to (item 1 of listResponse) |
---|
125 | end if |
---|
126 | #ビルド番号 |
---|
127 | set ocidRootElement to ocidReadXMLDoc's rootElement() |
---|
128 | set listResponse to (ocidRootElement's nodesForXPath:("//productList/product/buildNumber") |error| :(reference)) |
---|
129 | set ocidBuildNumber to (item 1 of listResponse)'s firstObject() |
---|
130 | set ocidBuldName to ocidBuildNumber's stringValue() |
---|
131 | #バージョン |
---|
132 | set listResponse to (ocidRootElement's nodesForXPath:("//productList/product/version") |error| :(reference)) |
---|
133 | set ocidVersionNumber to (item 1 of listResponse)'s firstObject() |
---|
134 | set ocidVersionName to ocidVersionNumber's stringValue() as text |
---|
135 | #ファイル名 |
---|
136 | set listResponse to (ocidRootElement's nodesForXPath:("//componentList/component/relativePath") |error| :(reference)) |
---|
137 | set ocidRelativePath to (item 1 of listResponse)'s firstObject() |
---|
138 | set ocidTarFileName to ocidRelativePath's stringValue() as text |
---|
139 | #パスを繋げてURLにして |
---|
140 | set strSetPath to ("/cds/vmw-desktop/fusion/" & ocidVersionName & "/" & ocidBuldName & "/universal/core/" & ocidTarFileName) as text |
---|
141 | set ocidURLComponents to refMe's NSURLComponents's alloc()'s init() |
---|
142 | ocidURLComponents's setScheme:("https") |
---|
143 | ocidURLComponents's setHost:("softwareupdate.vmware.com") |
---|
144 | ocidURLComponents's setPath:(strSetPath) |
---|
145 | set ocidTarURL to ocidURLComponents's |URL|() |
---|
146 | log ocidTarURL's absoluteString() as text |
---|
147 | |
---|
148 | #NSDATAに読み込む ダウンロード |
---|
149 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
---|
150 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidTarURL) options:(ocidOption) |error| :(reference) |
---|
151 | if (item 2 of listResponse) = (missing value) then |
---|
152 | set ocidSetImageData to (item 1 of listResponse) |
---|
153 | else 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 "エラーしました" & strErrorNO & strErrorMes |
---|
158 | end if |
---|
159 | #ダウンロードフォルダに指定のファイル名で |
---|
160 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask)) |
---|
161 | set ocidDownloadsDirPathURL to ocidURLsArray's firstObject() |
---|
162 | set ocidSaveFilePathURL to ocidDownloadsDirPathURL's URLByAppendingPathComponent:(ocidTarFileName) isDirectory:false |
---|
163 | #保存 |
---|
164 | set ocidOption to (refMe's NSDataWritingAtomic) |
---|
165 | set listDone to ocidSetImageData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error| :(reference) |
---|
166 | if (item 1 of listDone) is true then |
---|
167 | log "正常終了" |
---|
168 | else if (item 1 of listDone) is false then |
---|
169 | log (item 2 of listDone)'s localizedDescription() as text |
---|
170 | return "保存に失敗しました" |
---|
171 | end if |
---|
172 | #保存先を開く |
---|
173 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
---|
174 | set boolDone to appSharedWorkspace's openURL:(ocidDownloadsDirPathURL) |
---|
175 | |
---|
176 | |
---|
177 | return "終了" |
---|