webarchive

[com.apple.webarchive]webarchiveから画像データーを書き出します ちょっと修正

webarchiveから画像データーを書き出.scpt

AppleScript サンプルコード

【スクリプトエディタで開く】 |

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004webarchiveから画像データーを書き出します
005v1.1 ちょっと修正版
006v1.2 ダイアログ呼び出しを変更
007v1.3 missing value対策を入れた
008com.cocolog-nifty.quicktimer.icefloe *)
009----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
010
011use AppleScript version "2.8"
012use framework "Foundation"
013use framework "AppKit"
014use scripting additions
015
016
017property refMe : a reference to current application
018
019
020set appFileManager to refMe's NSFileManager's defaultManager()
021set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()
022
023
024###ダイアログを前面に出す
025set strName to (name of current application) as text
026if strName is "osascript" then
027  tell application "SystemUIServer" to activate
028else
029  tell current application to activate
030end if
031
032
033####ダイアログで使うデフォルトロケーション
034set ocidGetUrlArray to appFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask)
035set ocidDesktopDirPathURL to ocidGetUrlArray's firstObject()
036set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
037
038####UTIリスト com.apple.webarchiveかplist
039set listUTI to {"com.apple.webarchive", "com.apple.property-list"} as list
040
041
042####ダイアログを出す
043tell 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
046end tell
047
048set strFilePath to (POSIX path of aliasFilePath) as text
049####ドキュメントのパスをNSString
050set ocidFilePath to refMe's NSString's stringWithString:(strFilePath)
051set ocidFilePath to ocidFilePath's stringByStandardizingPath()
052set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false)
053###選んだファイルのディレクトリ
054set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent()
055
056##############################################
057## 本処理
058##############################################
059set ocidOption to (refMe's NSDataWritingAtomic)
060###PLIST ROOT
061set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s init()
062set listReadPlistData to refMe's NSMutableDictionary's dictionaryWithContentsOfURL:(ocidFilePathURL) |error| :(reference)
063set ocidPlistDict to item 1 of listReadPlistData
064##############################################
065## 本処理   WebMainResource
066##############################################
067###ダウンロードしたURLのホスト名を保存先フォルダ名にする
068set ocidWebMainResourceDict to ocidPlistDict's objectForKey:"WebMainResource"
069set strMainURLString to (ocidWebMainResourceDict's valueForKey:("WebResourceURL")) as text
070set ocidURLString to refMe's NSString's stringWithString:(strMainURLString)
071set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLString)
072set strHostName to ocidURL's |host|() as text
073if strHostName ends with ".app" then
074  set strHostName to (strHostName & "_Folder") as text
075end if
076set strHostName to (strHostName & "/Images") as text
077###イメージ保存先
078set ocidSaveFileDirURL to (ocidContainerDirURL's URLByAppendingPathComponent:(strHostName))
079###保存先ディレクトリを作成
080set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
081ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions)
082appFileManager's createDirectoryAtURL:(ocidSaveFileDirURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error| :(reference)
083
084##############################################
085## 本処理   WebSubresources
086##############################################
087###WebSubresources
088set ocidWebSubresourcesArray to refMe's NSMutableArray's alloc()'s init()
089set ocidWebSubresourcesArray to ocidPlistDict's objectForKey:("WebSubresources")
090###インラインイメージ用のカウント
091set numInlineNO to 1 as integer
092repeat 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 ""
206end repeat
207
208
209##############################################
210## 本処理   WebSubframeArchives
211##############################################
212set ocidWebSubframeArray to refMe's NSMutableArray's alloc()'s init()
213###WebSubframe
214set ocidWebSubframeArray to ocidPlistDict's objectForKey:("WebSubframeArchives")
215if ocidWebSubframeArray = (missing value) then
216  appShardWorkspace's openURL:(ocidSaveFileDirURL)
217  tell application "SystemUIServer" to quit
218  return
219end if
220###インラインイメージ用のカウント
221set numInlineNO to 1 as integer
222repeat 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
347end repeat
348
349##############################################
350## 書き出し先をFinderで開く
351##############################################
352appShardWorkspace's openURL:(ocidSaveFileDirURL)
353
354tell application "SystemUIServer" to quit
355
356return
357
358
359
360
361
AppleScriptで生成しました

【スクリプトエディタで開く】 |

webarchiveから画像データーを書き出.scpt
ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004webarchiveから画像データーを書き出します
005v1.1 ちょっと修正版
006com.cocolog-nifty.quicktimer.icefloe *)
007----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
008
009use AppleScript version "2.8"
010use framework "Foundation"
011use framework "AppKit"
012use scripting additions
013
014
015property refMe : a reference to current application
016
017
018set objFileManager to refMe's NSFileManager's defaultManager()
019set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()
020
021
022####ダイアログで使うデフォルトロケーション
023set ocidGetUrlArray to (objFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask))
024set ocidDesktopDirPathURL to ocidGetUrlArray's objectAtIndex:0
025set aliasDefaultLocation to ocidDesktopDirPathURL as alias
026
027####UTIリスト com.apple.webarchiveplist
028set listUTI to {"com.apple.webarchive", "com.apple.property-list"}
029
030
031####ダイアログを出す
032set 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
034set strFilePath to (POSIX path of aliasFilePath) as text
035####ドキュメントのパスをNSString
036set ocidFilePath to refMe's NSString's stringWithString:(strFilePath)
037set ocidFilePath to ocidFilePath's stringByStandardizingPath
038set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false
039###選んだファイルのディレクトリ
040set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent
041
042##############################################
043## 本処理
044##############################################
045###PLIST ROOT
046set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
047set listReadPlistData to refMe's NSMutableDictionary's dictionaryWithContentsOfURL:(ocidFilePathURL) |error|:(reference)
048set ocidPlistDict to item 1 of listReadPlistData
049##############################################
050## 本処理   WebMainResource
051##############################################
052###ダウンロードしたURLのホスト名を保存先フォルダ名にする
053set ocidWebMainResourceDict to ocidPlistDict's objectForKey:"WebMainResource"
054set ocidMainURLString to ocidWebMainResourceDict's valueForKey:("WebResourceURL")
055set ocidURLString to refMe's NSString's stringWithString:(ocidMainURLString)
056set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidMainURLString)
057set strHostName to ocidURL's |host|() as text
058set strHostName to (strHostName & "/Images")
059###イメージ保存先
060set ocidSaveFileDirURL to (ocidContainerDirURL's URLByAppendingPathComponent:(strHostName))
061###保存先ディレクトリを作成
062set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
063ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions)
064objFileManager's createDirectoryAtURL:ocidSaveFileDirURL withIntermediateDirectories:true attributes:ocidAttrDict |error|:(reference)
065
066##############################################
067## 本処理   WebSubresources
068##############################################
069###WebSubresources
070set ocidWebSubresourcesArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
071set ocidWebSubresourcesArray to ocidPlistDict's objectForKey:"WebSubresources"
072###インラインイメージ用のカウント
073set numInlineNO to 1 as text
074repeat 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 ""
188end repeat
189##############################################
190## 本処理   WebSubframeArchives
191##############################################
192
193###WebSubframe
194set ocidWebSubframeArray to ocidPlistDict's objectForKey:"WebSubframeArchives"
195###インラインイメージ用のカウント
196set numInlineNO to 1 as text
197repeat 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   
321end repeat
322
323##############################################
324## 書き出し先をFinderで開く
325##############################################
326appShardWorkspace's openURL:ocidSaveFileDirURL
327
328
329
330return
331
332
333
334
335
AppleScriptで生成しました

