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 21, 2018

Trevor this works perfectly with photoshop mac

  1. if (app.name !== 'Adobe Photoshop') { 
  2.     alert('Crash,for Adobe Photoshop only!'
  3.  
  4. var remoteCode = 'https://gist.githubusercontent.com/Trevor-/6a02d8cb49bbfeb9822f061dea300bc1/raw'
  5.  
  6. // this function is nicked from // https: //forums.adobe.com/thread/2364797 
  7. function getSystemCommandStdout(command) { 
  8.     var stdout = ""
  9.     var tempFile = new File(Folder.temp + "/temp.txt"); 
  10.     app.system(command + " > " + tempFile.fsName); 
  11.     if (tempFile.open("r")) { 
  12.         stdout = tempFile.read(); 
  13.         tempFile.close(); 
  14.         tempFile.remove(); 
  15.     } 
  16.     return stdout; 
  17.  
  18. var result = getSystemCommandStdout("curl 'remoteCode'".replace("remoteCode", remoteCode)); 
  19. // Set script args dosn't work on PS so just using setenv 
  20. $.setenv("envVar1", "Hello"); 
  21. // Set environmental vars 
  22. $.setenv("envVar2", "World"); 
  23. // Run the "remote" script 
  24. eval(result); 
  25. // Share functions and vars 
  26. blah(testVar, 'bar'); 

If I had to use windows

how the script should be

that's fine

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

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

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


Sorry, I don't have time now. I might have a moment on Sunday if not then I'll be away for 1.5 weeks.

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.