« [com.apple.webarchive]webarchiveから画像データを取り出す | トップページ | [NSSize] NSMakeSize »

[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から画像データを取り出す | トップページ | [NSSize] NSMakeSize »

Safari」カテゴリの記事

webarchive」カテゴリの記事