webarchiveから画像データーを書き出します

【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
# webarchiveから画像データーを書き出します
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


property refMe : a reference to current application


set objFileManager to refMe's NSFileManager's defaultManager()
set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()


####ダイアログで使うデフォルトロケーション
set ocidGetUrlArray to (objFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopDirPathURL to ocidGetUrlArray's objectAtIndex:0
set aliasDefaultLocation to ocidDesktopDirPathURL as alias

####UTIリスト com.apple.webarchiveかplist
set listUTI to {"com.apple.webarchive", "com.apple.property-list"}


####ダイアログを出す
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

set strFilePath to (POSIX path of aliasFilePath) as text
####ドキュメントのパスをNSString
set ocidFilePath to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePath's stringByStandardizingPath
set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false
###選んだファイルのディレクトリ
set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent

##############################################
## 本処理
##############################################
###PLIST ROOT
set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
set listReadPlistData to refMe's NSMutableDictionary's dictionaryWithContentsOfURL:(ocidFilePathURL) |error|:(reference)
set ocidPlistDict to item 1 of listReadPlistData
##############################################
## 本処理   WebMainResource
##############################################
###ダウンロードしたURLのホスト名を保存先フォルダ名にする
set ocidWebMainResourceDict to ocidPlistDict's objectForKey:"WebMainResource"
set ocidMainURLString to ocidWebMainResourceDict's valueForKey:("WebResourceURL")
set ocidURLString to refMe's NSString's stringWithString:(ocidMainURLString)
set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidMainURLString)
set strHostName to ocidURL's |host|() as text
set strHostName to (strHostName & "/Images")
###イメージ保存先
set ocidSaveFileDirURL to (ocidContainerDirURL's URLByAppendingPathComponent:(strHostName))
###保存先ディレクトリを作成
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions)
objFileManager's createDirectoryAtURL:ocidSaveFileDirURL withIntermediateDirectories:true attributes:ocidAttrDict |error|:(reference)

