Skip to content

Instantly share code, notes, and snippets.

@Trevor-
Last active September 21, 2018 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Trevor-/a5de06d41e28936ad72fe3e348bddd33 to your computer and use it in GitHub Desktop.
Save Trevor-/a5de06d41e28936ad72fe3e348bddd33 to your computer and use it in GitHub Desktop.
Curls And Executes a remote script in InDesign
////////////////////////////////////////////////////////////
// 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') { // [M]ac
appleScript = "do shell script \"curl 'remoteCodeUrl'\"".replace("remoteCodeUrl", remoteCodeUrl);
remoteScript = app.doScript(appleScript, ScriptLanguage.APPLESCRIPT_LANGUAGE);
} else { // [W]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
*/
@Trevor-
Copy link
Author

Trevor- commented Sep 21, 2018

This shows how to curl and execute a script in InDesign with various methods of sharing vars / functions between the local and remote in both directions.
See https://forums.adobe.com/message/10632451#10632451 for background

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment