I have an AppleScript that I'm trying to convert to JXA.
PURPOSE: Get HTML Source of Web Page
The AppleScript runs fine, but the JXA script fails with this error:
"TypeError: Object is not a function"
I'm running Script Editor 2.8.1 (183.1) on macOS 10.11.6.
I've studied this ref (and others), but it doesn't help me: initWithData:encoding:
Any help/suggestions/references will be greatly appreciated.
Here are my scripts:
//---- PURPOSE: GET HTML of Web Page ----
/*
---- AppleScript VERSION ----
-- (runs fine in Script Editor with AppleScript as Language)
use framework "Foundation"
use scripting additions
set urlStr to "https://stackoverflow.com/"
set theURL to current application's class "NSURL"'s URLWithString:urlStr
set theData to current application's NSData's dataWithContentsOfURL:theURL
set theString to current application's NSString's alloc()'s initWithData:theData encoding:(current application's NSUTF8StringEncoding)
set theString to theString as text
*/
//--- JXA VERSION ---
var urlStr = "https://stackoverflow.com/";
var theURL = $.NSURL.URLWithString(urlStr);
var theData = $.NSData.dataWithContentsOfURL(theURL);
//-- NEXT statement FAILS WITH ERROR: "TypeError: Object is not a function"
//--- AppleScript Code I'm trying to Convert ---
//current application's NSString's alloc()'s initWithData:theData encoding:(current application's NSUTF8StringEncoding)
var theString = $.NSString.alloc().initWithData(theData,
encoding($.NSUTF8StringEncoding)
);
var theString = ObjC.unwrap(theString);