• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Explorer ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

5.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guru , Sep 20, 2018 Sep 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",

...

Votes

Translate

Translate
Guru ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

use #include "path/to/file"

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

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

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/88837413a0d5a234ad25...

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

The 1st doScript downloads the remote script.

The 2nd one runs it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

BTW I did a nice Windows version, I'll post later when I'm by my Windows machine.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

sorry Trevor I use mac

I would like to use it with photoshop

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

The BTW was for the benefit of others.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

Trevor, thank you for your time.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

Sorry, I thought prev. thread was about inDesign and not PS, so that's why I miss led you here. Hope it works out!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

Trevor I tried with mac but it does not work.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

Works for me what error are you getting?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

the mistake is this

Schermata 2018-09-20 alle 21.01.01.png

I would like to open this here

I entered the code but it does not work

var remoteCode = 'https://drive.google.com/open?id=1wB7rJILvXaCO5FVcGkqEEPbHPpAMFB6X/SCRIPT.jsx'; 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

It's for InDesign 😉

For photoshop it would need to be slightly changed.

I don't have time now.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

Also for the remote code make sure to use a Raw format otherwise the background HTML will mess up the script.

Github gists are a very easy method to do this.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

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

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

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

*/

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Sep 21, 2018 Sep 21, 2018

Copy link to clipboard

Copied

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 ...

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Sep 21, 2018 Sep 21, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Sep 21, 2018 Sep 21, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Sep 21, 2018 Sep 21, 2018

Copy link to clipboard

Copied

OK

Thanks for everything

good job

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Oct 03, 2018 Oct 03, 2018

Copy link to clipboard

Copied

geppettol66959005​ The solution is very simple but as it's a Photoshop question start a new thread on the ps scripting forum and send then post a link to it here and I'll answer there.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines