Overview

Scripts are the most powerful way to utilize the capabitlities of the pdf renderer or priint:comet Plug-Ins. Any script implemented in cscript or Python can be executed via the Java Rendering Library.

Execute Scripts

There are 2 methods to execute a cscript in a document context:

Execute predefined Actions

// run a script defined in the comet project associated to this document.
// Scripts are referred by their ID, so let's assume there is a suitable
// script with ID=17
int     scriptId = 17;
String   scriptArguments = "";

try (OpenedDocument document = Renderer.openDocument("DSN", new TmpCopyFromString("w2mlString", W2MLDocumentType.class))) {
  String scriptResult = document.execScript(scriptId, scriptArguments);
}
catch (RendererException e) {
  // Exceptions during opening or script execution 
}
catch (Exception e) {
  // Exception during auto-close
}

Script execution specifics are similar to the behavior we already know from InDesign® Server or "classic" PDF Renderer:

 

Script Arguments

The following example prints the String passed as scriptArgument to stdout:

import com.priint.comet.renderer.Renderer;
import com.priint.comet.renderer.internal.OpenedDocument;
// ...
String scriptCode = "int main() { showmessage(\"%s\", gArgument); return 0; }";
try (OpenedDocument document = ((Renderer) renderer).openDocument("DSN", new TmpCopyFromString("w2mlString", W2MLDocumentType.class))) {
  String scriptResult = document.execScriptCode(scriptCode, "Hello World");
}
catch (RendererException e) {
  // Exceptions during opening or script execution
}
catch (Exception e) {
  // Exception during auto-close
}

Script Result

The result type of execScript / execScriptCode methods is a Java String, which can be set in the target cscript via copying characters to the global variable gOutput. Maximum length is 32767 characters (+ terminating zero).

String scriptCode = "int main() { strcpy(gOutput, gArgument); return 0; }";
try (OpenedDocument document = ((Renderer) renderer).openDocument("DSN", new TmpCopyFromString("w2mlString", W2MLDocumentType.class))) {
  String scriptResult = ((OpenedDocument)document).execScriptCode(scriptCode, "Hello World");
  // scriptResult should be "Hello World"
}
catch (Exception e) {
  // ... 
}

Script Return Code

cscripts are supposed to return a result code:

Each (known) result code is translated to an appropriate Java Exception, see here for a list of known error codes.

As stated before in other context: Scripts should always return an appropriate result code!

String scriptCode = "int main() { return 1219; }";
try (OpenedDocument document = ((Renderer) renderer).openDocument(
                                  "DSN", 
                                  new TmpCopyFromString("w2mlString", W2MLDocumentType.class))) {
  document.execScriptCode(scriptCode, "");
}
catch (PageNotFoundException e) {
  // Error code for PageNotFoundException is 1219.
  // See Plugin documentation or ErrorCode class in 
  // Java Rendering Library for error code reference.
}
catch (RendererException e) {
  // Other exceptions during opening or script execution 
}
catch (Exception e) {
  // Exception during auto-close
}