ファイル比較 (選んだファイルが同一の内容か?の判断)
こんな感じでHTMLで比較結果を表示します
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | #com.cocolog-nifty.quicktimer.icefloe |
004 | # 選んだファイルを比較して 内容が同じか?判断します |
005 | # 生成するHTMLは再起動時に削除されますので放置でOK |
006 | # |
007 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
008 | use AppleScript version "2.8" |
009 | use framework "Foundation" |
010 | use framework "UniformTypeIdentifiers" |
011 | use framework "AppKit" |
012 | |
013 | use scripting additions |
014 | property refMe : a reference to current application |
015 | set appFileManager to refMe's NSFileManager's defaultManager() |
016 | ###ダイアログ |
017 | set strName to (name of current application) as text |
018 | if strName is "osascript" then |
019 | tell application "Finder" to activate |
020 | else |
021 | tell current application to activate |
022 | end if |
023 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
024 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
025 | set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias |
026 | set listUTI to {"public.item"} |
027 | set strMes to ("ファイルを選んでください") as text |
028 | set strPrompt to ("ファイルを選んでください") as text |
029 | try |
030 | ### ファイル選択時 |
031 | set listAliasFilePath to (choose file strMes with prompt strPrompt default location aliasDefaultLocation of type listUTI with invisibles, multiple selections allowed and showing package contents) as list |
032 | on error |
033 | log "エラーしました" |
034 | return "エラーしました" |
035 | end try |
036 | if listAliasFilePath is {} then |
037 | return "選んでください" |
038 | end if |
039 | |
040 | ################## |
041 | #情報収集 |
042 | #出力用のDICT |
043 | set ocidResultDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0) |
044 | #選択した順番に出力したいので入力順をリストにしておく |
045 | set listKey to {} as list |
046 | #ファイルの数だけ繰り返し |
047 | repeat with itemAliasFilePath in listAliasFilePath |
048 | #出力用のDICTにセットするファイルの内容を格納するDICT |
049 | set ocidItemDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0) |
050 | #パス |
051 | set aliasItemFilePath to itemAliasFilePath as alias |
052 | set strItemFilePath to (POSIX path of aliasItemFilePath) as text |
053 | set ocidItemFilePathStr to (refMe's NSString's stringWithString:(strItemFilePath)) |
054 | set ocidItemFilePath to ocidItemFilePathStr's stringByStandardizingPath() |
055 | #ファイル名はキーに使う |
056 | set ocidFileName to (ocidItemFilePath's lastPathComponent()) |
057 | #順番用のリストに格納 |
058 | set end of listKey to (ocidFileName as text) |
059 | set ocidItemFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidItemFilePath) isDirectory:false) |
060 | #パスとURLを格納 |
061 | (ocidItemDict's setObject:(ocidItemFilePath) forKey:("PATH")) |
062 | (ocidItemDict's setObject:(ocidItemFilePathURL) forKey:("URL")) |
063 | #サイズ |
064 | set listResponse to (ocidItemFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLFileSizeKey) |error| :(reference)) |
065 | set ocidSetValue to (item 2 of listResponse) |
066 | (ocidItemDict's setObject:(ocidSetValue) forKey:("SIZE")) |
067 | #変更日 |
068 | set listResponse to (ocidItemFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentModificationDateKey) |error| :(reference)) |
069 | set ocidSetValue to (item 2 of listResponse) |
070 | (ocidItemDict's setObject:(ocidSetValue) forKey:("ModDate")) |
071 | #作成日 |
072 | set listResponse to (ocidItemFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLCreationDateKey) |error| :(reference)) |
073 | set ocidSetValue to (item 2 of listResponse) |
074 | (ocidItemDict's setObject:(ocidSetValue) forKey:("CreDate")) |
075 | #MD5 |
076 | set argCommandText to ("/sbin/md5 \"" & strItemFilePath & "\"") as text |
077 | set strValue to doExecTask(argCommandText) |
078 | set ocidValueStr to (refMe's NSString's stringWithString:(strValue)) |
079 | set ocidValueArray to (ocidValueStr's componentsSeparatedByString:("=")) |
080 | set ocidItemValue to ocidValueArray's lastObject() |
081 | set ocidItemValue to (ocidItemValue's stringByReplacingOccurrencesOfString:(" ") withString:("")) |
082 | set ocidItemValue to (ocidItemValue's stringByReplacingOccurrencesOfString:("\n") withString:("")) |
083 | (ocidItemDict's setObject:(ocidItemValue) forKey:("MD5")) |
084 | #sha256sum |
085 | set argCommandText to ("/sbin/sha256sum \"" & strItemFilePath & "\"") as text |
086 | set strValue to doExecTask(argCommandText) |
087 | set ocidValueStr to (refMe's NSString's stringWithString:(strValue)) |
088 | set ocidValueArray to (ocidValueStr's componentsSeparatedByString:(" ")) |
089 | set ocidItemValue to ocidValueArray's firstObject() |
090 | set ocidItemValue to (ocidItemValue's stringByReplacingOccurrencesOfString:("\n") withString:("")) |
091 | (ocidItemDict's setObject:(ocidItemValue) forKey:("SHA256")) |
092 | #取得した情報を出力用のDICTに格納 キーはファイル名 |
093 | (ocidResultDict's setObject:(ocidItemDict) forKey:(ocidFileName)) |
094 | end repeat |
095 | |
096 | ################## |
097 | #HTML生成開始 |
098 | set ocidXMLDoc to refMe's NSXMLDocument's alloc()'s init() |
099 | ocidXMLDoc's setDocumentContentKind:(refMe's NSXMLDocumentHTMLKind) |
100 | set ocidDTD to refMe's NSXMLDTD's alloc()'s init() |
101 | ocidDTD's setName:("html") |
102 | ocidXMLDoc's setDTD:(ocidDTD) |
103 | set ocidRootElement to refMe's NSXMLElement's elementWithName:("html") |
104 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("lang") stringValue:("ja") |
105 | ocidRootElement's addAttribute:(ocidAddNode) |
106 | #head |
107 | set ocidHeadElement to refMe's NSXMLElement's elementWithName:("head") |
108 | set ocidAddElement to refMe's NSXMLElement's elementWithName:("title") |
109 | ocidAddElement's setStringValue:("ファイル比較") |
110 | ocidHeadElement's addChild:(ocidAddElement) |
111 | set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta") |
112 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("name") stringValue:("viewport") |
113 | ocidAddElement's addAttribute:(ocidAddNode) |
114 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("width=720") |
115 | ocidAddElement's addAttribute:(ocidAddNode) |
116 | ocidHeadElement's addChild:(ocidAddElement) |
117 | set ocidAddElement to refMe's NSXMLElement's elementWithName:("style") |
118 | ocidAddElement's setStringValue:("body { margin: 10px; background-color: #FFFFFF; } table { border-spacing: 0; caption-side: top; font-family: system-ui; } thead th { border: solid 1px #666666; padding: .5ch 1ch; border-block-width: 1px 0; border-inline-width: 1px 0; &:first-of-type { border-start-start-radius: .5em } &:last-of-type { border-start-end-radius: .5em; border-inline-end-width: 1px } } tbody td { word-wrap: anywhere;word-break: break-all;max-width: 360px;border-spacing: 0; border: solid 1px #666666; padding: .5ch 1ch; border-block-width: 1px 0; border-inline-width: 1px 0; &:last-of-type { border-inline-end-width: 1px } } tbody th { border-spacing: 0; border: solid 1px #666666; padding: .5ch 1ch; border-block-width: 1px 0; border-inline-width: 1px 0; } tbody tr:nth-of-type(odd) { background: #F2F2F2; } .kind_string { font-size: 0.75em; } .date_string { font-size: 0.5em; } .tbody_th_title{text-align: left;} tfoot th { border: solid 1px #666666; padding: .5ch 1ch; &:first-of-type { border-end-start-radius: .5em } &:last-of-type { border-end-end-radius: .5em; border-inline-end-width: 1px } }") |
119 | ocidHeadElement's addChild:(ocidAddElement) |
120 | ocidRootElement's addChild:(ocidHeadElement) |
121 | #body |
122 | set ocidBodyElement to refMe's NSXMLElement's elementWithName:("body") |
123 | ################## |
124 | #ヘッダー |
125 | set ocidHeaderElement to refMe's NSXMLElement's elementWithName:("header") |
126 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("header") |
127 | ocidHeaderElement's addAttribute:(ocidAddNode) |
128 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_header") |
129 | ocidHeaderElement's addAttribute:(ocidAddNode) |
130 | set ocidSetHeaderElement to (refMe's NSXMLElement's elementWithName:("div")) |
131 | set ocidH3Element to (refMe's NSXMLElement's elementWithName:("h3")) |
132 | (ocidH3Element's setStringValue:("ファイル比較v1")) |
133 | (ocidSetHeaderElement's addChild:(ocidH3Element)) |
134 | ocidHeaderElement's addChild:(ocidSetHeaderElement) |
135 | ocidBodyElement's addChild:(ocidHeaderElement) |
136 | |
137 | |
138 | ################## |
139 | #アーティクル |
140 | set ocidArticleElement to refMe's NSXMLElement's elementWithName:("article") |
141 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("article") |
142 | ocidArticleElement's addAttribute:(ocidAddNode) |
143 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_article") |
144 | ocidArticleElement's addAttribute:(ocidAddNode) |
145 | ################## |
146 | #テーブル開始 |
147 | #【table】 |
148 | set ocidTableElement to (refMe's NSXMLElement's elementWithName:("table")) |
149 | #【caption】 |
150 | set ocidCaptionElement to (refMe's NSXMLElement's elementWithName:("caption")) |
151 | (ocidCaptionElement's setStringValue:("内容が同じでも修正が入っているなら相違となります")) |
152 | (ocidTableElement's addChild:(ocidCaptionElement)) |
153 | #【colgroup】 |
154 | set beginning of listKey to "項目名" |
155 | set numCntCol to (count of listKey) as integer |
156 | set ocidColgroupElement to (refMe's NSXMLElement's elementWithName:("colgroup")) |
157 | repeat with itemNo from 1 to numCntCol by 1 |
158 | set strItemName to (item itemNo of listKey) as text |
159 | #【col】col生成 |
160 | set ocidAddElement to (refMe's NSXMLElement's elementWithName:("col")) |
161 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:(strItemName)) |
162 | (ocidAddElement's addAttribute:(ocidAddNode)) |
163 | (ocidColgroupElement's addChild:(ocidAddElement)) |
164 | end repeat |
165 | #テーブルエレメントに追加 |
166 | (ocidTableElement's addChild:(ocidColgroupElement)) |
167 | #【thead】 |
168 | set ocidTheadElement to (refMe's NSXMLElement's elementWithName:("thead")) |
169 | set ocidTrElement to (refMe's NSXMLElement's elementWithName:("tr")) |
170 | repeat with itemNo from 1 to numCntCol by 1 |
171 | set strItemName to (item itemNo of listKey) as text |
172 | set ocidAddElement to (refMe's NSXMLElement's elementWithName:("th")) |
173 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:(strItemName)) |
174 | (ocidAddElement's addAttribute:(ocidAddNode)) |
175 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("col")) |
176 | (ocidAddElement's addAttribute:(ocidAddNode)) |
177 | (ocidAddElement's setStringValue:(strItemName)) |
178 | (ocidTrElement's addChild:(ocidAddElement)) |
179 | end repeat |
180 | (ocidTheadElement's addChild:(ocidTrElement)) |
181 | (ocidTableElement's addChild:(ocidTheadElement)) |
182 | #【tbody】 |
183 | set ocidTbodyElement to refMe's NSXMLElement's elementWithName:("tbody") |
184 | set listKey to (items 2 thru -1 of listKey) as list |
185 | set numCntCol to (count of listKey) as integer |
186 | #TR 1 |
187 | set ocidTrElement to (refMe's NSXMLElement's elementWithName:("tr")) |
188 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("row")) |
189 | (ocidTrElement's addAttribute:(ocidAddNode)) |
190 | set ocidTdElement to (refMe's NSXMLElement's elementWithName:("th")) |
191 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("項目名")) |
192 | (ocidTdElement's addAttribute:(ocidAddNode)) |
193 | (ocidTdElement's setStringValue:("ファイルパス")) |
194 | (ocidTrElement's addChild:(ocidTdElement)) |
195 | repeat with itemNo from 1 to numCntCol by 1 |
196 | set strItemName to (item itemNo of listKey) as text |
197 | set ocidAddElement to (refMe's NSXMLElement's elementWithName:("td")) |
198 | set itemDict to (ocidResultDict's objectForKey:(strItemName)) |
199 | set itemValue to (itemDict's valueForKey:("PATH")) |
200 | (ocidAddElement's setStringValue:(itemValue)) |
201 | (ocidTrElement's addChild:(ocidAddElement)) |
202 | end repeat |
203 | (ocidTbodyElement's addChild:(ocidTrElement)) |
204 | #TR 2 |
205 | set ocidTrElement to (refMe's NSXMLElement's elementWithName:("tr")) |
206 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("row")) |
207 | (ocidTrElement's addAttribute:(ocidAddNode)) |
208 | set ocidTdElement to (refMe's NSXMLElement's elementWithName:("th")) |
209 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("項目名")) |
210 | (ocidTdElement's addAttribute:(ocidAddNode)) |
211 | (ocidTdElement's setStringValue:("リンク")) |
212 | (ocidTrElement's addChild:(ocidTdElement)) |
213 | repeat with itemNo from 1 to numCntCol by 1 |
214 | set strItemName to (item itemNo of listKey) as text |
215 | set ocidAddElement to (refMe's NSXMLElement's elementWithName:("td")) |
216 | set itemDict to (ocidResultDict's objectForKey:(strItemName)) |
217 | set itemValue to (itemDict's valueForKey:("URL")) |
218 | set strLinkPathURL to itemValue's absoluteString() as text |
219 | set ocidAElement to (refMe's NSXMLElement's elementWithName:("a")) |
220 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:(strLinkPathURL)) |
221 | (ocidAElement's addAttribute:(ocidAddNode)) |
222 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank")) |
223 | (ocidAElement's addAttribute:(ocidAddNode)) |
224 | (ocidAElement's setStringValue:("LINK")) |
225 | (ocidAddElement's addChild:(ocidAElement)) |
226 | (ocidTrElement's addChild:(ocidAddElement)) |
227 | end repeat |
228 | (ocidTbodyElement's addChild:(ocidTrElement)) |
229 | #TR 3 |
230 | set ocidTrElement to (refMe's NSXMLElement's elementWithName:("tr")) |
231 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("row")) |
232 | (ocidTrElement's addAttribute:(ocidAddNode)) |
233 | set ocidTdElement to (refMe's NSXMLElement's elementWithName:("th")) |
234 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("項目名")) |
235 | (ocidTdElement's addAttribute:(ocidAddNode)) |
236 | (ocidTdElement's setStringValue:("ファイルサイズ")) |
237 | (ocidTrElement's addChild:(ocidTdElement)) |
238 | repeat with itemNo from 1 to numCntCol by 1 |
239 | set strItemName to (item itemNo of listKey) as text |
240 | set ocidAddElement to (refMe's NSXMLElement's elementWithName:("td")) |
241 | set itemDict to (ocidResultDict's objectForKey:(strItemName)) |
242 | set itemValue to (itemDict's valueForKey:("SIZE")) |
243 | set strValue to itemValue as text |
244 | (ocidAddElement's setStringValue:(strValue)) |
245 | (ocidTrElement's addChild:(ocidAddElement)) |
246 | end repeat |
247 | (ocidTbodyElement's addChild:(ocidTrElement)) |
248 | #TR 4 |
249 | set ocidTrElement to (refMe's NSXMLElement's elementWithName:("tr")) |
250 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("row")) |
251 | (ocidTrElement's addAttribute:(ocidAddNode)) |
252 | set ocidTdElement to (refMe's NSXMLElement's elementWithName:("th")) |
253 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("項目名")) |
254 | (ocidTdElement's addAttribute:(ocidAddNode)) |
255 | (ocidTdElement's setStringValue:("作成日")) |
256 | (ocidTrElement's addChild:(ocidTdElement)) |
257 | repeat with itemNo from 1 to numCntCol by 1 |
258 | set strItemName to (item itemNo of listKey) as text |
259 | set ocidAddElement to (refMe's NSXMLElement's elementWithName:("td")) |
260 | set itemDict to (ocidResultDict's objectForKey:(strItemName)) |
261 | set itemValue to (itemDict's valueForKey:("CreDate")) |
262 | set strValue to itemValue as date |
263 | (ocidAddElement's setStringValue:(strValue)) |
264 | (ocidTrElement's addChild:(ocidAddElement)) |
265 | end repeat |
266 | (ocidTbodyElement's addChild:(ocidTrElement)) |
267 | #TR 5 |
268 | set ocidTrElement to (refMe's NSXMLElement's elementWithName:("tr")) |
269 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("row")) |
270 | (ocidTrElement's addAttribute:(ocidAddNode)) |
271 | set ocidTdElement to (refMe's NSXMLElement's elementWithName:("th")) |
272 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("項目名")) |
273 | (ocidTdElement's addAttribute:(ocidAddNode)) |
274 | (ocidTdElement's setStringValue:("修正日")) |
275 | (ocidTrElement's addChild:(ocidTdElement)) |
276 | # |
277 | set ocidCompareArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
278 | repeat with itemNo from 1 to numCntCol by 1 |
279 | set strItemName to (item itemNo of listKey) as text |
280 | set itemDict to (ocidResultDict's objectForKey:(strItemName)) |
281 | set itemValue to (itemDict's valueForKey:("ModDate")) |
282 | (ocidCompareArray's addObject:(itemValue)) |
283 | end repeat |
284 | set ocidFirstItem to ocidCompareArray's firstObject() |
285 | set boolCompare to missing value |
286 | repeat with itemArray in ocidCompareArray |
287 | set boolEqual to (ocidFirstItem's isEqualToDate:(itemArray)) as boolean |
288 | if boolEqual is false then |
289 | set boolCompare to false as boolean |
290 | exit repeat |
291 | end if |
292 | end repeat |
293 | # |
294 | set ocidNewerDate to ocidCompareArray's valueForKeyPath:("@max.self") |
295 | # |
296 | repeat with itemNo from 1 to numCntCol by 1 |
297 | set strItemName to (item itemNo of listKey) as text |
298 | set ocidAddElement to (refMe's NSXMLElement's elementWithName:("td")) |
299 | set itemDict to (ocidResultDict's objectForKey:(strItemName)) |
300 | set itemValue to (itemDict's valueForKey:("ModDate")) |
301 | set boolSameDate to (ocidNewerDate's isEqualToDate:(itemValue)) as boolean |
302 | if boolCompare is false then |
303 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("style") stringValue:("font-weight: bold;")) |
304 | (ocidAddElement's addAttribute:(ocidAddNode)) |
305 | if boolSameDate is true then |
306 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("style") stringValue:("color: blue;font-weight: bold;")) |
307 | (ocidAddElement's addAttribute:(ocidAddNode)) |
308 | end if |
309 | end if |
310 | |
311 | set strValue to itemValue as date |
312 | (ocidAddElement's setStringValue:(strValue)) |
313 | (ocidTrElement's addChild:(ocidAddElement)) |
314 | end repeat |
315 | (ocidTbodyElement's addChild:(ocidTrElement)) |
316 | #TR 6 |
317 | set ocidTrElement to (refMe's NSXMLElement's elementWithName:("tr")) |
318 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("row")) |
319 | (ocidTrElement's addAttribute:(ocidAddNode)) |
320 | set ocidTdElement to (refMe's NSXMLElement's elementWithName:("th")) |
321 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("項目名")) |
322 | (ocidTdElement's addAttribute:(ocidAddNode)) |
323 | (ocidTdElement's setStringValue:("HASH MD5")) |
324 | (ocidTrElement's addChild:(ocidTdElement)) |
325 | # |
326 | set ocidCompareArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
327 | repeat with itemNo from 1 to numCntCol by 1 |
328 | set strItemName to (item itemNo of listKey) as text |
329 | set itemDict to (ocidResultDict's objectForKey:(strItemName)) |
330 | set itemValue to (itemDict's valueForKey:("MD5")) |
331 | (ocidCompareArray's addObject:(itemValue)) |
332 | end repeat |
333 | set ocidFirstItem to ocidCompareArray's firstObject() |
334 | set boolCompare to missing value |
335 | repeat with itemArray in ocidCompareArray |
336 | set boolEqual to (ocidFirstItem's isEqualToString:(itemArray)) as boolean |
337 | if boolEqual is false then |
338 | set boolCompare to false as boolean |
339 | exit repeat |
340 | end if |
341 | end repeat |
342 | |
343 | repeat with itemNo from 1 to numCntCol by 1 |
344 | set strItemName to (item itemNo of listKey) as text |
345 | set ocidAddElement to (refMe's NSXMLElement's elementWithName:("td")) |
346 | if boolCompare is false then |
347 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("style") stringValue:("color: red;")) |
348 | (ocidAddElement's addAttribute:(ocidAddNode)) |
349 | end if |
350 | set itemDict to (ocidResultDict's objectForKey:(strItemName)) |
351 | set itemValue to (itemDict's valueForKey:("SHA256")) |
352 | set strValue to itemValue as text |
353 | (ocidAddElement's setStringValue:(strValue)) |
354 | (ocidTrElement's addChild:(ocidAddElement)) |
355 | end repeat |
356 | (ocidTbodyElement's addChild:(ocidTrElement)) |
357 | |
358 | #TR 7 |
359 | set ocidTrElement to (refMe's NSXMLElement's elementWithName:("tr")) |
360 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("row")) |
361 | (ocidTrElement's addAttribute:(ocidAddNode)) |
362 | set ocidTdElement to (refMe's NSXMLElement's elementWithName:("th")) |
363 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("項目名")) |
364 | (ocidTdElement's addAttribute:(ocidAddNode)) |
365 | (ocidTdElement's setStringValue:("HASH SHA256")) |
366 | (ocidTrElement's addChild:(ocidTdElement)) |
367 | # |
368 | set ocidCompareArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
369 | repeat with itemNo from 1 to numCntCol by 1 |
370 | set strItemName to (item itemNo of listKey) as text |
371 | set itemDict to (ocidResultDict's objectForKey:(strItemName)) |
372 | set itemValue to (itemDict's valueForKey:("SHA256")) |
373 | (ocidCompareArray's addObject:(itemValue)) |
374 | end repeat |
375 | set ocidFirstItem to ocidCompareArray's firstObject() |
376 | set boolCompare to missing value |
377 | repeat with itemArray in ocidCompareArray |
378 | set boolEqual to (ocidFirstItem's isEqualToString:(itemArray)) as boolean |
379 | if boolEqual is false then |
380 | set boolCompare to false as boolean |
381 | exit repeat |
382 | end if |
383 | end repeat |
384 | |
385 | repeat with itemNo from 1 to numCntCol by 1 |
386 | set strItemName to (item itemNo of listKey) as text |
387 | set ocidAddElement to (refMe's NSXMLElement's elementWithName:("td")) |
388 | if boolCompare is false then |
389 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("style") stringValue:("color: red;")) |
390 | (ocidAddElement's addAttribute:(ocidAddNode)) |
391 | end if |
392 | set itemDict to (ocidResultDict's objectForKey:(strItemName)) |
393 | set itemValue to (itemDict's valueForKey:("MD5")) |
394 | set strValue to itemValue as text |
395 | (ocidAddElement's setStringValue:(strValue)) |
396 | (ocidTrElement's addChild:(ocidAddElement)) |
397 | end repeat |
398 | (ocidTbodyElement's addChild:(ocidTrElement)) |
399 | |
400 | |
401 | |
402 | (ocidTableElement's addChild:(ocidTbodyElement)) |
403 | #【tfoot】 |
404 | set ocidTfootElement to (refMe's NSXMLElement's elementWithName:("tfoot")) |
405 | set ocidTrElement to (refMe's NSXMLElement's elementWithName:("tr")) |
406 | set ocidThElement to (refMe's NSXMLElement's elementWithName:("th")) |
407 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("テーブルの終わり")) |
408 | (ocidThElement's addAttribute:(ocidAddNode)) |
409 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("colspan") stringValue:(numCntCol + 1)) |
410 | (ocidThElement's addAttribute:(ocidAddNode)) |
411 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("row")) |
412 | (ocidThElement's addAttribute:(ocidAddNode)) |
413 | (ocidThElement's setStringValue:("ファイルの比較結果です")) |
414 | (ocidTrElement's addChild:(ocidThElement)) |
415 | (ocidTfootElement's addChild:(ocidTrElement)) |
416 | (ocidTableElement's addChild:(ocidTfootElement)) |
417 | # |
418 | ocidArticleElement's addChild:(ocidTableElement) |
419 | ocidBodyElement's addChild:(ocidArticleElement) |
420 | |
421 | ################## |
422 | #フッター |
423 | set ocidFooterElement to refMe's NSXMLElement's elementWithName:("footer") |
424 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("footer") |
425 | ocidFooterElement's addAttribute:(ocidAddNode) |
426 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_footer") |
427 | ocidFooterElement's addAttribute:(ocidAddNode) |
428 | set ocidSetFooterElement to (refMe's NSXMLElement's elementWithName:("div")) |
429 | set ocidAElement to (refMe's NSXMLElement's elementWithName:("a")) |
430 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:("https://quicktimer.cocolog-nifty.com/")) |
431 | (ocidAElement's addAttribute:(ocidAddNode)) |
432 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank")) |
433 | (ocidAElement's addAttribute:(ocidAddNode)) |
434 | set strContents to ("AppleScriptで生成しました") as text |
435 | (ocidAElement's setStringValue:(strContents)) |
436 | (ocidSetFooterElement's addChild:(ocidAElement)) |
437 | ocidFooterElement's addChild:(ocidSetFooterElement) |
438 | ocidBodyElement's addChild:(ocidFooterElement) |
439 | |
440 | |
441 | ################## |
442 | #保存先 |
443 | set appFileManager to refMe's NSFileManager's defaultManager() |
444 | set ocidTempDirURL to appFileManager's temporaryDirectory() |
445 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
446 | set ocidUUIDString to ocidUUID's UUIDString |
447 | set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true |
448 | set appFileManager to refMe's NSFileManager's defaultManager() |
449 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
450 | ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions) |
451 | set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
452 | set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("index.html") isDirectory:false |
453 | |
454 | ################## |
455 | #保存 |
456 | #ボディをROOTエレメントにセット |
457 | ocidRootElement's addChild:(ocidBodyElement) |
458 | ocidXMLDoc's setRootElement:(ocidRootElement) |
459 | # |
460 | set ocidXMLdata to (ocidXMLDoc's XMLDataWithOptions:(refMe's NSXMLNodePrettyPrint)) |
461 | set listDone to (ocidXMLdata's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error| :(reference)) |
462 | set aliasSaveFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias |
463 | |
464 | ####ブラウザで開く |
465 | tell application "Finder" |
466 | open location aliasSaveFilePath |
467 | end tell |
468 | |
469 | return "処理終了" |
470 | |
471 | |
472 | |
473 | |
474 | |
475 | ################## |
476 | #コマンド実行 |
477 | to doExecTask(argCommandText) |
478 | # |
479 | set ocidComString to refMe's NSString's stringWithString:(argCommandText) |
480 | # |
481 | set appFileManager to refMe's NSFileManager's defaultManager() |
482 | set ocidTempDirURL to appFileManager's temporaryDirectory() |
483 | set ocidTermTask to refMe's NSTask's alloc()'s init() |
484 | ocidTermTask's setLaunchPath:("/bin/zsh") |
485 | set ocidArgumentsArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
486 | ocidArgumentsArray's addObject:("-c") |
487 | ocidArgumentsArray's addObject:(ocidComString) |
488 | ocidTermTask's setArguments:(ocidArgumentsArray) |
489 | set ocidOutPut to refMe's NSPipe's pipe() |
490 | set ocidError to refMe's NSPipe's pipe() |
491 | ocidTermTask's setStandardOutput:(ocidOutPut) |
492 | ocidTermTask's setStandardError:(ocidError) |
493 | ocidTermTask's setCurrentDirectoryURL:(ocidTempDirURL) |
494 | set listDoneReturn to ocidTermTask's launchAndReturnError:(reference) |
495 | if (item 1 of listDoneReturn) is (false) then |
496 | log "エラーコード:" & (item 2 of listDoneReturn)'s code() as text |
497 | log "エラードメイン:" & (item 2 of listDoneReturn)'s domain() as text |
498 | log "Description:" & (item 2 of listDoneReturn)'s localizedDescription() as text |
499 | log "FailureReason:" & (item 2 of listDoneReturn)'s localizedFailureReason() as text |
500 | end if |
501 | ################## |
502 | #終了待ち |
503 | ocidTermTask's waitUntilExit() |
504 | |
505 | ################## |
506 | #標準出力をログに |
507 | set ocidOutPutData to ocidOutPut's fileHandleForReading() |
508 | set listResponse to ocidOutPutData's readDataToEndOfFileAndReturnError:(reference) |
509 | set ocidStdOut to (item 1 of listResponse) |
510 | set ocidStdOut to refMe's NSString's alloc()'s initWithData:(ocidStdOut) encoding:(refMe's NSUTF8StringEncoding) |
511 | ##これが戻り値 |
512 | return ocidStdOut as text |
513 | |
514 | end doExecTask |
AppleScriptで生成しました |
| 固定リンク