webarchive

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

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を保存して画像データを取り出す

#!/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から画像データを取り出す

#!/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 AddOn Acrobat Annotation Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat JS Acrobat Manifest Acrobat Menu Acrobat Open Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat python Acrobat Reader Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin Support Admin System Information Admin Tools Admin Users Admin Volumes Adobe Adobe FDKO Adobe RemoteUpdateManager AppKit Apple AppleScript AppleScript do shell script AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript System Events AppleScript System Events Plist AppleScript Video Applications AppStore Archive Attributes Automator BackUp Barcode Barcode QR Barcode QR Decode Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome CIImage CityCode CloudStorage Color com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Device Diff Disk Dock DropBox Droplet eMail Encode % Encode Decode Encode UTF8 Error EXIFData ffmpeg File Finder Firefox Folder FolderAction Fonts GIF github Guide HTML HTML Entity Icon Illustrator Image Events Image2PDF ImageOptim iPhone iWork Javascript Jedit Json Label Leading Zero List locationd LRC lsappinfo LSSharedFileList m3u8 Mail MakePDF Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Mouse Music NetWork Notes NSArray NSArray Sort NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSColor NSColorList NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSPrintOperation NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth OneDrive PDF PDFAnnotation PDFAnnotationWidget PDFContext PDFDisplayBox PDFDocumentPermissions PDFImageRep PDFKit PDFnUP PDFOutline perl Photoshop PlistBuddy pluginkit postalcode PostScript prefPane Preview Python QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver SF Symbols character id SF Symbols Entity sips Skype Slack Sound Spotlight sqlite SRT StandardAdditions Swift System Settings TCC 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 webarchive webp Wifi Windows XML XML EPUB XML OPML XML Plist XML RSS XML savedSearch XML SVG XML TTML XML webloc XML XMP YouTube zoom