GithubHelp home page GithubHelp logo

run-script-examples's Introduction

Run a script

Scripts can be run with a POST API call using your Run Script API Key and Secret to authenticate. Use the body of the POST to control input & output files and script arguments.

Request body

Key Type Description
inputs array of file objects {href: S3 Presigned getObject URL, path: local path to store file}
outputs array of file objects {href: S3 Presigned putObject URL, path: local path to store file}
args array of arg objects {name: string, value: string}
script string InDesign script
webhook url url receives a POST with job data every time a job changes status

Request example

var axios = require('axios');
var data = {
  "inputs": [
    {
      "href": "https://example.s3.us-east-1.amazonaws.com.....", // S3 Presigned getObject URL 
      "path": "jobFolder/input.indd" // Path to store file
    }
	],
  "outputs": [
    {
      "href": "https://example.s3.us-east-1.amazonaws.com....." // S3 Presigned putObject URL 
      "path": "jobFolder/output.pdf", // Path to store file			
    }
  ],
  "args": [
    {
      "name": "count",
      "value": "100"
    }
  ],
  "script": "app.consoleout('test');",
};

var runscriptApiKey = '5d6c6298de22d$$83f5180f4'; // Your API key
var runscriptApiSecret = '2b108tRnVwK2VDG8qJGpLZWRredLmL2dljBaywkOIlPR2YmO9QWq1DwRy'; // Your API Secret
var auth = {username: runscriptApiKey, password: runscriptApiSecret};
var url = 'https://runscript.typefi.com/api/v1/job'
var response = await axios.post(url, data, {auth: auth});

Response

{
  "_id": "5d71b313eef1c01dcb997s1e",
  "status": "queued",    
  "created": "2019-09-06T01:15:00.007Z",
  "script": "...",
  "inputs": [
    ...
  ],
  "outputs": [
    ...
  ],
  "args": [
    ...
  ],    
}

Get a jobs status

You can manually poll a job to check its status using a GET API call with the job id in the url.

Key Type Description
status string current status: queued, inProgress, complete, failed or canceled
result string completed result: success, canceled or failed
completed date when the job completed
runTime integer time the job ran for in milliseconds

Request example

var runscriptApiKey = '5d6c6298de22d$$83f5180f4'; // Your API key
var runscriptApiSecret = '2b108tRnVwK2VDG8qJGpLZWRredLmL2dljBaywkOIlPR2YmO9QWq1DwRy'; // Your API Secret
var jobId = '5d71b313eef1c01dcb997s1e';
var auth = {username: runscriptApiKey, password: runscriptApiSecret};
var url = 'https://runscript.typefi.com/api/v1/job/' + jobId;
var response = await axios.get(url, {auth: auth});

Response

{
  "_id": "5d71b313eef1c01dcb997s1e",
  "status": "complete",    
  "created": "2019-09-09T00:09:30.489Z",
  "completed": "2019-09-09T00:09:33.929Z",
  "result": "success",
  "runTime": 2862,
  "script": "...",
  "inputs": [
    ...
  ],
  "outputs": [
    ...
  ],
  "args": [
    ...
  ],    
}

File inputs & outputs

You need to list the files that will be inputs from S3 to the InDesignContainer and also the files that will be outputs from the InDesignContainer to S3. This is done using a S3 Presigned URL.

var AWS = require("aws-sdk");
var s3region = 'example-region-1';
var s3accessKeyId = 'EXAMPLE_ACCESS_KEY';
var s3secretAccessKey = 'EXAMPLE_SECRET_ACCESS_KEY';
var s3bucket = 'example-bucket';

var s3 = new AWS.S3({ region: s3region, accessKeyId: s3accessKeyId, secretAccessKey: s3secretAccessKey});

// Create an input file 
var inputFile1 = {};
inputFile1.href = await s3.getSignedUrl('getObject', {Bucket: s3bucket, Key: 'folder/example.txt'}); // must match existing file on s3
inputFile1.path = 'jobFolder/example.txt';

// Create an out file
var outputFile1 = {};
outputFile1.href = await s3.getSignedUrl('putObject', {Bucket: s3bucket, Key: 'folder/example.txt', ContentType: 'application/octet-stream'}); // ContentType must be application/octet-stream  
outputFile1.path = 'jobFolder/example.txt';

var inputs = [
  inputFile1
];

var outputs = [
  outputFile1
];

Script example

var currentPath = Folder.current; // The root path usually "c:/"
File example = new File('jobFolder/example.txt'); // File path is relative

Script arguments

Run Script passes arguments to InDesign Servers scriptArgs which can be accessed from your scripts. *All values are type string

Request body

{
  "args": [
    {
      "name": "foo",
      "value": "bar"
    },
    {
      "name": "count",
      "value": 123
    }
  ]
}

Script example

if(app.scriptArgs.isDefined("foo") == true){
  var foo = app.scriptArgs.getValue("foo"); // returns "bar"
}
if(app.scriptArgs.isDefined("count") == true){
  var count = app.scriptArgs.getValue("count"); // returns "123"
}

Webhook

Instead of polling RunScript with GET calls to track job status you can use a webhook. A webhook is a url that recieves a POST with the job object every time the job status changes. The webhook could also be used to notify extenal applications like Slack or GitHub.

Request body

{
  "inputs": [
    ...
  ],
  "outputs": [
    ...
  ],
  "script": "...",
  "webhook": "https://hooks.zapier.com/hooks/catch/24234234/example/"
}

Response body

{
  "_id": "5d71b313eef1c01dcb997s1e",
  "status": "inProgress",    
  "created": "2019-09-09T00:09:30.489Z",
  "script": "...",
  "inputs": [
    ...
  ],
  "outputs": [
    ...
  ],
  "webhook": "https://hooks.zapier.com/hooks/catch/24234234/example/"
}

run-script-examples's People

Contributors

benvale avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.