##############################################
## 本処理   WebSubresources
##############################################
###WebSubresources
set ocidWebSubresourcesArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
set ocidWebSubresourcesArray to ocidPlistDict's objectForKey:"WebSubresources"
###インラインイメージ用のカウント
set numInlineNO to 1 as text
repeat with itemWebSubresourcesDict in ocidWebSubresourcesArray
  set strMIME to (itemWebSubresourcesDict's objectForKey:"WebResourceMIMEType") as text
  if strMIME contains "image" then
    set ocidWebPath to (itemWebSubresourcesDict's objectForKey:"WebResourceURL")
    set strWebPath to ocidWebPath as text
    #####
    if strWebPath contains "data:image" then
      #####インラインSVGを保存するやっつけ感満載の処理…
      
      if strWebPath contains "data:image/svg+xml;charset=utf-8," then
set strFileName to "svg-" & numInlineNO & ".svg" as text
set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName)
set AppleScript's text item delimiters to "data:image/svg+xml;charset=utf-8,"
set listDelim to every text item of strWebPath
set AppleScript's text item delimiters to ""
set strInLineContents to item 2 of listDelim
set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents)
set ocidDencodedContents to (ocidInlineContents's stringByRemovingPercentEncoding)
set boolDone to (ocidDencodedContents's writeToURL:ocidSaveFilePathURL atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))
      else if strWebPath contains "data:image/svg+xml;charset=utf-8," then
set strFileName to "svg-" & numInlineNO & ".svg" as text
set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName)
set AppleScript's text item delimiters to "data:image/svg+xml;charset=utf-8,"
set listDelim to every text item of strWebPath
set AppleScript's text item delimiters to ""
set strInLineContents to item 2 of listDelim
set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents)
set ocidDencodedContents to (ocidInlineContents's stringByRemovingPercentEncoding)
set boolDone to (ocidDencodedContents's writeToURL:ocidSaveFilePathURL atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))
      else if strWebPath contains "data:image/svg+xml;base64," then
set strFileName to "png-" & numInlineNO & ".svg" as text
set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName)
set AppleScript's text item delimiters to "data:image/svg+xml;base64,"
set listDelim to every text item of strWebPath
set AppleScript's text item delimiters to ""
set strInLineContents to item 2 of listDelim
set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents)
set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:ocidInlineContents options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters))
set boolResults to (ocidData's writeToURL:ocidSaveFilePathURL atomically:true)
      else if strWebPath contains "data:image/png;base64," then
set strFileName to "png-" & numInlineNO & ".png" as text
set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName)
set AppleScript's text item delimiters to "data:image/png;base64,"
set listDelim to every text item of strWebPath
set AppleScript's text item delimiters to ""
set strInLineContents to item 2 of listDelim
set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents)
set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:ocidInlineContents options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters))
set boolResults to (ocidData's writeToURL:ocidSaveFilePathURL atomically:true)
      else if strWebPath contains "data:image/jpeg;base64," then
set strFileName to "jpg-" & numInlineNO & ".jpg" as text
set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName)
set AppleScript's text item delimiters to "data:image/jpeg;base64,"
set listDelim to every text item of strWebPath
set AppleScript's text item delimiters to ""
set strInLineContents to item 2 of listDelim
set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents)
set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:ocidInlineContents options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters))
set boolResults to (ocidData's writeToURL:ocidSaveFilePathURL atomically:true)
      else if strWebPath contains "data:image/jpg;base64," then
set strFileName to "jpg-" & numInlineNO & ".jpg" as text
set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName)
set AppleScript's text item delimiters to "data:image/jpg;base64,"
set listDelim to every text item of strWebPath
set AppleScript's text item delimiters to ""
set strInLineContents to item 2 of listDelim
set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents)
set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:ocidInlineContents options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters))
set boolResults to (ocidData's writeToURL:ocidSaveFilePathURL atomically:true)
      else
log "インラインイメージはとりあえず処理しない"
      end if
      set numInlineNO to numInlineNO + 1 as text
    else
      set ocidWebURL to (refMe's NSURL's alloc()'s initWithString:ocidWebPath)
      set ocidFileName to ocidWebURL's lastPathComponent()
      set strExtensionName to ocidWebURL's pathExtension() as text
      if strExtensionName is "" then
log "拡張子がない"
ocidFileName's stringByDeletingPathExtension()
if strMIME contains "png" then
set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"png")
else if strMIME contains "jpeg" then
set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"jpg")
else if strMIME contains "jpg" then
set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"jpg")
else if strMIME contains "webp" then
set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"webp")
else if strMIME contains "gif" then
set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"gif")
else if strMIME contains "svg" then
set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"svg")
end if
      end if
      set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:ocidFileName)
      set ocidImageDataBase64 to (itemWebSubresourcesDict's objectForKey:"WebResourceData")
      set boolResults to (ocidImageDataBase64's writeToURL:ocidSaveFilePathURL atomically:true)
    end if
  else
    log "イメージ以外は処理しない"
  end if
  set ocidImageDataBase64 to ""
end repeat
##############################################
## 本処理   WebSubframeArchives
##############################################

###WebSubframe
set ocidWebSubframeArray to ocidPlistDict's objectForKey:"WebSubframeArchives"
###インラインイメージ用のカウント
set numInlineNO to 1 as text
repeat with itemWebSubframeDict in ocidWebSubframeArray
  set ocidWebSubresourcesArray to (itemWebSubframeDict's objectForKey:"WebSubresources")
  repeat with itemWebSubresources in ocidWebSubresourcesArray
    
    
    
    set strMIME to (itemWebSubresources's objectForKey:"WebResourceMIMEType") as text
    if strMIME contains "image" then
      set ocidWebPath to (itemWebSubresources's objectForKey:"WebResourceURL")
      set strWebPath to ocidWebPath as text
      #####
      if strWebPath contains "data:image" then
#####インラインSVGを保存するやっつけ感満載の処理…
if strWebPath contains "data:image/svg+xml;charset=utf-8," then
set strFileName to "svg-" & numInlineNO & ".svg" as text
set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName)
set AppleScript's text item delimiters to "data:image/svg+xml;charset=utf-8,"
set listDelim to every text item of strWebPath
set AppleScript's text item delimiters to ""
set strInLineContents to item 2 of listDelim
set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents)
set ocidDencodedContents to (ocidInlineContents's stringByRemovingPercentEncoding)
set boolDone to (ocidDencodedContents's writeToURL:ocidSaveFilePathURL atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))
else if strWebPath contains "data:image/svg+xml;charset=utf-8," then
set strFileName to "svg-" & numInlineNO & ".svg" as text
set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName)
set AppleScript's text item delimiters to "data:image/svg+xml;charset=utf-8,"
set listDelim to every text item of strWebPath
set AppleScript's text item delimiters to ""
set strInLineContents to item 2 of listDelim
set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents)
set ocidDencodedContents to (ocidInlineContents's stringByRemovingPercentEncoding)
set boolDone to (ocidDencodedContents's writeToURL:ocidSaveFilePathURL atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))
else if strWebPath contains "data:image/svg+xml;base64," then
set strFileName to "png-" & numInlineNO & ".svg" as text
set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName)
set AppleScript's text item delimiters to "data:image/svg+xml;base64,"
set listDelim to every text item of strWebPath
set AppleScript's text item delimiters to ""
set strInLineContents to item 2 of listDelim
set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents)
set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:ocidInlineContents options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters))
set boolResults to (ocidData's writeToURL:ocidSaveFilePathURL atomically:true)
else if strWebPath contains "data:image/png;base64," then
set strFileName to "png-" & numInlineNO & ".png" as text
set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName)
set AppleScript's text item delimiters to "data:image/png;base64,"
set listDelim to every text item of strWebPath
set AppleScript's text item delimiters to ""
set strInLineContents to item 2 of listDelim
set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents)
set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:ocidInlineContents options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters))
set boolResults to (ocidData's writeToURL:ocidSaveFilePathURL atomically:true)
else if strWebPath contains "data:image/jpeg;base64," then
set strFileName to "jpg-" & numInlineNO & ".jpg" as text
set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName)
set AppleScript's text item delimiters to "data:image/jpeg;base64,"
set listDelim to every text item of strWebPath
set AppleScript's text item delimiters to ""
set strInLineContents to item 2 of listDelim
set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents)
set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:ocidInlineContents options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters))
set boolResults to (ocidData's writeToURL:ocidSaveFilePathURL atomically:true)
else if strWebPath contains "data:image/jpg;base64," then
set strFileName to "jpg-" & numInlineNO & ".jpg" as text
set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName)
set AppleScript's text item delimiters to "data:image/jpg;base64,"
set listDelim to every text item of strWebPath
set AppleScript's text item delimiters to ""
set strInLineContents to item 2 of listDelim
set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents)
set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:ocidInlineContents options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters))
set boolResults to (ocidData's writeToURL:ocidSaveFilePathURL atomically:true)
else
log "インラインイメージはとりあえず処理しない"
end if
set numInlineNO to numInlineNO + 1 as text
      else
set ocidWebURL to (refMe's NSURL's alloc()'s initWithString:ocidWebPath)
set ocidFileName to ocidWebURL's lastPathComponent()
set strExtensionName to ocidWebURL's pathExtension() as text
if strExtensionName is "" then
log "拡張子がない"
ocidFileName's stringByDeletingPathExtension()
if strMIME contains "png" then
set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"png")
else if strMIME contains "jpeg" then
set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"jpg")
else if strMIME contains "jpg" then
set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"jpg")
else if strMIME contains "webp" then
set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"webp")
else if strMIME contains "gif" then
set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"gif")
else if strMIME contains "svg" then
set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"svg")
end if
end if
set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:ocidFileName)
set ocidImageDataBase64 to (itemWebSubframeDict's objectForKey:"WebResourceData")
if ocidImageDataBase64 ≠ (missing value) then
set boolResults to (ocidImageDataBase64's writeToURL:ocidSaveFilePathURL atomically:true)
end if
      end if
    else
      log "イメージ以外は処理しない"
    end if
    set ocidImageDataBase64 to ""
  end repeat
  
end repeat

##############################################
## 書き出し先をFinderで開く
##############################################
appShardWorkspace's openURL:ocidSaveFileDirURL



return







|

[webarchive]Safariでwebarchiveを保存して画像データを取り出す

webarchiveから画像データーを書き出.scpt

AppleScript サンプルコード

【スクリプトエディタで開く】 |

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004webarchiveから画像データーを書き出します
005v1.1 ちょっと修正版
006v1.2 ダイアログ呼び出しを変更
007v1.3 missing value対策を入れた
008com.cocolog-nifty.quicktimer.icefloe *)
009----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
010
011use AppleScript version "2.8"
012use framework "Foundation"
013use framework "AppKit"
014use scripting additions
015
016
017property refMe : a reference to current application
018
019
020set appFileManager to refMe's NSFileManager's defaultManager()
021set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()
022
023
024###ダイアログを前面に出す
025set strName to (name of current application) as text
026if strName is "osascript" then
027  tell application "SystemUIServer" to activate
028else
029  tell current application to activate
030end if
031
032
033####ダイアログで使うデフォルトロケーション
034set ocidGetUrlArray to appFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask)
035set ocidDesktopDirPathURL to ocidGetUrlArray's firstObject()
036set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
037
038####UTIリスト com.apple.webarchiveかplist
039set listUTI to {"com.apple.webarchive", "com.apple.property-list"} as list
040
041
042####ダイアログを出す
043tell 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
046end tell
047
048set strFilePath to (POSIX path of aliasFilePath) as text
049####ドキュメントのパスをNSString
050set ocidFilePath to refMe's NSString's stringWithString:(strFilePath)
051set ocidFilePath to ocidFilePath's stringByStandardizingPath()
052set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false)
053###選んだファイルのディレクトリ
054set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent()
055
056##############################################
057## 本処理
058##############################################
059set ocidOption to (refMe's NSDataWritingAtomic)
060###PLIST ROOT
061set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s init()
062set listReadPlistData to refMe's NSMutableDictionary's dictionaryWithContentsOfURL:(ocidFilePathURL) |error| :(reference)
063set ocidPlistDict to item 1 of listReadPlistData
064##############################################
065## 本処理   WebMainResource
066##############################################
067###ダウンロードしたURLのホスト名を保存先フォルダ名にする
068set ocidWebMainResourceDict to ocidPlistDict's objectForKey:"WebMainResource"
069set strMainURLString to (ocidWebMainResourceDict's valueForKey:("WebResourceURL")) as text
070set ocidURLString to refMe's NSString's stringWithString:(strMainURLString)
071set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLString)
072set strHostName to ocidURL's |host|() as text
073if strHostName ends with ".app" then
074  set strHostName to (strHostName & "_Folder") as text
075end if
076set strHostName to (strHostName & "/Images") as text
077###イメージ保存先
078set ocidSaveFileDirURL to (ocidContainerDirURL's URLByAppendingPathComponent:(strHostName))
079###保存先ディレクトリを作成
080set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
081ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions)
082appFileManager's createDirectoryAtURL:(ocidSaveFileDirURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error| :(reference)
083
084##############################################
085## 本処理   WebSubresources
086##############################################
087###WebSubresources
088set ocidWebSubresourcesArray to refMe's NSMutableArray's alloc()'s init()
089set ocidWebSubresourcesArray to ocidPlistDict's objectForKey:("WebSubresources")
090###インラインイメージ用のカウント
091set numInlineNO to 1 as integer
092repeat 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 ""
206end repeat
207
208
209##############################################
210## 本処理   WebSubframeArchives
211##############################################
212set ocidWebSubframeArray to refMe's NSMutableArray's alloc()'s init()
213###WebSubframe
214set ocidWebSubframeArray to ocidPlistDict's objectForKey:("WebSubframeArchives")
215if ocidWebSubframeArray = (missing value) then
216  appShardWorkspace's openURL:(ocidSaveFileDirURL)
217  tell application "SystemUIServer" to quit
218  return
219end if
220###インラインイメージ用のカウント
221set numInlineNO to 1 as integer
222repeat 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
347end repeat
348
349##############################################
350## 書き出し先をFinderで開く
351##############################################
352appShardWorkspace's openURL:(ocidSaveFileDirURL)
353
354tell application "SystemUIServer" to quit
355
356return
357
358
359
360
361
AppleScriptで生成しました

【スクリプトエディタで開く】 |

webarchiveから画像データーを書き出.scpt
ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004webarchiveから画像データーを書き出します
005v1.1 ちょっと修正版
006com.cocolog-nifty.quicktimer.icefloe *)
007----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
008
009use AppleScript version "2.8"
010use framework "Foundation"
011use framework "AppKit"
012use scripting additions
013
014
015property refMe : a reference to current application
016
017
018set objFileManager to refMe's NSFileManager's defaultManager()
019set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()
020
021
022####ダイアログで使うデフォルトロケーション
023set ocidGetUrlArray to (objFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask))
024set ocidDesktopDirPathURL to ocidGetUrlArray's objectAtIndex:0
025set aliasDefaultLocation to ocidDesktopDirPathURL as alias
026
027####UTIリスト com.apple.webarchiveplist
028set listUTI to {"com.apple.webarchive", "com.apple.property-list"}
029
030
031####ダイアログを出す
032set 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
034set strFilePath to (POSIX path of aliasFilePath) as text
035####ドキュメントのパスをNSString
036set ocidFilePath to refMe's NSString's stringWithString:(strFilePath)
037set ocidFilePath to ocidFilePath's stringByStandardizingPath
038set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false
039###選んだファイルのディレクトリ
040set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent
041
042##############################################
043## 本処理
044##############################################
045###PLIST ROOT
046set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
047set listReadPlistData to refMe's NSMutableDictionary's dictionaryWithContentsOfURL:(ocidFilePathURL) |error|:(reference)
048set ocidPlistDict to item 1 of listReadPlistData
049##############################################
050## 本処理   WebMainResource
051##############################################
052###ダウンロードしたURLのホスト名を保存先フォルダ名にする
053set ocidWebMainResourceDict to ocidPlistDict's objectForKey:"WebMainResource"
054set ocidMainURLString to ocidWebMainResourceDict's valueForKey:("WebResourceURL")
055set ocidURLString to refMe's NSString's stringWithString:(ocidMainURLString)
056set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidMainURLString)
057set strHostName to ocidURL's |host|() as text
058set strHostName to (strHostName & "/Images")
059###イメージ保存先
060set ocidSaveFileDirURL to (ocidContainerDirURL's URLByAppendingPathComponent:(strHostName))
061###保存先ディレクトリを作成
062set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
063ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions)
064objFileManager's createDirectoryAtURL:ocidSaveFileDirURL withIntermediateDirectories:true attributes:ocidAttrDict |error|:(reference)
065
066##############################################
067## 本処理   WebSubresources
068##############################################
069###WebSubresources
070set ocidWebSubresourcesArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
071set ocidWebSubresourcesArray to ocidPlistDict's objectForKey:"WebSubresources"
072###インラインイメージ用のカウント
073set numInlineNO to 1 as text
074repeat 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 ""
188end repeat
189##############################################
190## 本処理   WebSubframeArchives
191##############################################
192
193###WebSubframe
194set ocidWebSubframeArray to ocidPlistDict's objectForKey:"WebSubframeArchives"
195###インラインイメージ用のカウント
196set numInlineNO to 1 as text
197repeat 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   
321end repeat
322
323##############################################
324## 書き出し先をFinderで開く
325##############################################
326appShardWorkspace's openURL:ocidSaveFileDirURL
327
328
329
330return
331
332
333
334
335
AppleScriptで生成しました

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#

