AppleScript 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





|

その他のカテゴリー

Acrobat Acrobat Annotation Acrobat BookMark Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat JS Acrobat Open Acrobat Plugin Acrobat Preflight Acrobat python Acrobat Reader Acrobat SCA Acrobat Sequ Acrobat Sign Acrobat Stamps Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin System Information Admin Tools Admin Users Admin Volumes Adobe Adobe RemoteUpdateManager Apple AppleScript AppleScript Accessibility AppleScript AppKit AppleScript Applications AppleScript AppStore AppleScript Archive AppleScript Attributes AppleScript Audio AppleScript Automator AppleScript AVAsset AppleScript AVconvert AppleScript AVFoundation AppleScript AVURLAsset AppleScript BackUp AppleScript Barcode AppleScript Bash AppleScript Basic AppleScript Basic Path AppleScript Bluetooth AppleScript BOX AppleScript Browser AppleScript Calendar AppleScript CD/DVD AppleScript Choose AppleScript Chrome AppleScript CIImage AppleScript CloudStorage AppleScript Color AppleScript com.apple.LaunchServices.OpenWith AppleScript Console AppleScript Contacts AppleScript CotEditor AppleScript CURL AppleScript current application AppleScript Date&Time AppleScript delimiters AppleScript Desktop AppleScript Device AppleScript Disk AppleScript do shell script AppleScript Dock AppleScript DropBox AppleScript eMail AppleScript Encode Decode AppleScript Error AppleScript EXIFData AppleScript ffmpeg AppleScript File AppleScript Finder AppleScript Firefox AppleScript Folder AppleScript Fonts AppleScript GIF AppleScript Guide AppleScript HTML AppleScript Icon AppleScript Illustrator AppleScript Image Events AppleScript Image2PDF AppleScript ImageOptim Applescript iWork AppleScript Javascript AppleScript Jedit AppleScript Json AppleScript Label AppleScript List AppleScript locationd AppleScript LRC AppleScript LSSharedFileList AppleScript m3u8 AppleScript Mail AppleScript MakePDF AppleScript Map AppleScript Math AppleScript Microsoft AppleScript Microsoft Edge AppleScript Microsoft Excel AppleScript Mobileconfig AppleScript Mouse AppleScript Movie AppleScript Music AppleScript NetWork AppleScript Notes AppleScript NSArray AppleScript NSBitmapImageRep AppleScript NSBundle AppleScript NSCFBoolean AppleScript NSCharacterSet AppleScript NSColor AppleScript NSColorList AppleScript NSData AppleScript NSDictionary AppleScript NSError AppleScript NSEvent AppleScript NSFileAttributes AppleScript NSFileManager AppleScript NSFileManager enumeratorAtURL AppleScript NSFont AppleScript NSFontManager AppleScript NSImage AppleScript NSIndex AppleScript NSKeyedArchiver AppleScript NSKeyedUnarchiver AppleScript NSLocale AppleScript NSMutableArray AppleScript NSMutableDictionary AppleScript NSMutableString AppleScript NSNotFound AppleScript NSNumber AppleScript NSOpenPanel AppleScript NSPasteboard AppleScript NSPDFImageRep AppleScript NSpoint AppleScript NSPredicate AppleScript NSRange AppleScript NSRegularExpression AppleScript NSRunningApplication AppleScript NSScreen AppleScript NSSize AppleScript NSString AppleScript NSStringCompareOptions AppleScript NSTask AppleScript NSTimeZone AppleScript NSURL AppleScript NSURL File AppleScript NSURLBookmark AppleScript NSURLComponents AppleScript NSURLResourceKey AppleScript NSUserDefaults AppleScript NSUUID AppleScript NSView AppleScript NSWorkspace AppleScript Numbers AppleScript OAuth AppleScript ObjC AppleScript OneDrive AppleScript Osax AppleScript PDF AppleScript PDFAnnotationWidget AppleScript PDFContext AppleScript PDFDisplayBox AppleScript PDFDocumentPermissions AppleScript PDFKit AppleScript PDFnUP AppleScript PDFOutline AppleScript Photos AppleScript Photoshop AppleScript PostScript AppleScript prefPane AppleScript Preview AppleScript Python AppleScript QR AppleScript QR Decode AppleScript QuickLook AppleScript QuickTime AppleScript record AppleScript Regular Expression AppleScript Reminders AppleScript ReName AppleScript Repeat AppleScript RTF AppleScript Safari AppleScript SaveFile AppleScript ScreenCapture AppleScript ScreenSaver AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript Sort AppleScript Sound AppleScript Spotlight AppleScript SRT AppleScript StandardAdditions AppleScript stringByApplyingTransform AppleScript System Events AppleScript System Events Plist AppleScript System Settings AppleScript TemporaryItems AppleScript Terminal AppleScript Text AppleScript Text CSV AppleScript Text TSV AppleScript TextEdit AppleScript Translate AppleScript Trash AppleScript Twitter AppleScript UI AppleScript Unit Conversion AppleScript UTType AppleScript valueForKeyPath AppleScript Video AppleScript VisionKit AppleScript Visual Studio Code AppleScript webarchive AppleScript webp AppleScript Wifi AppleScript XML AppleScript XML EPUB AppleScript XML OPML AppleScript XML Plist AppleScript XML RSS AppleScript XML savedSearch AppleScript XML SVG AppleScript XML TTML AppleScript XML webloc AppleScript XMP AppleScript YouTube AppleScript Zero Suppress Applications CityCode Design github iPhone List Logs lsappinfo Memo Music perl PlistBuddy pluginkit postalcode ReadMe SF Symbols character id SF Symbols Entity sips Skype Slack sqlite TCC Tools Typography Video Wacom zoom