Skip to main content
carlp33822801
Inspiring
September 20, 2018
Answered

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

  • September 20, 2018
  • 3 replies
  • 8030 views

'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();

This topic has been closed for replies.
Correct answer 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 replies

Trevor:
Trevor:Correct answer
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

Geppetto Luis
Legend
September 20, 2018

Trevor I tried with mac but it does not work.

Trevor:
Legend
September 20, 2018

I'm really not a PS guy but this should work on Mac

if (app.name !== 'Adobe Photoshop') {

    alert('Crash,for Adobe Photoshop only!')

}

var remoteCode = 'https://gist.githubusercontent.com/Trevor-/6a02d8cb49bbfeb9822f061dea300bc1/raw';

// this function is nicked from // https: //forums.adobe.com/thread/2364797

function getSystemCommandStdout(command) {

    var stdout = "";

    var tempFile = new File(Folder.temp + "/temp.txt");

    app.system(command + " > " + tempFile.fsName);

    if (tempFile.open("r")) {

        stdout = tempFile.read();

        tempFile.close();

        tempFile.remove();

    }

    return stdout;

}

var result = getSystemCommandStdout("curl 'remoteCode'".replace("remoteCode", remoteCode));

// Set script args dosn't work on PS so just using setenv

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

// Set environmental vars

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

// Run the "remote" script

eval(result);

// Share functions and vars

blah(testVar, 'bar');

Note that after editing the gist it's getting a new URL, probably there's a simple work around

Edit 2: Changed the above seems to be be corrects and I changed the URL to the shorter form.


Yep if we just include the basic URL it should work with the updates

https://gist.githubusercontent.com/Trevor-/6a02d8cb49bbfeb9822f061dea300bc1/raw/

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.