#
#
#             com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "WebKit"


property refMe : a reference to current application
property refNSWorkspace : a reference to refMe's NSWorkspace
set ocidShardWorkspace to refNSWorkspace's sharedWorkspace()
set objFileManager to refMe's NSFileManager's defaultManager()


property strFilePath : missing value


tell application "Safari"
    tell document 1
        set strURL to its URL
    end tell
end tell

###SaveFileName = host name + date time
set ocidURLstring to refMe's NSString's stringWithString:strURL
set ocidWebURL to refMe's NSURL's alloc()'s initWithString:ocidURLstring
set strHostName to ocidWebURL's |host|() as text
set strDateTimeNO to doGetDateNo("yyyyMMdd-hhmmss")
set strFileName to strHostName & "." & strDateTimeNO & ".webarchive" as text

####SavePathDir
set ocidGetUrlArray to (objFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDownloadsDirPathURL to ocidGetUrlArray's objectAtIndex:0
set ocidSaveFileDirURL to (ocidDownloadsDirPathURL's URLByAppendingPathComponent:"Safari")
###Make Folder SavePathDir
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions)
objFileManager's createDirectoryAtURL:ocidSaveFileDirURL withIntermediateDirectories:true attributes:ocidAttrDict |error|:(reference)
###SaveFilePath
set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName)
set ocidFilePath to ocidSaveFilePathURL's |path|() as text
set strFilePath to ocidFilePath as text

