ExtendExtendScript/GetURL: now with a simple installer

If all you want to get is an easy-to-use GetURL function for InDesign ExtendScript, I now have a simple installer to install ExtendExtendScript with the GetURL plug-in.

Download it here: https://www.rorohiko.com/downloads/ExtendExtendScript.1.0.3.zip

Important: no warranties, neither expressed nor implied: use at your own risk.

You can find the source code and read more about ExtendExtendScript and how it works in some of my other posts: https://coppieters.nz/?p=133

If you’re not interested in how ExtendExtendScript works and all you want to do is access some URL from ExtendScript, this installer is for you.

The ExtendExtendScript extension is invisible. All it does is augment the InDesign ExtendScript environment with some new functions you can call.

It only works from a persistent ExtendScript engine; don’t run it from the ‘main’ engine, which is not persistent.

After installing this extension you can access a full-fledged HTTP/HTTPS downloader callable from ExtendScript, and you can write ExtendScript code like the following examples:

// Need to run in a persistent engine for callbacks to work
// Make sure to add JSInterface.jsx and json2.jsx

#targetengine TestSomewhere
#include "JSInterface.jsx"

function handleData(data) { 
 var fileURL = "~/Desktop/image.jpg";
 var file = new File(fileURL);
 file.encoding = "BINARY";
 file.open("w");
 file.write(data);
 file.close();
 alert("File downloaded to " + fileURL);
}

// Download an image file in ExtendScript over https
var url = "https://www.rorohiko.com/MagnetoGuides2-poster.jpg";

JSInterface.evalScript(
  "JSInterface.plugins.getURL(JSInterface.getData())", 
  url, 
  handleData);

Or

// Need to run in a persistent engine for callbacks to work
// Make sure to add JSInterface.jsx and json2.jsx

#targetengine TestSomewhere
#include "JSInterface.jsx"

// Some sample ExtendScript code. Fetch a URL over https and display 
// in an alert

function handleData(data) { 
 alert(data);
}

var url = "https://www.rorohiko.com/welcome.msg";

JSInterface.evalScript(
  "JSInterface.plugins.getURL(JSInterface.getData())", 
  url, 
  handleData);

This installer came about because I had built a more or less ‘standardized’ installer for installing the more recent software I wrote for Rorohiko. This installer does not rely on the ExManCmd command line tool, so it should be more robust. ExManCmd fails way too often, so I avoided using it.

And because I had the installer available anyway, I decided to roll off a copy just for ExtendExtendScript. Hope it proves useful!

5 thoughts on “ExtendExtendScript/GetURL: now with a simple installer”

  1. I’m integrating ExtendExtendScript into an application since I need the https support. Is there a recommended way of waiting for the result of the getURL to complete before continuing?

    Thanks again for this! This is saving me a lot of dev time.

  2. As to my previous question, I did not provide any details on what I am trying to do:

    I have queried a server:

    https://myserver.com?parms
    and the server is returning true or false which has been demonstrated using the sample code provided.

    Where I invoke the getURL call is where I want to wait until I get the result back in the callback.

    1. Hi Bill,

      I am not quite clear on what exactly you are doing. When you say ‘an application’ that seems to indicate an external stand-alone application, but I am not sure whether that is correct and whether that is relevant.

      Normally, what you do in JavaScript or ExtendScript (and this is how ExtendExtendScript does it too): you don’t wait. Instead you pass a callback into the asynchronous code, and that callback will be called once the asynchronous operation is finished. The calling code does not wait. It either goes on doing something else, or it terminates.

      If you are trying to code something like (pseudocode):

      DoSomething();
      var data = GetURL("farfaraway");
      DoSomethingWithData(data);

      That does not work. You need to approach this differently.
      DoSomething();
      GetURL("farfaraway", function(data) {
      DoSomethingWithData(data);
      });

      I.e. the main ‘thread’, if you will, ‘falls of the edge’ after the call to GetURL. And then once the GetURL is finished, it calls the callback, passes the data into it, and then we call DoSomethingWithData. This code does not wait.

  3. Hi,
    I’m calling an API that returns JSON which I then want to use in my script, so I do the following, which works, except that the JSON is UTF-8 but data is a string that is not UTF-8 decoded properly.

    I’m guessing this is because the GetURL is doing response.setEncoding(‘binary’), but I would like it to do response.setEncoding(‘utf8’). Now I have to manually UTF-8 decode the string. Is this something you have considered?

    function handleData(data) {
    doStuffWith(JSON.parse(data))
    }

    var url = “http://myapi”;

    JSInterface.evalScript(“JSInterface.plugins.getURL(JSInterface.getData())”, url, handleData);

    1. Hi Martin,

      The pre-packaged version that comes with the installer is as-is, but if you look at my other posts, all source code to GetURL is made available.

      It is based on a little utility module I built, called JSInterface, and the GetURL is no more than a sample plug-in for JSInterface. All source code is provided, and all you’d need to do is change the GetURL plug-in, or make a separate second plug-in.

      Option 1: dig into the source code, and modify the extension to match your needs. The GetURL plug-in is no more than a very small Node.js module, and the line you’re interested is around line 103:


      protocol.get(parsedURL, function(response) {

      response.setEncoding(‘binary’);

      function checkForCompletion() {

      Option 2: if you have time to wait it out, I think it is a good idea to make this configurable, and I could add some parameters to the GetURL so you could set the encoding from the calling code. Problem is: I am very busy, so it’ll be a fair while before I get round to that.

      Cheers,

      Kris

Leave a Reply

Your email address will not be published. Required fields are marked *