Creation and execution of a Jobs Queue.

Last Change :
24.04.2025, 13:54 Uhr

Creation and execution of a Jobs Queue. Unfortunately, editing many documents in a single script repeatedly leads to memory overflow (and crashing) of InDesign®. The problem here is that although you can open and close documents properly, InDesign® holds the closed documents in memory until the next idle time (i.e. at least until the end of the script). And there is no way to enter this intermediate world between life and death or to somehow get InDesign® to rest.

The Jobs Queue solves this problem by executing each job in its own timer-controlled independent sequence. Once a job has opened a document (and closed it again), the sequence waits until InDesign® has closed the document in the background too. Only then is the next job in the queue started.

For this to work, the job script must register the document after opening it in the variable gOpenedDoc and close it again before the end of the script. To register an open document, use a call as follows:

item::define (gOpenedDoc, document::get_front (1), 1);

If the script leaves the edited document open (or does not open a document at all), leave the variable gOpenedDoc unchanged and empty. The queue will then start the next job immediately after the current job has been completed.

Once all jobs have been completed, the processing of the queue is automatically deactivated and must be reactivated with jobs::start to process further jobs.

The following global variables are defined in queue scripts:

Variable In/Out Data Type Description
gJobsQueueId in char*

Job identifier. The value of the variable is defined in jobs::add and usually contains the sually contains the ID or path of the document to be processed.

Note: The job identifier does not have to be a reference to a document. Of course, you can also perform tasks here that are completely independent of a specific document.

gOpenedDoc out ItemRef

After the script has opened a document, enter the reference to this document here. The queue will then wait after the job has been executed until we can no longer find this document in the list of open documents.

publication::checkout_by_id (
 	gJobsQueueId,
 	2, 0, 0,
 	0,
 	1); // suppress UI
item::define (gOpenedDoc, document::get_front (1), 1);
...
document::save (); publication::checkin_by_id (gJobsQueueId);

Important note : If you register a document in gOpenedDoc, then the script must also close this document again! If you leave a registered document open, the jobs queue will wait until the document is closed.

If the document is to remain open or if no document has been opened at all, leave gOpenedDoc empty.

static int jobs::add(
  int jobsQueueID,
  int actionID = 0,
  char* actionPath = 0)

Add a job to the Jobs Queue. Processing of the jobs is started with jobs::start.

Name Type Default Description
Return int   0 or Error Code
jobsQueueID char* oder String - Identification of the job. The information is passed to the executing script in the parameter gJobsQueueId and usually contains the ID or path of a document to be processed. However, you can also use any other name that you evaluate accordingly in the job script (the action).
actionID int 0 Id a defined action of the data connection to execute the job

≤ 0 : Use actionPath
actionPath char* or String 0 Complete path of the action for executing the order. The parameter is only used if actionID ≤ 0 is.

Create a processing job for each of five existing documents. After that, a new document should be created in a further job. At the end of the script, the queue processing of the jobs is started.

#pragma plain
int main () { int actionID = 0; char * actionPath = "$DESKTOP/bbb.cpp";
jobs::add ("$DESKTOP/aaa.indd", actionID, actionPath); jobs::add ("$DESKTOP/bbb.indd", actionID, actionPath); jobs::add ("$DESKTOP/ccc.indd", actionID, actionPath); jobs::add ("$DESKTOP/ddd.indd", actionID, actionPath); jobs::add ("create", actionID, actionPath);
wlog ("", "Jobs in queue : %d\n", jobs::count ());
jobs::start (); return 0; }

The example shows a simple script for a Jobs Queue job. It is important to open and close the target document and registering the document in the queue using the variable gOpenedDoc.

#include "internal/types.h"
int main () { ItemRef fr = item::alloc (); int result;
wlog ("", "# Working on '%s', %d docs are opened before\n",   gJobsQueueId,   document::count ());
// Open or create the document to apply // if (strcmp (gJobsQueueId, "create") == 0) document::create (); else document::open (gJobsQueueId);
// Register the document in the Jobs Queue // item::define (gOpenedDoc, document::get_front (), 1);
// Create a star // result = frame::create2 (   fr,   kStar,   0.0, 0.0, 110.0, 110.0,   1, // Page   "", // Layer name   1, // Number of columns   0, // Vertical columns?   0.0, // Gutter   5, 50); // Number and length of edges of the star
// Fill and rotate the star // frame::color_rgb (fr, 0, 153, 204); frame::rotate (fr, -20.0);
// If we have a new or converted document, ask for 'Save As'. // Otherwise save // if (!document::can_save ()) document::saveas (0, "", kMinimalUI); document::close ();
wlog ("", "# Working on '%s' done.\n", gJobsQueueId);
return 0; }

v5.0 R36580, 21. April 2025

priint:comet InDesign® Plug-Ins

start
document::open
publication::checkout_by_id

static int jobs::start()

Start processing the jobs in the Jobs Queue.

Once all jobs have been completed, the processing of the queue is automatically deactivated and must be reactivated with jobs::start to process further jobs.

v5.0 R36580, 21. April 2025

priint:comet InDesign® Plug-Ins

add
stop

static int jobs::active()

Is Jobs Queue processing activated?

Name Type Default Description
Return int   0 : No
1 : Yes

v5.0 R36580, 21. April 2025

priint:comet InDesign® Plug-Ins

add
start
stop

static int jobs::stop(int doClear = 0)

Stop processing the jobs in the queue.

Name Type Default Description
Return int   0 or Error Code
doClear int 0 Should all jobs that are still in the queue also be removed?

0 : No
1 : Yes

v5.0 R36580, 21. April 2025

priint:comet InDesign® Plug-Ins

add
start
clear

static int jobs::clear(int doStop = 0)

Remove all pending and unstarted jobs from the queue.

Name Type Default Description
Return int   0 or Error Code
doClear int 0 Should the processing of the queue also be deactivated?

0 : No
1 : Yes

v5.0 R36580, 21. April 2025

priint:comet InDesign® Plug-Ins

add
start
stop

static int jobs::count()

How many orders (jobs) are still in the queue?

Name Type Default Description
Return int   Number of orders still outstanding in the queue

v5.0 R36580, 21. April 2025

priint:comet InDesign® Plug-Ins

add
clear
get_nth

static int jobs::get_nth(
  int nth,
  IDType* data = 0,
  char* docId = 0)

Get the data of the nth order in the queue.

Name Type Default Description
Return int   0 or Error Code
nth int - 0-based index of the order in the queue
data IDType 0 Allocated IDType for the order data or 0, see add
jobsQueueID char* oder String 0 Allocated string/char* for the job identifier or 0, see add.

v5.0 R36580, 21. April 2025

priint:comet InDesign® Plug-Ins

add
count
clear

Since
v5.0 R36580, 21. April 2025
Version
24.04.2025, 13:54 Uhr
Author
Paul Seidel

Alphabetic index HTML hierarchy of classes or Java