###################################
##
(*
Base Script
https://www.macscripter.net/t/
    webarchive-of-document-1-of-safari-saved-to-desktop/72583
*)
##
###################################

my archivePage:strURL toPath:strFilePath

on archivePage:argPageURL toPath:argPath
    set my strFilePath to argPath
    my performSelectorOnMainThread:"setUpWebViewForPage:" withObject:argPageURL waitUntilDone:false
end archivePage:toPath:

on setUpWebViewForPage:argPageURL
    
    set theView to refMe's WebView's alloc()'s initWithFrame:{origin:{x:0, y:0}, |size|:{width:100, height:100}}
    theView's setFrameLoadDelegate:me
    theView's setMainFrameURL:argPageURL
end setUpWebViewForPage:


on WebView:aWebView didFinishLoadForFrame:argWebFrame
    if argWebFrame = aWebView's mainFrame() then
        set theArchiveData to argWebFrame's dataSource()'s webArchive()'s |data|()
        theArchiveData's writeToFile:strFilePath atomically:true
    end if
end WebView:didFinishLoadForFrame:

on WebView:WebView didFailLoadWithError:argError forFrame:argWebFrame
    WebView's stopLoading:me
end WebView:didFailLoadWithError:forFrame:


###################################
#### webarchive が生成されたか?チェック
###################################

