[com.apple.webarchive]webarchiveから画像データーを書き出します ちょっと修正
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | (* |
004 | webarchiveから画像データーを書き出します |
005 | v1.1 ちょっと修正版 |
006 | v1.2 ダイアログ呼び出しを変更 |
007 | v1.3 missing value対策を入れた |
008 | com.cocolog-nifty.quicktimer.icefloe *) |
009 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
010 | |
011 | use AppleScript version "2.8" |
012 | use framework "Foundation" |
013 | use framework "AppKit" |
014 | use scripting additions |
015 | |
016 | |
017 | property refMe : a reference to current application |
018 | |
019 | |
020 | set appFileManager to refMe's NSFileManager's defaultManager() |
021 | set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace() |
022 | |
023 | |
024 | ###ダイアログを前面に出す |
025 | set strName to (name of current application) as text |
026 | if strName is "osascript" then |
027 | tell application "SystemUIServer" to activate |
028 | else |
029 | tell current application to activate |
030 | end if |
031 | |
032 | |
033 | ####ダイアログで使うデフォルトロケーション |
034 | set ocidGetUrlArray to appFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask) |
035 | set ocidDesktopDirPathURL to ocidGetUrlArray's firstObject() |
036 | set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias |
037 | |
038 | ####UTIリスト com.apple.webarchiveかplist |
039 | set listUTI to {"com.apple.webarchive", "com.apple.property-list"} as list |
040 | |
041 | |
042 | ####ダイアログを出す |
043 | tell application "SystemUIServer" |
044 | activate |
045 | set aliasFilePath to (choose file with prompt "webarchive ファイルを選んでください" default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias |
046 | end tell |
047 | |
048 | set strFilePath to (POSIX path of aliasFilePath) as text |
049 | ####ドキュメントのパスをNSString |
050 | set ocidFilePath to refMe's NSString's stringWithString:(strFilePath) |
051 | set ocidFilePath to ocidFilePath's stringByStandardizingPath() |
052 | set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false) |
053 | ###選んだファイルのディレクトリ |
054 | set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent() |
055 | |
056 | ############################################## |
057 | ## 本処理 |
058 | ############################################## |
059 | set ocidOption to (refMe's NSDataWritingAtomic) |
060 | ###PLIST ROOT |
061 | set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s init() |
062 | set listReadPlistData to refMe's NSMutableDictionary's dictionaryWithContentsOfURL:(ocidFilePathURL) |error| :(reference) |
063 | set ocidPlistDict to item 1 of listReadPlistData |
064 | ############################################## |
065 | ## 本処理 WebMainResource |
066 | ############################################## |
067 | ###ダウンロードしたURLのホスト名を保存先フォルダ名にする |
068 | set ocidWebMainResourceDict to ocidPlistDict's objectForKey:"WebMainResource" |
069 | set strMainURLString to (ocidWebMainResourceDict's valueForKey:("WebResourceURL")) as text |
070 | set ocidURLString to refMe's NSString's stringWithString:(strMainURLString) |
071 | set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLString) |
072 | set strHostName to ocidURL's |host|() as text |
073 | if strHostName ends with ".app" then |
074 | set strHostName to (strHostName & "_Folder") as text |
075 | end if |
076 | set strHostName to (strHostName & "/Images") as text |
077 | ###イメージ保存先 |
078 | set ocidSaveFileDirURL to (ocidContainerDirURL's URLByAppendingPathComponent:(strHostName)) |
079 | ###保存先ディレクトリを作成 |
080 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init() |
081 | ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions) |
082 | appFileManager's createDirectoryAtURL:(ocidSaveFileDirURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error| :(reference) |
083 | |
084 | ############################################## |
085 | ## 本処理 WebSubresources |
086 | ############################################## |
087 | ###WebSubresources |
088 | set ocidWebSubresourcesArray to refMe's NSMutableArray's alloc()'s init() |
089 | set ocidWebSubresourcesArray to ocidPlistDict's objectForKey:("WebSubresources") |
090 | ###インラインイメージ用のカウント |
091 | set numInlineNO to 1 as integer |
092 | repeat with itemWebSubresourcesDict in ocidWebSubresourcesArray |
093 | set strMIME to (itemWebSubresourcesDict's valueForKey:("WebResourceMIMEType")) as text |
094 | if strMIME contains "image" then |
095 | set ocidWebPath to (itemWebSubresourcesDict's valueForKey:("WebResourceURL")) |
096 | set strWebPath to ocidWebPath as text |
097 | ##### |
098 | if strWebPath contains "data:image" then |
099 | #####インラインSVGを保存するやっつけ感満載の処理… |
100 | |
101 | if strWebPath contains "data:image/svg+xml;charset=utf-8," then |
102 | set strFileName to ("svg-" & numInlineNO & ".svg") as text |
103 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:(strFileName)) |
104 | |
105 | set AppleScript's text item delimiters to "data:image/svg+xml;charset=utf-8," |
106 | set listDelim to every text item of strWebPath |
107 | set AppleScript's text item delimiters to "" |
108 | set strInLineContents to (item 2 of listDelim) as text |
109 | set ocidInlineContents to (refMe's NSString's stringWithString:(strInLineContents)) |
110 | set ocidDencodedContents to (ocidInlineContents's stringByRemovingPercentEncoding) |
111 | set boolDone to (ocidDencodedContents's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference)) |
112 | |
113 | else if strWebPath contains "data:image/svg+xml;charset=utf-8," then |
114 | set strFileName to ("svg-" & numInlineNO & ".svg") as text |
115 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:(strFileName)) |
116 | |
117 | set AppleScript's text item delimiters to "data:image/svg+xml;charset=utf-8," |
118 | set listDelim to every text item of strWebPath |
119 | set AppleScript's text item delimiters to "" |
120 | set strInLineContents to (item 2 of listDelim) as text |
121 | set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents) |
122 | set ocidDencodedContents to (ocidInlineContents's stringByRemovingPercentEncoding) |
123 | set boolDone to (ocidDencodedContents's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference)) |
124 | |
125 | else if strWebPath contains "data:image/svg+xml;base64," then |
126 | set strFileName to "png-" & numInlineNO & ".svg" as text |
127 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName) |
128 | |
129 | set AppleScript's text item delimiters to "data:image/svg+xml;base64," |
130 | set listDelim to every text item of strWebPath |
131 | set AppleScript's text item delimiters to "" |
132 | set strInLineContents to (item 2 of listDelim) as text |
133 | set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents) |
134 | set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:(ocidInlineContents) options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters)) |
135 | set boolResults to (ocidData's writeToURL:ocidSaveFilePathURL atomically:(true)) |
136 | |
137 | else if strWebPath contains "data:image/png;base64," then |
138 | set strFileName to "png-" & numInlineNO & ".png" as text |
139 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:(strFileName)) |
140 | |
141 | set AppleScript's text item delimiters to "data:image/png;base64," |
142 | set listDelim to every text item of strWebPath |
143 | set AppleScript's text item delimiters to "" |
144 | set strInLineContents to (item 2 of listDelim) as text |
145 | set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents) |
146 | set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:(ocidInlineContents) options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters)) |
147 | set boolResults to (ocidData's writeToURL:(ocidSaveFilePathURL) atomically:(true)) |
148 | |
149 | else if strWebPath contains "data:image/jpeg;base64," then |
150 | set strFileName to "jpg-" & numInlineNO & ".jpg" as text |
151 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:(strFileName)) |
152 | |
153 | set AppleScript's text item delimiters to "data:image/jpeg;base64," |
154 | set listDelim to every text item of strWebPath |
155 | set AppleScript's text item delimiters to "" |
156 | set strInLineContents to (item 2 of listDelim) as text |
157 | set ocidInlineContents to (refMe's NSString's stringWithString:(strInLineContents)) |
158 | set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:(ocidInlineContents) options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters)) |
159 | set boolResults to (ocidData's writeToURL:(ocidSaveFilePathURL) atomically:true) |
160 | |
161 | else if strWebPath contains "data:image/jpg;base64," then |
162 | set strFileName to "jpg-" & numInlineNO & ".jpg" as text |
163 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName) |
164 | |
165 | set AppleScript's text item delimiters to "data:image/jpg;base64," |
166 | set listDelim to every text item of strWebPath |
167 | set AppleScript's text item delimiters to "" |
168 | set strInLineContents to (item 2 of listDelim) as text |
169 | set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents) |
170 | set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:ocidInlineContents options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters)) |
171 | set boolResults to (ocidData's writeToURL:ocidSaveFilePathURL atomically:(true)) |
172 | |
173 | else |
174 | log "インラインイメージはとりあえず処理しない" |
175 | end if |
176 | set numInlineNO to (numInlineNO + 1) as text |
177 | else |
178 | set ocidWebURL to (refMe's NSURL's alloc()'s initWithString:strWebPath) |
179 | set ocidFileName to ocidWebURL's lastPathComponent() |
180 | set strExtensionName to ocidWebURL's pathExtension() as text |
181 | if strExtensionName is "" then |
182 | log "拡張子がない" |
183 | ocidFileName's stringByDeletingPathExtension() |
184 | if strMIME contains "png" then |
185 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:("png")) |
186 | else if strMIME contains "jpeg" then |
187 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:("jpg")) |
188 | else if strMIME contains "jpg" then |
189 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:("jpg")) |
190 | else if strMIME contains "webp" then |
191 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:("webp")) |
192 | else if strMIME contains "gif" then |
193 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:("gif")) |
194 | else if strMIME contains "svg" then |
195 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:("svg")) |
196 | end if |
197 | end if |
198 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:(ocidFileName)) |
199 | set ocidImageDataBase64 to (itemWebSubresourcesDict's objectForKey:("WebResourceData")) |
200 | set boolResults to (ocidImageDataBase64's writeToURL:(ocidSaveFilePathURL) atomically:(true)) |
201 | end if |
202 | else |
203 | log "イメージ以外は処理しない" |
204 | end if |
205 | set ocidImageDataBase64 to "" |
206 | end repeat |
207 | |
208 | |
209 | ############################################## |
210 | ## 本処理 WebSubframeArchives |
211 | ############################################## |
212 | set ocidWebSubframeArray to refMe's NSMutableArray's alloc()'s init() |
213 | ###WebSubframe |
214 | set ocidWebSubframeArray to ocidPlistDict's objectForKey:("WebSubframeArchives") |
215 | if ocidWebSubframeArray = (missing value) then |
216 | appShardWorkspace's openURL:(ocidSaveFileDirURL) |
217 | tell application "SystemUIServer" to quit |
218 | return |
219 | end if |
220 | ###インラインイメージ用のカウント |
221 | set numInlineNO to 1 as integer |
222 | repeat with itemWebSubframeDict in ocidWebSubframeArray |
223 | set ocidWebSubresourcesArray to (itemWebSubframeDict's objectForKey:("WebSubresources")) |
224 | log ocidWebSubresourcesArray's |count| |
225 | repeat with itemWebSubresources in ocidWebSubresourcesArray |
226 | return |
227 | set strMIME to (itemWebSubresources's valueForKey:("WebResourceMIMEType")) as text |
228 | if strMIME contains "image" then |
229 | set ocidWebPath to (itemWebSubresources's valueForKey:("WebResourceURL")) |
230 | set strWebPath to ocidWebPath as text |
231 | ##### |
232 | if strWebPath contains "data:image" then |
233 | #####インラインSVGを保存するやっつけ感満載の処理… |
234 | |
235 | if strWebPath contains "data:image/svg+xml;charset=utf-8," then |
236 | set strFileName to ("svg-" & (numInlineNO as text) & ".svg") as text |
237 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:(strFileName)) |
238 | |
239 | set AppleScript's text item delimiters to "data:image/svg+xml;charset=utf-8," |
240 | set listDelim to every text item of strWebPath |
241 | set AppleScript's text item delimiters to "" |
242 | set strInLineContents to (item 2 of listDelim) as text |
243 | set ocidInlineContents to (refMe's NSString's stringWithString:(strInLineContents)) |
244 | set ocidDencodedContents to ocidInlineContents's stringByRemovingPercentEncoding() |
245 | set listDone to (ocidDencodedContents's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference)) |
246 | log (item 1 of listDone) |
247 | |
248 | else if strWebPath contains "data:image/svg+xml;charset=utf-8," then |
249 | set strFileName to ("svg-" & (numInlineNO as text) & ".svg") as text |
250 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:(strFileName)) |
251 | |
252 | set AppleScript's text item delimiters to "data:image/svg+xml;charset=utf-8," |
253 | set listDelim to every text item of strWebPath |
254 | set AppleScript's text item delimiters to "" |
255 | set strInLineContents to (item 2 of listDelim) as text |
256 | set ocidInlineContents to (refMe's NSString's stringWithString:(strInLineContents)) |
257 | set ocidDencodedContents to ocidInlineContents's stringByRemovingPercentEncoding() |
258 | set listDone to (ocidDencodedContents's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference)) |
259 | log (item 1 of listDone) |
260 | else if strWebPath contains "data:image/svg+xml;base64," then |
261 | set strFileName to ("png-" & (numInlineNO as text) & ".svg") as text |
262 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:(strFileName)) |
263 | |
264 | set AppleScript's text item delimiters to "data:image/svg+xml;base64," |
265 | set listDelim to every text item of strWebPath |
266 | set AppleScript's text item delimiters to "" |
267 | set strInLineContents to (item 2 of listDelim) as text |
268 | set ocidInlineContents to (refMe's NSString's stringWithString:(strInLineContents)) |
269 | set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:(ocidInlineContents) options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters)) |
270 | set listDone to (ocidData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error| :(reference)) |
271 | log (item 1 of listDone) |
272 | else if strWebPath contains "data:image/png;base64," then |
273 | set strFileName to ("png-" & (numInlineNO as text) & ".png") as text |
274 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:(strFileName)) |
275 | |
276 | set AppleScript's text item delimiters to "data:image/png;base64," |
277 | set listDelim to every text item of strWebPath |
278 | set AppleScript's text item delimiters to "" |
279 | set strInLineContents to (item 2 of listDelim) as text |
280 | set ocidInlineContents to (refMe's NSString's stringWithString:(strInLineContents)) |
281 | set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:(ocidInlineContents) options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters)) |
282 | set listDone to (ocidData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error| :(reference)) |
283 | log (item 1 of listDone) |
284 | else if strWebPath contains "data:image/jpeg;base64," then |
285 | set strFileName to ("jpg-" & (numInlineNO as text) & ".jpg") as text |
286 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:(strFileName)) |
287 | |
288 | set AppleScript's text item delimiters to "data:image/jpeg;base64," |
289 | set listDelim to every text item of strWebPath |
290 | set AppleScript's text item delimiters to "" |
291 | set strInLineContents to (item 2 of listDelim) as text |
292 | set ocidInlineContents to (refMe's NSString's stringWithString:(strInLineContents)) |
293 | set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:ocidInlineContents options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters)) |
294 | set listDone to (ocidData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error| :(reference)) |
295 | log (item 1 of listDone) |
296 | |
297 | else if strWebPath contains "data:image/jpg;base64," then |
298 | set strFileName to ("jpg-" & (numInlineNO as text) & ".jpg") as text |
299 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName) |
300 | |
301 | set AppleScript's text item delimiters to "data:image/jpg;base64," |
302 | set listDelim to every text item of strWebPath |
303 | set AppleScript's text item delimiters to "" |
304 | set strInLineContents to (item 2 of listDelim) as text |
305 | set ocidInlineContents to (refMe's NSString's stringWithString:(strInLineContents)) |
306 | set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:(ocidInlineContents) options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters)) |
307 | set listDone to (ocidData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error| :(reference)) |
308 | log (item 1 of listDone) |
309 | else |
310 | log "インラインイメージはとりあえず処理しない" |
311 | end if |
312 | set numInlineNO to (numInlineNO + 1) as text |
313 | else |
314 | set ocidWebURL to (refMe's NSURL's alloc()'s initWithString:(strWebPath)) |
315 | set ocidFileName to ocidWebURL's lastPathComponent() |
316 | set strExtensionName to (ocidWebURL's pathExtension()) as text |
317 | if strExtensionName is "" then |
318 | log "拡張子がない" |
319 | ocidFileName's stringByDeletingPathExtension() |
320 | if strMIME contains "png" then |
321 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:("png")) |
322 | else if strMIME contains "jpeg" then |
323 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:("jpg")) |
324 | else if strMIME contains "jpg" then |
325 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:("jpg")) |
326 | else if strMIME contains "webp" then |
327 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:("webp")) |
328 | else if strMIME contains "gif" then |
329 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:("gif")) |
330 | else if strMIME contains "svg" then |
331 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:("svg")) |
332 | end if |
333 | end if |
334 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:(ocidFileName)) |
335 | set ocidImageDataBase64 to (itemWebSubframeDict's objectForKey:"WebResourceData") |
336 | |
337 | if ocidImageDataBase64 ≠ (missing value) then |
338 | set boolResults to (ocidImageDataBase64's writeToURL:(ocidSaveFilePathURL) atomically:(true)) |
339 | end if |
340 | end if |
341 | else |
342 | log "イメージ以外は処理しない" |
343 | end if |
344 | set ocidImageDataBase64 to "" |
345 | end repeat |
346 | |
347 | end repeat |
348 | |
349 | ############################################## |
350 | ## 書き出し先をFinderで開く |
351 | ############################################## |
352 | appShardWorkspace's openURL:(ocidSaveFileDirURL) |
353 | |
354 | tell application "SystemUIServer" to quit |
355 | |
356 | return |
357 | |
358 | |
359 | |
360 | |
361 | |
AppleScriptで生成しました |
ソース | |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | (* |
004 | webarchiveから画像データーを書き出します |
005 | v1.1 ちょっと修正版 |
006 | com.cocolog-nifty.quicktimer.icefloe *) |
007 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
008 | |
009 | use AppleScript version "2.8" |
010 | use framework "Foundation" |
011 | use framework "AppKit" |
012 | use scripting additions |
013 | |
014 | |
015 | property refMe : a reference to current application |
016 | |
017 | |
018 | set objFileManager to refMe's NSFileManager's defaultManager() |
019 | set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace() |
020 | |
021 | |
022 | ####ダイアログで使うデフォルトロケーション |
023 | set ocidGetUrlArray to (objFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask)) |
024 | set ocidDesktopDirPathURL to ocidGetUrlArray's objectAtIndex:0 |
025 | set aliasDefaultLocation to ocidDesktopDirPathURL as alias |
026 | |
027 | ####UTIリスト com.apple.webarchiveかplist |
028 | set listUTI to {"com.apple.webarchive", "com.apple.property-list"} |
029 | |
030 | |
031 | ####ダイアログを出す |
032 | set aliasFilePath to (choose file with prompt "webarchive ファイルを選んでください" default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias |
033 | |
034 | set strFilePath to (POSIX path of aliasFilePath) as text |
035 | ####ドキュメントのパスをNSString |
036 | set ocidFilePath to refMe's NSString's stringWithString:(strFilePath) |
037 | set ocidFilePath to ocidFilePath's stringByStandardizingPath |
038 | set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false |
039 | ###選んだファイルのディレクトリ |
040 | set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent |
041 | |
042 | ############################################## |
043 | ## 本処理 |
044 | ############################################## |
045 | ###PLIST ROOT |
046 | set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
047 | set listReadPlistData to refMe's NSMutableDictionary's dictionaryWithContentsOfURL:(ocidFilePathURL) |error|:(reference) |
048 | set ocidPlistDict to item 1 of listReadPlistData |
049 | ############################################## |
050 | ## 本処理 WebMainResource |
051 | ############################################## |
052 | ###ダウンロードしたURLのホスト名を保存先フォルダ名にする |
053 | set ocidWebMainResourceDict to ocidPlistDict's objectForKey:"WebMainResource" |
054 | set ocidMainURLString to ocidWebMainResourceDict's valueForKey:("WebResourceURL") |
055 | set ocidURLString to refMe's NSString's stringWithString:(ocidMainURLString) |
056 | set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidMainURLString) |
057 | set strHostName to ocidURL's |host|() as text |
058 | set strHostName to (strHostName & "/Images") |
059 | ###イメージ保存先 |
060 | set ocidSaveFileDirURL to (ocidContainerDirURL's URLByAppendingPathComponent:(strHostName)) |
061 | ###保存先ディレクトリを作成 |
062 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
063 | ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions) |
064 | objFileManager's createDirectoryAtURL:ocidSaveFileDirURL withIntermediateDirectories:true attributes:ocidAttrDict |error|:(reference) |
065 | |
066 | ############################################## |
067 | ## 本処理 WebSubresources |
068 | ############################################## |
069 | ###WebSubresources |
070 | set ocidWebSubresourcesArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0 |
071 | set ocidWebSubresourcesArray to ocidPlistDict's objectForKey:"WebSubresources" |
072 | ###インラインイメージ用のカウント |
073 | set numInlineNO to 1 as text |
074 | repeat with itemWebSubresourcesDict in ocidWebSubresourcesArray |
075 | set strMIME to (itemWebSubresourcesDict's objectForKey:"WebResourceMIMEType") as text |
076 | if strMIME contains "image" then |
077 | set ocidWebPath to (itemWebSubresourcesDict's objectForKey:"WebResourceURL") |
078 | set strWebPath to ocidWebPath as text |
079 | ##### |
080 | if strWebPath contains "data:image" then |
081 | #####インラインSVGを保存するやっつけ感満載の処理… |
082 | |
083 | if strWebPath contains "data:image/svg+xml;charset=utf-8," then |
084 | set strFileName to "svg-" & numInlineNO & ".svg" as text |
085 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName) |
086 | |
087 | set AppleScript's text item delimiters to "data:image/svg+xml;charset=utf-8," |
088 | set listDelim to every text item of strWebPath |
089 | set AppleScript's text item delimiters to "" |
090 | set strInLineContents to item 2 of listDelim |
091 | set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents) |
092 | set ocidDencodedContents to (ocidInlineContents's stringByRemovingPercentEncoding) |
093 | set boolDone to (ocidDencodedContents's writeToURL:ocidSaveFilePathURL atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)) |
094 | |
095 | else if strWebPath contains "data:image/svg+xml;charset=utf-8," then |
096 | set strFileName to "svg-" & numInlineNO & ".svg" as text |
097 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName) |
098 | |
099 | set AppleScript's text item delimiters to "data:image/svg+xml;charset=utf-8," |
100 | set listDelim to every text item of strWebPath |
101 | set AppleScript's text item delimiters to "" |
102 | set strInLineContents to item 2 of listDelim |
103 | set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents) |
104 | set ocidDencodedContents to (ocidInlineContents's stringByRemovingPercentEncoding) |
105 | set boolDone to (ocidDencodedContents's writeToURL:ocidSaveFilePathURL atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)) |
106 | |
107 | else if strWebPath contains "data:image/svg+xml;base64," then |
108 | set strFileName to "png-" & numInlineNO & ".svg" as text |
109 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName) |
110 | |
111 | set AppleScript's text item delimiters to "data:image/svg+xml;base64," |
112 | set listDelim to every text item of strWebPath |
113 | set AppleScript's text item delimiters to "" |
114 | set strInLineContents to item 2 of listDelim |
115 | set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents) |
116 | set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:ocidInlineContents options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters)) |
117 | set boolResults to (ocidData's writeToURL:ocidSaveFilePathURL atomically:true) |
118 | |
119 | else if strWebPath contains "data:image/png;base64," then |
120 | set strFileName to "png-" & numInlineNO & ".png" as text |
121 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName) |
122 | |
123 | set AppleScript's text item delimiters to "data:image/png;base64," |
124 | set listDelim to every text item of strWebPath |
125 | set AppleScript's text item delimiters to "" |
126 | set strInLineContents to item 2 of listDelim |
127 | set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents) |
128 | set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:ocidInlineContents options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters)) |
129 | set boolResults to (ocidData's writeToURL:ocidSaveFilePathURL atomically:true) |
130 | |
131 | else if strWebPath contains "data:image/jpeg;base64," then |
132 | set strFileName to "jpg-" & numInlineNO & ".jpg" as text |
133 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName) |
134 | |
135 | set AppleScript's text item delimiters to "data:image/jpeg;base64," |
136 | set listDelim to every text item of strWebPath |
137 | set AppleScript's text item delimiters to "" |
138 | set strInLineContents to item 2 of listDelim |
139 | set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents) |
140 | set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:ocidInlineContents options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters)) |
141 | set boolResults to (ocidData's writeToURL:ocidSaveFilePathURL atomically:true) |
142 | |
143 | else if strWebPath contains "data:image/jpg;base64," then |
144 | set strFileName to "jpg-" & numInlineNO & ".jpg" as text |
145 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName) |
146 | |
147 | set AppleScript's text item delimiters to "data:image/jpg;base64," |
148 | set listDelim to every text item of strWebPath |
149 | set AppleScript's text item delimiters to "" |
150 | set strInLineContents to item 2 of listDelim |
151 | set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents) |
152 | set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:ocidInlineContents options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters)) |
153 | set boolResults to (ocidData's writeToURL:ocidSaveFilePathURL atomically:true) |
154 | |
155 | else |
156 | log "インラインイメージはとりあえず処理しない" |
157 | end if |
158 | set numInlineNO to numInlineNO + 1 as text |
159 | else |
160 | set ocidWebURL to (refMe's NSURL's alloc()'s initWithString:ocidWebPath) |
161 | set ocidFileName to ocidWebURL's lastPathComponent() |
162 | set strExtensionName to ocidWebURL's pathExtension() as text |
163 | if strExtensionName is "" then |
164 | log "拡張子がない" |
165 | ocidFileName's stringByDeletingPathExtension() |
166 | if strMIME contains "png" then |
167 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"png") |
168 | else if strMIME contains "jpeg" then |
169 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"jpg") |
170 | else if strMIME contains "jpg" then |
171 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"jpg") |
172 | else if strMIME contains "webp" then |
173 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"webp") |
174 | else if strMIME contains "gif" then |
175 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"gif") |
176 | else if strMIME contains "svg" then |
177 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"svg") |
178 | end if |
179 | end if |
180 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:ocidFileName) |
181 | set ocidImageDataBase64 to (itemWebSubresourcesDict's objectForKey:"WebResourceData") |
182 | set boolResults to (ocidImageDataBase64's writeToURL:ocidSaveFilePathURL atomically:true) |
183 | end if |
184 | else |
185 | log "イメージ以外は処理しない" |
186 | end if |
187 | set ocidImageDataBase64 to "" |
188 | end repeat |
189 | ############################################## |
190 | ## 本処理 WebSubframeArchives |
191 | ############################################## |
192 | |
193 | ###WebSubframe |
194 | set ocidWebSubframeArray to ocidPlistDict's objectForKey:"WebSubframeArchives" |
195 | ###インラインイメージ用のカウント |
196 | set numInlineNO to 1 as text |
197 | repeat with itemWebSubframeDict in ocidWebSubframeArray |
198 | set ocidWebSubresourcesArray to (itemWebSubframeDict's objectForKey:"WebSubresources") |
199 | repeat with itemWebSubresources in ocidWebSubresourcesArray |
200 | |
201 | |
202 | |
203 | set strMIME to (itemWebSubresources's objectForKey:"WebResourceMIMEType") as text |
204 | if strMIME contains "image" then |
205 | set ocidWebPath to (itemWebSubresources's objectForKey:"WebResourceURL") |
206 | set strWebPath to ocidWebPath as text |
207 | ##### |
208 | if strWebPath contains "data:image" then |
209 | #####インラインSVGを保存するやっつけ感満載の処理… |
210 | |
211 | if strWebPath contains "data:image/svg+xml;charset=utf-8," then |
212 | set strFileName to "svg-" & numInlineNO & ".svg" as text |
213 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName) |
214 | |
215 | set AppleScript's text item delimiters to "data:image/svg+xml;charset=utf-8," |
216 | set listDelim to every text item of strWebPath |
217 | set AppleScript's text item delimiters to "" |
218 | set strInLineContents to item 2 of listDelim |
219 | set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents) |
220 | set ocidDencodedContents to (ocidInlineContents's stringByRemovingPercentEncoding) |
221 | set boolDone to (ocidDencodedContents's writeToURL:ocidSaveFilePathURL atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)) |
222 | |
223 | else if strWebPath contains "data:image/svg+xml;charset=utf-8," then |
224 | set strFileName to "svg-" & numInlineNO & ".svg" as text |
225 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName) |
226 | |
227 | set AppleScript's text item delimiters to "data:image/svg+xml;charset=utf-8," |
228 | set listDelim to every text item of strWebPath |
229 | set AppleScript's text item delimiters to "" |
230 | set strInLineContents to item 2 of listDelim |
231 | set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents) |
232 | set ocidDencodedContents to (ocidInlineContents's stringByRemovingPercentEncoding) |
233 | set boolDone to (ocidDencodedContents's writeToURL:ocidSaveFilePathURL atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)) |
234 | |
235 | else if strWebPath contains "data:image/svg+xml;base64," then |
236 | set strFileName to "png-" & numInlineNO & ".svg" as text |
237 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName) |
238 | |
239 | set AppleScript's text item delimiters to "data:image/svg+xml;base64," |
240 | set listDelim to every text item of strWebPath |
241 | set AppleScript's text item delimiters to "" |
242 | set strInLineContents to item 2 of listDelim |
243 | set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents) |
244 | set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:ocidInlineContents options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters)) |
245 | set boolResults to (ocidData's writeToURL:ocidSaveFilePathURL atomically:true) |
246 | |
247 | else if strWebPath contains "data:image/png;base64," then |
248 | set strFileName to "png-" & numInlineNO & ".png" as text |
249 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName) |
250 | |
251 | set AppleScript's text item delimiters to "data:image/png;base64," |
252 | set listDelim to every text item of strWebPath |
253 | set AppleScript's text item delimiters to "" |
254 | set strInLineContents to item 2 of listDelim |
255 | set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents) |
256 | set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:ocidInlineContents options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters)) |
257 | set boolResults to (ocidData's writeToURL:ocidSaveFilePathURL atomically:true) |
258 | |
259 | else if strWebPath contains "data:image/jpeg;base64," then |
260 | set strFileName to "jpg-" & numInlineNO & ".jpg" as text |
261 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName) |
262 | |
263 | set AppleScript's text item delimiters to "data:image/jpeg;base64," |
264 | set listDelim to every text item of strWebPath |
265 | set AppleScript's text item delimiters to "" |
266 | set strInLineContents to item 2 of listDelim |
267 | set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents) |
268 | set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:ocidInlineContents options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters)) |
269 | set boolResults to (ocidData's writeToURL:ocidSaveFilePathURL atomically:true) |
270 | |
271 | else if strWebPath contains "data:image/jpg;base64," then |
272 | set strFileName to "jpg-" & numInlineNO & ".jpg" as text |
273 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName) |
274 | |
275 | set AppleScript's text item delimiters to "data:image/jpg;base64," |
276 | set listDelim to every text item of strWebPath |
277 | set AppleScript's text item delimiters to "" |
278 | set strInLineContents to item 2 of listDelim |
279 | set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents) |
280 | set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:ocidInlineContents options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters)) |
281 | set boolResults to (ocidData's writeToURL:ocidSaveFilePathURL atomically:true) |
282 | |
283 | else |
284 | log "インラインイメージはとりあえず処理しない" |
285 | end if |
286 | set numInlineNO to numInlineNO + 1 as text |
287 | else |
288 | set ocidWebURL to (refMe's NSURL's alloc()'s initWithString:ocidWebPath) |
289 | set ocidFileName to ocidWebURL's lastPathComponent() |
290 | set strExtensionName to ocidWebURL's pathExtension() as text |
291 | if strExtensionName is "" then |
292 | log "拡張子がない" |
293 | ocidFileName's stringByDeletingPathExtension() |
294 | if strMIME contains "png" then |
295 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"png") |
296 | else if strMIME contains "jpeg" then |
297 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"jpg") |
298 | else if strMIME contains "jpg" then |
299 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"jpg") |
300 | else if strMIME contains "webp" then |
301 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"webp") |
302 | else if strMIME contains "gif" then |
303 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"gif") |
304 | else if strMIME contains "svg" then |
305 | set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"svg") |
306 | end if |
307 | end if |
308 | set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:ocidFileName) |
309 | set ocidImageDataBase64 to (itemWebSubframeDict's objectForKey:"WebResourceData") |
310 | |
311 | if ocidImageDataBase64 ≠ (missing value) then |
312 | set boolResults to (ocidImageDataBase64's writeToURL:ocidSaveFilePathURL atomically:true) |
313 | end if |
314 | end if |
315 | else |
316 | log "イメージ以外は処理しない" |
317 | end if |
318 | set ocidImageDataBase64 to "" |
319 | end repeat |
320 | |
321 | end repeat |
322 | |
323 | ############################################## |
324 | ## 書き出し先をFinderで開く |
325 | ############################################## |
326 | appShardWorkspace's openURL:ocidSaveFileDirURL |
327 | |
328 | |
329 | |
330 | return |
331 | |
332 | |
333 | |
334 | |
335 | |
AppleScriptで生成しました |
webarchiveから画像データーを書き出します
| 固定リンク