• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Save as web archive if text found
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Save as web archive if text found


  • Subject: Re: Save as web archive if text found
  • From: Shane Stanley <email@hidden>
  • Date: Wed, 13 Nov 2013 17:43:32 +1100

On 13 Nov 2013, at 1:35 PM, Shane Stanley <email@hidden> wrote:

Good luck with a GUI script for that -- I wouldn't hold my breath.

If you're on Mavericks, you can bypass Safari altogether.

it seems a bit rude to tease like that, and it might be more useful generally. So here's a script to download a Web page as a .webarchive in Mavericks without using Safari. 

You need to save the script as a script bundle in ~/Library/Script Libraries/, and call it something like Make webarchive.scptd. If you're using AppleScript Editor, you then need to  click the Bundle Contents button at the top of the window, click the AppleScript/Objective C Library checkbox, and save again. If you're using Script Debugger, you need version 5.0.7, and you make the initial file by choosing New Script from Template and clicking the button there. So...

use framework "Foundation"
use framework "WebKit"
property theSender : missing value
property thePath : missing value
property theSearchString : missing value

on archivePage:thePageURL toPath:aPath ifItContains:searchString sender:sender
set my theSender to sender -- store main script so we can call back
set my thePath to aPath -- store path for use later
set my theSearchString to searchString -- store for use later
-- 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 call delegate methods on me
theView's setFrameLoadDelegate:me
-- load the page
theView's setMainFrameURL:thePageURL
end archivePage:toPath:ifItContains:sender:

-- called when our WebView loads a frame
on WebView:aWebView didFinishLoadForFrame:webFrame
-- the main frame is our interest
if webFrame = aWebView's mainFrame() then
-- get the text of the page
set theText to (webFrame's DOMDocument()'s documentElement()'s outerText())
-- search it
if (theText's rangeOfString:theSearchString)'s |length|() > 0 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
-- tell our script it's all done
theSender's jobDone:"The webarchive was saved"
else
theSender's jobDone:"Seach string not found"
end if
end if
end WebView:didFinishLoadForFrame:

-- called when there's a problem
on WebView:WebView didFailLoadWithError:theError forFrame:webFrame
-- got an error, bail
WebView's stopLoading:me
theSender's jobDone:"The webarchive was not saved"
end WebView:didFailLoadWithError:forFrame:

To use the library, you need a script like this:

use theLib : script "Make webarchive" 
use scripting additions

set thePath to (POSIX path of (path to desktop)) & "Test.webarchive"
theLib's archivePage:"http://www.macosxautomation.com/applescript/apps/" toPath:thePath ifItContains:"Mavericks" sender:me

-- called when the job's done
on jobDone:theMessage
display notification theMessage sound name "Ping"
end jobDone:

And when you save it as an applet, you need to make it stay-open. You can't run the script above directly from AppleScript Editor because it runs scripts in the background -- you get:

error "-[WebView initWithFrame:frameName:groupName:] was called from a secondary thread" number -10000

But the saved app works fine (and of course you can run it in ASAObjC Explorer for Mavericks using Log & Run to force it to run on the main thread).

-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Save as web archive if text found
      • From: Bryan Harris <email@hidden>
References: 
 >Save as web archive if text found (From: Bryan Harris <email@hidden>)
 >Re: Save as web archive if text found (From: Shane Stanley <email@hidden>)

  • Prev by Date: Re: Mavericks: Long lag making new events in Calendar app
  • Next by Date: Re: Mavericks: Long lag making new events in Calendar app
  • Previous by thread: Re: Save as web archive if text found
  • Next by thread: Re: Save as web archive if text found
  • Index(es):
    • Date
    • Thread