repeat
    ##### webarchiveの生成チェック
    set boolFileExists to (objFileManager's fileExistsAtPath:ocidFilePath isDirectory:false)
    if boolFileExists is true then
        exit repeat
    else
        delay 2
    end if
    
end repeat

############################
####画像抜き出し先
############################
set ocidSaveImageDirURL to (ocidDownloadsDirPathURL's URLByAppendingPathComponent:"images")
###保存先ディレクトリを作成
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions)
objFileManager's createDirectoryAtURL:ocidSaveImageDirURL withIntermediateDirectories:true attributes:ocidAttrDict |error|:(reference)

##############################################
## 本処理
##############################################
###PLIST ROOT
set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
set listReadPlistData to refMe's NSMutableDictionary's dictionaryWithContentsOfURL:ocidSaveFilePathURL |error|:(reference)
set ocidPlistDict to item 1 of listReadPlistData


##############################################
## 本処理   WebSubresources
##############################################
###WebSubresources
set ocidWebSubresourcesArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
set ocidWebSubresourcesArray to ocidPlistDict's objectForKey:"WebSubresources"
###インラインイメージ用のカウント
set numInlineNO to 1 as text
repeat with itemWebSubresourcesDict in ocidWebSubresourcesArray
    set strMIME to (itemWebSubresourcesDict's objectForKey:"WebResourceMIMEType") as text
    if strMIME contains "image" then
        set ocidWebPath to (itemWebSubresourcesDict's objectForKey:"WebResourceURL")
        set strWebPath to ocidWebPath as text
        #####
        if strWebPath contains "data:image" then
            #####インラインSVGを保存するやっつけ感満載の処理…
            if strWebPath contains "data:image/svg+xml;charset=utf-8," then
                set strFileName to "svg-" & numInlineNO & ".svg" as text
                set ocidSaveFilePathURL to (ocidSaveImageDirURL's URLByAppendingPathComponent:strFileName)
                
                set AppleScript's text item delimiters to "data:image/svg+xml;charset=utf-8,"
                set listDelim to every text item of strWebPath
                set AppleScript's text item delimiters to ""
                set strInLineContents to item 2 of listDelim
                set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents)
                set ocidDencodedContents to (ocidInlineContents's stringByRemovingPercentEncoding)
                set boolDone to (ocidDencodedContents's writeToURL:ocidSaveFilePathURL atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))
            else if strWebPath contains "data:image/svg+xml," then
                set strFileName to "svg-" & numInlineNO & ".svg" as text
                set ocidSaveFilePathURL to (ocidSaveImageDirURL's URLByAppendingPathComponent:strFileName)
                
                set AppleScript's text item delimiters to "data:image/svg+xml,"
                set listDelim to every text item of strWebPath
                set AppleScript's text item delimiters to ""
                set strInLineContents to item 2 of listDelim
                set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents)
                set ocidDencodedContents to (ocidInlineContents's stringByRemovingPercentEncoding)
                set boolDone to (ocidDencodedContents's writeToURL:ocidSaveFilePathURL atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))
            else if strWebPath contains "data:image/svg+xml;base64," then
                set strFileName to "png-" & numInlineNO & ".svg" as text
                set ocidSaveFilePathURL to (ocidSaveImageDirURL's URLByAppendingPathComponent:strFileName)
                
                set AppleScript's text item delimiters to "data:image/svg+xml;base64,"
                set listDelim to every text item of strWebPath
                set AppleScript's text item delimiters to ""
                set strInLineContents to item 2 of listDelim
                set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents)
                set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:ocidInlineContents options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters))
                set boolResults to (ocidData's writeToURL:ocidSaveFilePathURL atomically:true)
                
            else if strWebPath contains "data:image/png;base64," then
                set strFileName to "png-" & numInlineNO & ".png" as text
                set ocidSaveFilePathURL to (ocidSaveImageDirURL's URLByAppendingPathComponent:strFileName)
                
                set AppleScript's text item delimiters to "data:image/png;base64,"
                set listDelim to every text item of strWebPath
                set AppleScript's text item delimiters to ""
                set strInLineContents to item 2 of listDelim
                set ocidInlineContents to (refMe's NSString's stringWithString:strInLineContents)
                set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:ocidInlineContents options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters))
                set boolResults to (ocidData's writeToURL:ocidSaveFilePathURL atomically:true)
                
                
            else
                
                log "インラインイメージはとりあえず処理しない"
            end if
            
            
            
            set numInlineNO to numInlineNO + 1 as text
        else
            set ocidWebURL to (refMe's NSURL's alloc()'s initWithString:ocidWebPath)
            set ocidFileName to ocidWebURL's lastPathComponent()
            set strExtensionName to ocidWebURL's pathExtension() as text
            if strExtensionName is "" then
                log "拡張子がない"
                ocidFileName's stringByDeletingPathExtension()
                if strMIME contains "png" then
                    set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"png")
                else if strMIME contains "jpeg" then
                    set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"jpg")
                else if strMIME contains "webp" then
                    set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"webp")
                else if strMIME contains "gif" then
                    set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"gif")
                else if strMIME contains "svg" then
                    set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"svg")
                end if
            end if
            set ocidSaveFilePathURL to (ocidSaveImageDirURL's URLByAppendingPathComponent:ocidFileName)
            set ocidImageDataBase64 to (itemWebSubresourcesDict's objectForKey:"WebResourceData")
            set boolResults to (ocidImageDataBase64's writeToURL:ocidSaveFilePathURL atomically:true)
        end if
    else
        log "イメージ以外は処理しない"
    end if
    set ocidImageDataBase64 to ""
end repeat

set ocidWebSubresourcesArray to ""
set itemWebMainResourceDict to ""
set listReadPlistData to ""
set ocidPlistDict to ""






##############################################
## 書き出し先をFinderで開く
##############################################
ocidShardWorkspace's openURL:ocidSaveImageDirURL
















############################
####英語書式
############################
to doGetDateNo(strDateFormat)
    ####日付情報の取得
    set ocidDate to refMe's NSDate's |date|()
    ###日付のフォーマットを定義
    set ocidNSDateFormatter to refMe's NSDateFormatter's alloc()'s init()
    ocidNSDateFormatter's setLocale:(refMe's NSLocale's localeWithLocaleIdentifier:"en_US")
    ocidNSDateFormatter's setDateFormat:strDateFormat
    set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate
    set strDateAndTime to ocidDateAndTime as text
    return strDateAndTime
end doGetDateNo

|

[com.apple.webarchive]webarchiveから画像データを取り出す

webarchiveから画像データーを書き出.scpt

AppleScript サンプルコード

【スクリプトエディタで開く】 |

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004webarchiveから画像データーを書き出します
005v1.1 ちょっと修正版
006v1.2 ダイアログ呼び出しを変更
007v1.3 missing value対策を入れた
008com.cocolog-nifty.quicktimer.icefloe *)
009----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
010
011use AppleScript version "2.8"
012use framework "Foundation"
013use framework "AppKit"
014use scripting additions
015
016
017property refMe : a reference to current application
018
019
020set appFileManager to refMe's NSFileManager's defaultManager()
021set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()
022
023
024###ダイアログを前面に出す
025set strName to (name of current application) as text
026if strName is "osascript" then
027  tell application "SystemUIServer" to activate
028else
029  tell current application to activate
030end if
031
032
033####ダイアログで使うデフォルトロケーション
034set ocidGetUrlArray to appFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask)
035set ocidDesktopDirPathURL to ocidGetUrlArray's firstObject()
036set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
037
038####UTIリスト com.apple.webarchiveかplist
039set listUTI to {"com.apple.webarchive", "com.apple.property-list"} as list
040
041
042####ダイアログを出す
043tell 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
046end tell
047
048set strFilePath to (POSIX path of aliasFilePath) as text
049####ドキュメントのパスをNSString
050set ocidFilePath to refMe's NSString's stringWithString:(strFilePath)
051set ocidFilePath to ocidFilePath's stringByStandardizingPath()
052set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false)
053###選んだファイルのディレクトリ
054set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent()
055
056##############################################
057## 本処理
058##############################################
059set ocidOption to (refMe's NSDataWritingAtomic)
060###PLIST ROOT
061set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s init()
062set listReadPlistData to refMe's NSMutableDictionary's dictionaryWithContentsOfURL:(ocidFilePathURL) |error| :(reference)
063set ocidPlistDict to item 1 of listReadPlistData
064##############################################
065## 本処理   WebMainResource
066##############################################
067###ダウンロードしたURLのホスト名を保存先フォルダ名にする
068set ocidWebMainResourceDict to ocidPlistDict's objectForKey:"WebMainResource"
069set strMainURLString to (ocidWebMainResourceDict's valueForKey:("WebResourceURL")) as text
070set ocidURLString to refMe's NSString's stringWithString:(strMainURLString)
071set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLString)
072set strHostName to ocidURL's |host|() as text
073if strHostName ends with ".app" then
074  set strHostName to (strHostName & "_Folder") as text
075end if
076set strHostName to (strHostName & "/Images") as text
077###イメージ保存先
078set ocidSaveFileDirURL to (ocidContainerDirURL's URLByAppendingPathComponent:(strHostName))
079###保存先ディレクトリを作成
080set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
081ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions)
082appFileManager's createDirectoryAtURL:(ocidSaveFileDirURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error| :(reference)
083
084##############################################
085## 本処理   WebSubresources
086##############################################
087###WebSubresources
088set ocidWebSubresourcesArray to refMe's NSMutableArray's alloc()'s init()
089set ocidWebSubresourcesArray to ocidPlistDict's objectForKey:("WebSubresources")
090###インラインイメージ用のカウント
091set numInlineNO to 1 as integer
092repeat 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 ""
206end repeat
207
208
209##############################################
210## 本処理   WebSubframeArchives
211##############################################
212set ocidWebSubframeArray to refMe's NSMutableArray's alloc()'s init()
213###WebSubframe
214set ocidWebSubframeArray to ocidPlistDict's objectForKey:("WebSubframeArchives")
215if ocidWebSubframeArray = (missing value) then
216  appShardWorkspace's openURL:(ocidSaveFileDirURL)
217  tell application "SystemUIServer" to quit
218  return
219end if
220###インラインイメージ用のカウント
221set numInlineNO to 1 as integer
222repeat 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
347end repeat
348
349##############################################
350## 書き出し先をFinderで開く
351##############################################
352appShardWorkspace's openURL:(ocidSaveFileDirURL)
353
354tell application "SystemUIServer" to quit
355
356return
357
358
359
360
361
AppleScriptで生成しました

【スクリプトエディタで開く】 |

webarchiveから画像データーを書き出.scpt
ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004webarchiveから画像データーを書き出します
005v1.1 ちょっと修正版
006com.cocolog-nifty.quicktimer.icefloe *)
007----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
008
009use AppleScript version "2.8"
010use framework "Foundation"
011use framework "AppKit"
012use scripting additions
013
014
015property refMe : a reference to current application
016
017
018set objFileManager to refMe's NSFileManager's defaultManager()
019set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()
020
021
022####ダイアログで使うデフォルトロケーション
023set ocidGetUrlArray to (objFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask))
024set ocidDesktopDirPathURL to ocidGetUrlArray's objectAtIndex:0
025set aliasDefaultLocation to ocidDesktopDirPathURL as alias
026
027####UTIリスト com.apple.webarchiveplist
028set listUTI to {"com.apple.webarchive", "com.apple.property-list"}
029
030
031####ダイアログを出す
032set 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
034set strFilePath to (POSIX path of aliasFilePath) as text
035####ドキュメントのパスをNSString
036set ocidFilePath to refMe's NSString's stringWithString:(strFilePath)
037set ocidFilePath to ocidFilePath's stringByStandardizingPath
038set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false
039###選んだファイルのディレクトリ
040set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent
041
042##############################################
043## 本処理
044##############################################
045###PLIST ROOT
046set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
047set listReadPlistData to refMe's NSMutableDictionary's dictionaryWithContentsOfURL:(ocidFilePathURL) |error|:(reference)
048set ocidPlistDict to item 1 of listReadPlistData
049##############################################
050## 本処理   WebMainResource
051##############################################
052###ダウンロードしたURLのホスト名を保存先フォルダ名にする
053set ocidWebMainResourceDict to ocidPlistDict's objectForKey:"WebMainResource"
054set ocidMainURLString to ocidWebMainResourceDict's valueForKey:("WebResourceURL")
055set ocidURLString to refMe's NSString's stringWithString:(ocidMainURLString)
056set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidMainURLString)
057set strHostName to ocidURL's |host|() as text
058set strHostName to (strHostName & "/Images")
059###イメージ保存先
060set ocidSaveFileDirURL to (ocidContainerDirURL's URLByAppendingPathComponent:(strHostName))
061###保存先ディレクトリを作成
062set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
063ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions)
064objFileManager's createDirectoryAtURL:ocidSaveFileDirURL withIntermediateDirectories:true attributes:ocidAttrDict |error|:(reference)
065
066##############################################
067## 本処理   WebSubresources
068##############################################
069###WebSubresources
070set ocidWebSubresourcesArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
071set ocidWebSubresourcesArray to ocidPlistDict's objectForKey:"WebSubresources"
072###インラインイメージ用のカウント
073set numInlineNO to 1 as text
074repeat 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 ""
188end repeat
189##############################################
190## 本処理   WebSubframeArchives
191##############################################
192
193###WebSubframe
194set ocidWebSubframeArray to ocidPlistDict's objectForKey:"WebSubframeArchives"
195###インラインイメージ用のカウント
196set numInlineNO to 1 as text
197repeat 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   
321end repeat
322
323##############################################
324## 書き出し先をFinderで開く
325##############################################
326appShardWorkspace's openURL:ocidSaveFileDirURL
327
328
329
330return
331
332
333
334
335
AppleScriptで生成しました

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
#             com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSURL : a reference to refMe's NSURL
property refNSWorkspace : a reference to refMe's NSWorkspace

