This chapter includes JavaScript samples that provide examples of scripts you might create for client profiles, import jobs, or recognition jobs.
This chapter contains the following sections:
This section includes the following sample scripts:
Section 2.1.1, "Sample Client Script 1"
Section 2.1.2, "Sample Client Script 2"
This sample script customizes client behavior in the following ways:
Prevents the client user from leaving a metadata field if the entry contains the word "test"
Prevents the user from entering an asterisk in any metadata field.
Outputs event information to the java console, such as coordinates after a user right-mouse-drags a selection on an image.
Note that this script also writes out a line (printIn) to the java console for each script event, for verification or debugging purposes.
importClass(java.awt.event.KeyEvent);
function ScriptStart() {
println("ScriptStart");
}
function BatchScanBegin(event) { // BatchScanEvent
println("BatchScanBegin");
}
function BatchScanComplete(event) { // BatchScanEvent
println("BatchScanComplete");
println(event.getBatch().getBatchName() + " finished Scanning.");
}
function BatchSelected(event) { // BatchSelectedEvent
println("BatchSelected: " + event.getBatch().getBatchName());
}
function PreBatchDelete(event) { // BatchDeleteEvent
println("BatchDelete");
}
function CaptureImage(event) { // ImageCaptureEvent
println("CaptureImage");
}
function DocumentCreated(event) { // DocumentCreatedEvent
println("DocumentCreated");
}
function DocumentSelected(event) { // DocumentSelectedEvent
println("DocumentSelected: " + event.getDocument().getTitle());
}
function FieldGotFocus(event) { // FieldEvent
println("FieldGotFocus");
}
function FieldLostFocus(event) { // FieldEvent
var dataField;
println("FieldLostFocus");
dataField = event.getField();
if (dataField.getUncommittedText().equalsIgnoreCase("test")) {
event.setCancel(true);
println("invalid value. script will not allow leaving focus.");
}
}
function FieldProcessKey(event) { // FieldEvent
var keyEvent;
// println("FieldProcessKey");
keyEvent = event.getKeyEvent();
if (keyEvent.getID() == KeyEvent.KEY_TYPED) {
//println(keyEvent.getKeyChar());
if (String.fromCharCode(keyEvent.getKeyChar()) == '*') {
println("Asterisk not allowed in any field.");
keyEvent.consume();
}
}
}
function PostCaptureImage(event) { // ImageCaptureEvent
println("PostCaptureImage");
}
function PreCaptureImage(event) { // ImageCaptureEvent
println("PreCaptureImage");
}
function PreUploadItem(event) { // UploadItemEvent
println("PreUploadItem: " + event.getCaptureItem().getFilename());
}
function PostUploadItem(event) { // UploadItemEvent
println("PostUploadItem: " + event.getCaptureItem().getFilename());
}
function DBSearchComplete(searchEvent) { // DBSearchEvent
println("DBSearchComplete.");
}
function DBSearchResults(searchEvent) { // DBSearchEvent
var results;
var resultRow;
var searchParameters;
println("DBSearchResult");
results = searchEvent.getRowResults();
println("Found " + results.size() + " results.");
}
function DBSearchStart(searchEvent) { // DBSearchEvent
println("DBSearchStart");
println("Metadata value was " + searchEvent.getMetadataValue());
//searchEvent.setMetadataValue("c");
}
function DocumentRemoved(event) { // DocumentRemovedEvent
println("DocumentRemoved");
}
function ImportFilesSelected(files, cancel) { // ImportFilesSelectedEvent
println("ImportFilesSelected");
}
function PostDownloadItem(event) { // DownloadItemEvent
println("PostDownloadItem: " + event.getCaptureItem().getFilename());
}
function PreDownloadItem(event) { // DownloadItemEvent
println("PreDownloadItem: " + event.getCaptureItem().getFilename());
}
function RegionSelected(event) { // RegionSelectedEvent
var rect;
println("RegionSelected");
rect = event.getSelectionRectangle();
println("Rectangle (X,Y): (" + rect.getX() + "," + rect.getY() + "); (W,H): (" + rect.getWidth() + "," + rect.getHeight() + ")");
}
This sample script customizes client behavior in the following ways:
Uses the BatchScanBegin function to restrict files that can be imported to those with a .TIF extension only.
Uses the DBSearchResults function to modify the results of a database lookup so that only the first result is used, and prevents the results list from displaying.
importClass(java.util.ArrayList);
function BatchScanBegin(event) { // BatchScanEvent
// Check if there are files being imported.
var sourceFilesList = event.getSourceFiles();
if (sourceFilesList != null) {
// Create a list to hold the filtered results.
var filteredList = new ArrayList();
// Loop through each of the files.
var iterator = sourceFilesList.iterator();
while (iterator.hasNext()) {
// If the file name ends with ".TIF", add it to the list.
var file = iterator.next();
var filename = file.getName().toUpperCase();
if (filename.endsWith(".TIF")) {
filteredList.add(file);
}
}
// Replace the original list with the filtered list.
event.setSourceFiles(filteredList);
}
}
function DBSearchResults(searchEvent) { // DBSearchEvent
var results;
var resultRow;
var searchParameters;
// Return only the first search result.
results = searchEvent.getRowResults();
if (results.size() > 0) {
resultRow = results.get(0);
results.clear();
results.add(resultRow);
// Do not display the list of results to the user.
searchEvent.setDisplayHitlist(false);
}
}
The following sample script sets each document's title to the name of the file being imported. When the documents are later committed, their document title can be mapped to an output field.
importClass(java.io.File);
function preCreateDocument(event) { // ImportProcessorContext
var document; // DocumentEntity
var sourceFile; // File
sourceFile = new File(event.getImportSourceFile());
document = event.getDocumentEntity();
// Set the document title to be the name of the source file
document.setDocumentTitle(sourceFile.getName());
}
This sample script customizes the recognition job's behavior in the following ways:
Sets the document organization type to a fixed number of pages per batch.
Sets the maximum pages per document to two.
Sets the job to detect pdf417 bar codes only.
Defines three bar codes named processorDate, Title, and Amount (with no validation rules).
Map the bar codes to three metadata fields.
function batchItemAllValidBarcodes (rpc) {
// Obtain current batch item
var batchItem = rpc.getBatchItem();
// obtain bar code count.
var count = batchItem.getBarcodeCount();
// All barcodes on a batch item.
var allBarcodes;
// bar code of interest.
var barcodeValue;
// after parsed barcode value.
var parsed;
// Obtain bar code value if there is a bar code found.
if (count > 0) {
allBarcodes = batchItem.getBarcodes();
barcodeValue = allBarcodes[0];
// Parse the bar code value by | character.
parsed = barcodeValue.split('\\|');
var len = parsed.length;
// It should get splitted into 10 strings.
if (len == 10) {
// This is the barcode we want, populate valid bar codes.
populateValues(rpc, parsed);
}
}
}
function populateValues(rpc, parsed) {
var valid = rpc.getValidBarcodes();
var i;
for (i=0; i<valid.size(); i++) {
var bar = valid.get(i);
if (bar.getName() == "processDate") {
bar.setValue(parsed[5]);
} else if (bar.getName() == "Title") {
bar.setValue(parsed[6]);
} else if (bar.getName() == "Amount") {
bar.setValue(parsed[4]);
}
}
}