Skip to main content
carlp33822801
Inspiring
September 20, 2018
Resuelto

Include remote .js-file in script-file (and use it's variables)

  • September 20, 2018
  • 3 respuestas
  • 8030 visualizaciones

'm trying to access a remote .jsfile within an inDesign script to use it's variables. I found functions for including js-files locally but haven't found a good way to include.

http://remote-site.com/test.js:

var testVar = "it works!";

myscript.js, including locally (working):

app.doScript(new File("/Users/Popmouth/test.js")); alert(testVar);

myscript.js, including locally including remotely (not working):

app.doScript(new File("http://remote-site.com/test.js")); alert(testVar);

Error message

I also found this snippet, this alert works (alerts the content of the file, i.e. "var testVar = "it works!;") but I don't know how to use the vars in my alert function below:

var HTTPFile = function (url,port) {

          if (arguments.length == 1) {

                    url = arguments[0];

                    port = 80;

          };

          this.url = url;

          this.port = port;

          this.httpPrefix = this.url.match(/http:\/\//);

          this.domain = this.httpPrefix == null ? this.url.split("/")[0]+":"+this.port :this.url.split("/")[2]+":"+this.port;

          this.call = "GET "+ (this.httpPrefix == null ? "http://"+this.url : this.url)+" HTTP/1.0\r\nHost:" +(this.httpPrefix == null ? this.url.split("/")[0] :this.url.split("/")[2])+"\r\nConnection: close\r\n\r\n";

          this.reply = new String();

          this.conn = new Socket();

          this.conn.encoding = "binary";

          HTTPFile.prototype.getFile = function(f) {

                    var typeMatch = this.url.match(/(\.)(\w{3,4}\b)/g);

                    if (this.conn.open(this.domain,"binary")) {

                              this.conn.write(this.call);

                              this.reply = this.conn.read(9999999999);

                              this.conn.close();

                    } else {

                              this.reply = "";

                    }

                    return this.reply.substr(this.reply.indexOf("\r\n\r\n")+4);;

          };

}



var hyle = new HTTPFile("http://remote-site.com/test.js ");

alert(hyle.getFile());

hyle.getFile();

Este tema ha sido cerrado para respuestas.
Mejor respuesta de Trevor:

This one works from a real remote.

It's Mac only I'll see about Windows for the sake of compleatness

var remoteCode = 'https://gist.githubusercontent.com/Trevor-/a297b25e270c2e19d3ba12b5406212d9/raw/88837413a0d5a234ad257bfc63c79f3fb3f87a25/gistfile1.txt';

var script = app.doScript("do shell script \"curl 'remoteCode'\"".replace("remoteCode", remoteCode), ScriptLanguage.APPLESCRIPT_LANGUAGE);

// Set script args

app.scriptArgs.setValue("scriptArg1", "Hello");

// Set environmental vars

$.setenv("envVar1", "World");

// Run the "remote" script

app.doScript(script, ScriptLanguage.JAVASCRIPT);

// Share functions and vars

blah(testVar, 'bar');

HTH

Trevor

3 respuestas

Trevor:
Trevor:Respuesta
Legend
September 20, 2018

This one works from a real remote.

It's Mac only I'll see about Windows for the sake of compleatness

var remoteCode = 'https://gist.githubusercontent.com/Trevor-/a297b25e270c2e19d3ba12b5406212d9/raw/88837413a0d5a234ad257bfc63c79f3fb3f87a25/gistfile1.txt';

var script = app.doScript("do shell script \"curl 'remoteCode'\"".replace("remoteCode", remoteCode), ScriptLanguage.APPLESCRIPT_LANGUAGE);

// Set script args

app.scriptArgs.setValue("scriptArg1", "Hello");

// Set environmental vars

$.setenv("envVar1", "World");

// Run the "remote" script

app.doScript(script, ScriptLanguage.JAVASCRIPT);

// Share functions and vars

blah(testVar, 'bar');

HTH

Trevor

carlp33822801
Inspiring
September 20, 2018

Thanks man! I googled for like 3 hours to find anything, and this worked. So just to understand, are you "knocking out" app.doScripts inherit function to search for the file on machine, and instead search remotely?

Trevor:
Legend
September 20, 2018

The 1st doScript downloads the remote script.

The 2nd one runs it.

Trevor:
Legend
September 20, 2018

OK here's part one of the demo

var script= [

            'var testVar = "foo";',

            'var scriptArg1 = app.scriptArgs.getValue("scriptArg1");',

            'var envVar1 = $.getenv("envVar1");',

            'function blah(bar, baz){',

            'var result = bar + " " + baz;',

            'alert(result);}',

            'blah(scriptArg1, envVar1)'

            ].join('\n');

// Set script args

app.scriptArgs.setValue("scriptArg1", "Hello");

// Set environmental vars

$.setenv("envVar1", "World");

// Run the "remote" script

app.doScript(script,ScriptLanguage.JAVASCRIPT);

// Share functions and vars

blah(testVar, 'bar');

For now we'll use a local "remote" script but the idea will be exactly the same once you have curled it.

You can see 3 methods for passing the vars and args from one to the other.

I'll try paste a gist so I can demonstrate the real remote version.

Trevor:
Legend
September 20, 2018

use #include "path/to/file"

or $.evalFile(File('path/to/file')

Edit: sorry I missed the point, you need for remote.

Trevor:
Legend
September 20, 2018

as you're using InDesign and http you could just use a quick doScript (applescript) to curl the site that will retrieve the code then you can do another doScript to pass the arguments to the code in js. I'll see if I get a chance to paste some code later.