« [BASE64]AppleScriptでBASE64デコードとBASE64エンコード | トップページ | [webarchive]Safariでwebarchiveを保存して画像データを取り出す »

[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





|

« [BASE64]AppleScriptでBASE64デコードとBASE64エンコード | トップページ | [webarchive]Safariでwebarchiveを保存して画像データを取り出す »

Safari」カテゴリの記事

webarchive」カテゴリの記事