I wrote a script to test downloading text from a website and I thought some people might be interested in seeing the script. Given a URL, the script downloads whatever the URL points to. However I only implemented enough code to decode text that is downloaded. For my purposes I only needed to verify it could be downloaded. But if you want to see the text for a web page this can do it. The size for the CNN.com page was 155,648 bytes when I tried it last night. So if you run it in the debugger ScriptDebugger will truncate most of the text displayed in the debugger, but the full text will be in the file.
I added a .html to the file name so if the downloaded datafile is double-clicked something will come up in the web browser. For the CNN example the text shows up in the web browser but not the images.
I added some comments and also added an example of how to get the MIME type, TextEncoding and the URL.
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
set TheURLStr to "https://www.cnn.com"
set TheURL to (current application's |NSURL|'s URLWithString:TheURLStr)
-- Creates and returns an initialized URL request with the specified value.
-- NSURLRequestReloadIgnoringLocalCacheData Causes data for the URL to be loaded from the originating source. No existing cache data would be used.
set TheRequest to current application's NSURLRequest's requestWithURL:TheURL cachePolicy:(current application's NSURLRequestReloadIgnoringLocalCacheData) timeoutInterval:10
-- sendSynchronousRequest:returningResponse:error: performs a synchronous download of the specified URL request.
-- It returns "missing value" if a connection could not be created or an error occurs.
-- Item 1 of the returned list contains the requested data & item 2 contains the status of request and other information about the data downloaded
set TheResult to current application's NSURLConnection's sendSynchronousRequest:TheRequest returningResponse:(reference) |error|:(missing value)
-- Returns the MIME type of the downloaded data
set MIMEType to MIMEType of item 2 of TheResult --> (NSString) "text/html"
-- Returns the data encoding of the returned data
set TextEncoding to textEncodingName of item 2 of TheResult --> (NSString) "utf-8"
-- Returns the URL the data was downloaded from
set URLValue to |URL| of item 2 of TheResult --> (NSURL) https://www.cnn.com/
set TheDownloadedData to item 1 of TheResult --> too big to show actual NSData in script
set HTTPURLResponse to item 2 of TheResult -->
(* <NSHTTPURLResponse: 0x608000837da0> { URL: https://www.cnn.com/ } { status code: 200, headers {
"Accept-Ranges" = bytes;
"Access-Control-Allow-Origin" = "*";
Age = 69;
"Cache-Control" = "max-age=60";
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Length" = 33977;
"Content-Type" = "text/html; charset=utf-8";
Date = "Mon, 15 Jan 2018 21:10:41 GMT";
"Fastly-Debug-Digest" = 46be59e687681f2cbdc5286ab50024ed035dc360065b1aec7ce355bf418daeb9;
"Set-Cookie" = "countryCode=US; Domain=.cnn.com; Path=/, geoData=surfside|CA|90743|US|NA; Domain=.cnn.com; Path=/";
Vary = "Accept-Encoding, Fastly-SSL, Fastly-SSL";
Via = "1.1 varnish, 1.1 varnish";
"X-Cache" = "HIT, HIT";
"X-Cache-Hits" = "2, 1";
"X-Served-By" = "cache-iad2143-IAD, cache-lax8634-LAX";
"X-Timer" = "S1516050641.061185,VS0,VE1";
"content-security-policy" = "default-src 'self' blob: https://*.cnn.com:* http://*.cnn.com:* *.cnn.io:* *.cnn.net:* *.turner.com:* *.turner.io:* *.ugdturner.com:* courageousstudio.com *.vgtf.net:*; script-src 'unsafe-eval' 'unsafe-inline' 'self' *; style-src 'unsafe-inline' 'self' blob: *; child-src 'self' blob: *; frame-src 'self' *; object-src 'self' *; img-src 'self' data: blob: *; media-src 'self' data: blob: *; font-src 'self' data: *; connect-src 'self' *; frame-ancestors 'self' *.cnn.com:* *.turner.com:* courageousstudio.com;";
"x-content-type-options" = nosniff;
"x-servedByHost" = "::ffff:172.17.71.21";
"x-xss-protection" = "1; mode=block";
} }
*)
-- NSString can be used to decode NSData data to "utf-8" text
set TheText to current application's NSString's alloc()'s initWithData:(TheDownloadedData) encoding:(current application's NSUTF8StringEncoding) --> too big to show actual NSData in script
-- Use NSString to write the text to the disk
set FileName to current application's NSString's stringWithString:"downloaded text.html"
set HomeDirPath to current application's NSHomeDirectory() -- Get the path to the folder
-- Get the path to write the text to
set DesktopPath to HomeDirPath's stringByAppendingPathComponent:"Desktop" -- Get the path to the folder
set ThePath to DesktopPath's stringByAppendingPathComponent:FileName -- Get the path to the file
-- If Sucessful = true the write did not get any errors, otherwise an error occured
set Sucessful to TheText's writeToFile:ThePath atomically:no encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)
Bill