Hi folks,
Long time developer (20+ years), but unfamiliar with AppleScript.
My ultimate goal is to scrape the source from a web page and save it in an html file. The reason I’m using AppleScript is that the file needs to be processed by a JavaScript engine. Here’s my initial attempt at it.
tell application "WebKit"
activate
open location "http://google.com/"
set page_source to source of front document as string
delay 5
end tell
tell application "BBEdit"
activate
set E to make new document
tell E
set its text to (page_source)
save E to "/Users/mrl/temp/123.html"
close E
end tell
delay 5
end tell
I’m using BBEdit because it is supposed to have decent AppleScript support, but any mechanism to save the file to disk is acceptable. The essential flow is:
- open web page in a browser
- ‘View source’
- save source to disk
- Lather, rinse, repeat
One issue I’m having is that often (seemingly randomly) the BBEdit buffer is blank, thus an empty html file is generated.
Any thoughts, suggestions, tips, fixes, refactoring, etc, are much appreciated. Bonus points for getting the URL and filename.html from an plain text input file.