Tuesday, September 29, 2020

#1 2020-09-15 10:58:04 am

Registered: 2019-10-23
Posts: 399

Webarchive of document 1 of Safari saved to desktop.

I updated the WebArchive ASOC Script.

Workflow: Make a webarchive of document 1 of Safari and save it to Desktop.
The title of the file is the name of the document of Safari.

I have include a handler that use offset to get correct name string but it could be
some issues if the webarchive is saved somewhere else.
I have test it on macscripter.net.

Thanks to Shane Stanley.


   It needs to be run either from Script Editor, or as a stay-open applet
       (in which case you can add code to quit after the dialogs).
           The stay-open issue means it won't run in Script Debugger as yet.
   Shane Stanley <sstanley@myriad-com.com.au>

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "WebKit"

property thePath : missing value

-- Tell Safari to get the name and URL of document 1
tell application "Safari"
   tell document 1
       set theName to its name
       set theURL to its URL
   end tell
end tell

-- Correct the name string.
set theName to my stringOffsetBeginOrEnd:theName withOffset:"/" withOption:"end"

-- Output path of the file.
set thePath to POSIX path of (path to desktop) & theName & "webarchive"

-- Run the handler.
my archivePage:theURL toPath:thePath

on archivePage:thePageURL toPath:aPath
   set my thePath to aPath -- store path for use later
   my performSelectorOnMainThread:"setUpWebViewForPage:" withObject:thePageURL waitUntilDone:false
end archivePage:toPath:

on setUpWebViewForPage:thePageURL
   -- needs to be done on the main thread
   -- make a WebView
   set theView to current application's WebView's alloc()'s initWithFrame:{origin:{x:0, y:0}, |size|:{width:100, height:100}}
   -- tell it to call delegate methods on me
   theView's setFrameLoadDelegate:me
   -- load the page
   theView's setMainFrameURL:thePageURL
end setUpWebViewForPage:

-- called when the WebView loads a frame
on WebView:aWebView didFinishLoadForFrame:webFrame
   -- the main frame is our interest
   if webFrame = aWebView's mainFrame() then
       -- get the data and write it to file
       set theArchiveData to webFrame's dataSource()'s webArchive()'s |data|()
       theArchiveData's writeToFile:thePath atomically:true
       display dialog "The webarchive was saved" buttons {"OK"} default button 1
   end if
end WebView:didFinishLoadForFrame:

on WebView:WebView didFailLoadWithError:theError forFrame:webFrame
   -- got an error, bail
   WebView's stopLoading:me
   display dialog "The webarchive was not saved" buttons {"OK"} default button 1
end WebView:didFailLoadWithError:forFrame:

on stringOffsetBeginOrEnd:theString withOffset:theChar withOption:theOption
   set aString to theString
   set x to the offset of theChar in aString
   if (theOption = "begin") then
       return (text from x to 1 of aString)
   else if (theOption = "end") then
       return (text from (x + 2) to -1 of aString)
       error number -128
   end if
end stringOffsetBeginOrEnd:withOffset:withOption:

I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)