set objFileManager to refMe's NSFileManager's defaultManager()
set ocidShardWorkspace to refNSWorkspace's sharedWorkspace()


####ダイアログで使うデフォルトロケーション
set ocidGetUrlArray to (objFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopDirPathURL to ocidGetUrlArray's objectAtIndex:0
set aliasDefaultLocation to ocidDesktopDirPathURL as alias

####UTIリスト com.apple.webarchiveplist
set listUTI to {"com.apple.webarchive", "com.apple.property-list"}


####ダイアログを出す
set aliasFilePath to (choose file with prompt "ファイルを選んでください" default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias

set strFilePath to POSIX path of aliasFilePath
####ドキュメントのパスをNSString
set ocidFilePath to refNSString's stringWithString:strFilePath
set ocidFilePath to ocidFilePath's stringByStandardizingPath
set ocidFilePathURL to refNSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false
###選んだファイルのディレクトリ
set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent
###イメージ保存先
set ocidSaveFileDirURL to (ocidContainerDirURL's URLByAppendingPathComponent:"images")
###保存先ディレクトリを作成
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions)
objFileManager's createDirectoryAtURL:ocidSaveFileDirURL withIntermediateDirectories:true attributes:ocidAttrDict |error|:(reference)


##############################################
## 本処理
##############################################
###PLIST ROOT
set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
set listReadPlistData to refMe's NSMutableDictionary's dictionaryWithContentsOfURL:ocidFilePathURL |error|:(reference)
set ocidPlistDict to item 1 of listReadPlistData


##############################################
## 本処理   WebSubresources
##############################################
###WebSubresources
set ocidWebSubresourcesArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
set ocidWebSubresourcesArray to ocidPlistDict's objectForKey:"WebSubresources"
###インラインイメージ用のカウント
set numInlineNO to 1 as text
repeat with itemWebSubresourcesDict in ocidWebSubresourcesArray
    set strMIME to (itemWebSubresourcesDict's objectForKey:"WebResourceMIMEType") as text
    if strMIME contains "image" then
        set ocidWebPath to (itemWebSubresourcesDict's objectForKey:"WebResourceURL")
        set strWebPath to ocidWebPath as text
        #####
        if strWebPath contains "data:image" then
            #####インラインSVGを保存するやっつけ感満載の処理
            
            if strWebPath contains "data:image/svg+xml;charset=utf-8," then
                set strFileName to "svg-" & numInlineNO & ".svg" as text
                set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName)
                
                set AppleScript's text item delimiters to "data:image/svg+xml;charset=utf-8,"
                set listDelim to every text item of strWebPath
                set AppleScript's text item delimiters to ""
                set strInLineContents to item 2 of listDelim
                set ocidInlineContents to (refNSString's stringWithString:strInLineContents)
                set ocidDencodedContents to (ocidInlineContents's stringByRemovingPercentEncoding)
                set boolDone to (ocidDencodedContents's writeToURL:ocidSaveFilePathURL atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))
                
            else if strWebPath contains "data:image/svg+xml;charset=utf-8," then
                set strFileName to "svg-" & numInlineNO & ".svg" as text
                set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName)
                
                set AppleScript's text item delimiters to "data:image/svg+xml;charset=utf-8,"
                set listDelim to every text item of strWebPath
                set AppleScript's text item delimiters to ""
                set strInLineContents to item 2 of listDelim
                set ocidInlineContents to (refNSString's stringWithString:strInLineContents)
                set ocidDencodedContents to (ocidInlineContents's stringByRemovingPercentEncoding)
                set boolDone to (ocidDencodedContents's writeToURL:ocidSaveFilePathURL atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))
                
            else if strWebPath contains "data:image/svg+xml;base64," then
                set strFileName to "png-" & numInlineNO & ".svg" as text
                set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName)
                
                set AppleScript's text item delimiters to "data:image/svg+xml;base64,"
                set listDelim to every text item of strWebPath
                set AppleScript's text item delimiters to ""
                set strInLineContents to item 2 of listDelim
                set ocidInlineContents to (refNSString's stringWithString:strInLineContents)
                set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:ocidInlineContents options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters))
                set boolResults to (ocidData's writeToURL:ocidSaveFilePathURL atomically:true)
                
                
            else if strWebPath contains "data:image/png;base64," then
                set strFileName to "png-" & numInlineNO & ".png" as text
                set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:strFileName)
                
                set AppleScript's text item delimiters to "data:image/png;base64,"
                set listDelim to every text item of strWebPath
                set AppleScript's text item delimiters to ""
                set strInLineContents to item 2 of listDelim
                set ocidInlineContents to (refNSString's stringWithString:strInLineContents)
                set ocidData to (refMe's NSData's alloc()'s initWithBase64EncodedString:ocidInlineContents options:(refMe's NSDataBase64DecodingIgnoreUnknownCharacters))
                set boolResults to (ocidData's writeToURL:ocidSaveFilePathURL atomically:true)
            else
                log "インラインイメージはとりあえず処理しない"
            end if
            set numInlineNO to numInlineNO + 1 as text
        else
            set ocidWebURL to (refNSURL's alloc()'s initWithString:ocidWebPath)
            set ocidFileName to ocidWebURL's lastPathComponent()
            set strExtensionName to ocidWebURL's pathExtension() as text
            if strExtensionName is "" then
                log "拡張子がない"
                ocidFileName's stringByDeletingPathExtension()
                if strMIME contains "png" then
                    set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"png")
                else if strMIME contains "jpeg" then
                    set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"jpg")
                else if strMIME contains "webp" then
                    set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"webp")
                else if strMIME contains "gif" then
                    set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"gif")
                else if strMIME contains "svg" then
                    set ocidFileName to (ocidFileName's stringByAppendingPathExtension:"svg")
                end if
            end if
            set ocidSaveFilePathURL to (ocidSaveFileDirURL's URLByAppendingPathComponent:ocidFileName)
            set ocidImageDataBase64 to (itemWebSubresourcesDict's objectForKey:"WebResourceData")
            set boolResults to (ocidImageDataBase64's writeToURL:ocidSaveFilePathURL atomically:true)
        end if
    else
        log "イメージ以外は処理しない"
    end if
    set ocidImageDataBase64 to ""
end repeat

set ocidWebSubresourcesArray to ""
set itemWebMainResourceDict to ""
set listReadPlistData to ""
set ocidPlistDict to ""
##############################################
## 書き出し先をFinderで開く
##############################################
ocidShardWorkspace's openURL:ocidSaveFileDirURL



return





|

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat 2024 Acrobat AddOn Acrobat Annotation Acrobat AppleScript Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat GentechAI Acrobat JS Acrobat JS Word Search Acrobat Maintenance Acrobat Manifest Acrobat Menu Acrobat Merge Acrobat Open Acrobat PDFPage Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat Print Acrobat Python Acrobat Reader Acrobat Reader Localized Acrobat Reference Acrobat Registered Products Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat URL List Mac Acrobat URL List Windows Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin ConfigCode Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin NetWork Admin Permission Admin Pkg Admin Power Management Admin Printer Admin Printer Basic Admin Printer Custompapers Admin SetUp Admin SMB Admin softwareupdate Admin Support Admin System Information Admin TCC Admin Tools Admin Umask Admin Users Admin Volumes Admin XProtect Adobe Adobe AUSST Adobe Bridge Adobe Documents Adobe FDKO Adobe Fonts Adobe Reference Adobe RemoteUpdateManager Adobe Sap Code AppKit Apple AppleScript AppleScript Duplicate AppleScript entire contents AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Video Applications AppStore Archive Archive Keka Attributes Automator BackUp Barcode Barcode Decode Barcode QR Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome Chromedriver CIImage CityCode CloudStorage Color Color NSColor Color NSColorList com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Desktop Position Device Diff Disk do shell script Dock Dock Launchpad DropBox Droplet eMail Encode % Encode Decode Encode HTML Entity Encode UTF8 Error EXIFData exiftool ffmpeg File File Name Finder Finder Window Firefox Folder FolderAction Font List FontCollections Fonts Fonts Asset_Font Fonts ATS Fonts Emoji Fonts Maintenance Fonts Morisawa Fonts Python Fonts Variable Foxit GIF github Guide HTML Icon Icon Assets.car Illustrator Image Events ImageOptim Input Dictionary iPhone iWork Javascript Jedit Ω Json Label Language Link locationd lsappinfo m3u8 Mail Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Microsoft Fonts Microsoft Office Microsoft Office Link Microsoft OneDrive Microsoft Teams Mouse Music Node Notes NSArray NSArray Sort NSAttributedString NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSGraphicsContext Crop NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMetadataItem NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSet NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSUbiquitous NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth PDF PDF Image2PDF PDF MakePDF PDF nUP PDF Pymupdf PDF Pypdf PDFContext PDFDisplayBox PDFImageRep PDFKit PDFKit Annotation PDFKit AnnotationWidget PDFKit DocumentPermissions PDFKit OCR PDFKit Outline PDFKit Start PDFPage PDFPage Rotation PDFView perl Photoshop PlistBuddy pluginkit plutil postalcode PostScript PowerShell prefPane Preview Python Python eyed3 Python pip QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver Script Editor Script Menu SF Symbols character id SF Symbols Entity Shortcuts Shortcuts Events sips Skype Slack Sound Spotlight sqlite StandardAdditions StationSearch Subtitles LRC Subtitles SRT Subtitles VTT Swift swiftDialog System Events System Settings TemporaryItems Terminal Text Text CSV Text MD Text TSV TextEdit Tools Translate Trash Twitter Typography UI Unit Conversion UTType valueForKeyPath Video VisionKit Visual Studio Code VMware Fusion Wacom Weather webarchive webp Wifi Windows XML XML EPUB XML HTML XML LSSharedFileList XML LSSharedFileList sfl2 XML LSSharedFileList sfl3 XML objectsForXQuery XML OPML XML Plist XML Plist System Events XML RSS XML savedSearch XML SVG XML TTML XML webloc XML xmllint XML XMP YouTube Zero Padding zoom