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

Geppetto Luis
Legend
September 20, 2018

Trevor I tried with mac but it does not work.

Trevor:
Legend
September 21, 2018

Here's Windows InDesign version.

I like it as it returns the result of the powershell script to the jsx engine which has some nice potential to it.

// For InDesign Windows !!!!!

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

var vbs = [

    'Set shell = CreateObject("WScript.Shell")',

    'Set executor = shell.Exec("powershell.exe  -windowstyle hidden Invoke-WebRequest -Uri remoteCode | Select-Object -ExpandProperty Content")'.replace("remoteCode", remoteCode),

    'executor.StdIn.Close',

    'returnValue = executor.StdOut.ReadAll',

].join('\n');

var script = app.doScript(vbs, ScriptLanguage.VISUAL_BASIC);

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

/* Some sources

https://github.com/PowerShell/PowerShell/issues/3028

https://stackoverflow.com/questions/36941027/how-to-return-powershell-variable-to-vbscript

https://forums.adobe.com/thread/2000455

*/


This one demonstrates sharing vars simply in both directions without needing environmental variables or ScriptArgs.

Works for both Mac and Windows on InDesign

See the gists Demo for running remote scripts on InDesign. Shares vars and arguments with "local" script. See 1st comment for link to …

and Curls And Executes a remote script in InDesign · GitHub

Local Script

////////////////////////////////////////////////////////////

// For InDesign Windows !!!!!                             //

// See https://forums.adobe.com/message/10632451#10632451 //

// By Trevor http://creative-scripts.com    21 Sep 18     //

////////////////////////////////////////////////////////////

// https://gist.github.com/Trevor-/a5de06d41e28936ad72fe3e348bddd33

var remoteCodeUrl, vbs, appleScript, remoteScript;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Change to desired URL, must be raw, i.e. no HTML, can use github / patesbin to host make sure to include the /raw part in the link //

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

remoteCodeUrl = 'https://gist.githubusercontent.com/Trevor-/55297356e5728f57c2225df16a8e8b0e/raw';

// "aVarFromLocalToRemote" is used in the "remote" script

var aVarFromLocalToRemote = 'A Var From Local To Remote';

if ($.os[0] === 'M') { // ac

    appleScript = "do shell script \"curl 'remoteCodeUrl'\"".replace("remoteCodeUrl", remoteCodeUrl);

    remoteScript = app.doScript(appleScript, ScriptLanguage.APPLESCRIPT_LANGUAGE);

} else { // indows

    vbs = [

        // Create Shell Object

        'Set shell = CreateObject("WScript.Shell")',

        // Execute curl in powershell

        'Set executor = shell.Exec("powershell.exe  -windowstyle hidden Invoke-WebRequest -Uri remoteCodeUrl | Select-Object -ExpandProperty Content")'.replace("remoteCodeUrl", remoteCodeUrl),

        // Capture stdout

        'executor.StdIn.Close',

        // "Forward" result to jsx engine

        'returnValue = executor.StdOut.ReadAll',

    ].join('\n');

    remoteScript = app.doScript(vbs, ScriptLanguage.VISUAL_BASIC);

}

/////////////////////////////////////

// Change as per remoteScript      //

// This works for the URL provided //

/////////////////////////////////////

// Set script args, these can be fetched by the remote

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

// Set environmental vars

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

// Run the "remote" script

app.doScript(remoteScript, ScriptLanguage.JAVASCRIPT);

// Share functions and vars

// "blah" and "aVarFromRemoteToLocal" are grabbed from the remote script

blah('Demo by: ', aVarFromRemoteToLocal);

/* Some sources

https://github.com/PowerShell/PowerShell/issues/3028

https://stackoverflow.com/questions/36941027/how-to-return-powershell-variable-to-vbscript

https://forums.adobe.com/thread/2000455

*/

Remote - Fetched Script

////////////////////////////////////////////////////////////////////////////////////////

// For InDesign but can easily be adapter for some other apps                         //

// Show methods of sharing vars etc. between local and remote scripts                 //

// By Trevor http://creative-scripts.com 21 Sep 18                                    //

// This is the "remote" script                                                        //

// The "local" is at https://gist.github.com/Trevor-/a5de06d41e28936ad72fe3e348bddd33 //

////////////////////////////////////////////////////////////////////////////////////////

// "aVarFromLocalToRemote" is set in the "local" script

alert("Got " + aVarFromLocalToRemote);

var aVarFromRemoteToLocal = "Trevor - http://creative-scripts.com";

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

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

function blah(bar, baz){

    var result = bar + " " + baz;

    alert(result);

    return result;

}

blah(scriptArg1, envVar1);

HTH

Trevor

P.s. The end!

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.