Template for batch processor
In essence, the script created from the template knows whether it’s executed from the batch processor or as a regular script.
In other words, if you want to make your script work both in the batch processor and as a regular script, use this template as a starting point.
The CalledFromBatchProcessor() function checks the current stack trace and if it contains ‘[Batch processor’, it returns true, otherwise – false.
With the all open documents option selected, make sure to use the g.doc variable to send the current document from the primary to the secondary script, otherwise, it will not work correctly. With all other options, you can alternatively use app.activeDocument or app.documents[0]. If the script is called from the batch processor and the g.doc variable is sent from it, the doc variable is set to g.doc. If something goes wrong, though it’s hardly possible, an error message is written to the log: "ERROR: 'g.doc == null' - Failed to get the document to process."
If the script is run as a regular one — from the Scripts panel or ESTK — the doc variable is set to the current document.
Also, the template shows how to handle arguments: the parameters that can be optionally sent from the primary script (the batch processor) to the secondary script. See here more on the arguments.
If you execute the script directly from InDesign, arguments are unavailable. If from the batch processor, some parameters can be skipped or you may not send arguments at all. For that reason, you should set default parameters.
Click here to download the template.
Back to the main Batch processor page