-1

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);
JMichaelTX
  • 1,659
  • 14
  • 19
  • To those who downvoted the question: If you really want to be helpful, post a comments stating why you downvoted, and how you would improve it. – JMichaelTX Aug 31 '17 at 22:27

2 Answers2

1

Thanks to Executing an Arbitrary JavaScript File, I was able to resolve my own question. The answer is:

var theString = $.NSString.alloc.initWithDataEncoding(theData, $.NSUTF8StringEncoding);

Here's the revised JXA script, which works:

//--- JXA VERSION ---

ObjC.import('Foundation');    // is this necessary?

var urlStr = "https://stackoverflow.com/";
var theURL = $.NSURL.URLWithString(urlStr);
var theData = $.NSData.dataWithContentsOfURL(theURL);

//--- THIS IS THE FIX ---
var theString = $.NSString.alloc.initWithDataEncoding(theData, $.NSUTF8StringEncoding);

var theString = ObjC.unwrap(theString);
theString.substring(1,100)

//--->"!DOCTYPE html>\r\n<html>\r\n\r\n<head>\r\n\r\n<title>Stack Overflow</title>\r\n    <link rel=\"shortcut icon\" hr"
JMichaelTX
  • 1,659
  • 14
  • 19
-2
var urlStr = "https://stackoverflow.com/";
var htmlStr = getHTMLSource(urlStr);
htmlStr.substring(0,200);
var nsURL     = $.NSURL.URLWithString(pURL);
var nsHTML     = $.NSData.dataWithContentsOfURL(nsURL);
var nsHTMLStr = $.NSString.alloc.initWithDataEncoding(nsHTML, $.NSUTF8StringEncoding);
var htmlStr   = ObjC.unwrap(nsHTMLStr);
return htmlStr; 
}

This should output the source code of StackOverflow

Samveen
  • 3,482
  • 35
  • 52
Ronan Leonard
  • 322
  • 3
  • 8