Friday, December 15, 2017

#1 2017-11-18 08:53:31 am

cudaboy_71
Member
Registered: 2014-03-04
Posts: 20

Need suggestions: how to save HTML source code without a browser

I have a working applescript that opens Safari and loads a page--and then does a bunch of other stuff with the results.

But, in the sake of a cleaner running UI I'd like to bypass the whole 'load in a browser' part. But, I'm stuck as to even a methodology or workflow to do so.

Are there any methods,or a known/common  way to build one to pass a URL/user agent to a web address using AS such that a stream of  HTML is returned and saved to a file--all without ever activating Safari, or any other browser?

Offline

 

#2 2017-11-19 11:28:17 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4454

Re: Need suggestions: how to save HTML source code without a browser

The choices are either a shell script with curl or AppleScriptObjC. The actual code will depend on what kind of data you want to download and what responses are likely to come from the server that a browser would handle automatically. But a simple script to download the HTML source of this very page to a file on your desktop might look like this using curl …

Applescript:

-- URL text split here to prevent MacScripter from inserting BBCode tags! Not necessary otherwise.
set aURL to "http" & "://macscripter.net/viewtopic.php?id=46012"
set POSIXPathToFile to POSIX path of (path to desktop) & "Downloaded Web Page.html"

do shell script ("curl " & quoted form of aURL & " > " & quoted form of POSIXPathToFile)

… or like this with ASOBjC:

Applescript:

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

-- URL text split here to prevent MacScripter from inserting BBCode tags! Not necessary otherwise.
set aURL to "http" & "://macscripter.net/viewtopic.php?id=46012"
set POSIXPathToFile to POSIX path of (path to desktop) & "Downloaded Web Page.html"

set anNSURL to current application's class "NSURL"'s URLWithString:(aURL)
set WebPageText to current application's class "NSString"'s stringWithContentsOfURL:(anNSURL) usedEncoding:(missing value) |error|:(missing value)
tell WebPageText to writeToFile:(POSIXPathToFile) atomically:(true) encoding:(|⌘|'s NSUTF8StringEncoding) |error|:(missing value)


NG

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)