Usage Patterns |
This topic covers patterns frequently used in ReadySuite scripts, drawn from real-world examples.
The most common operation is reading a field value from a document, transforming it, and writing it back (or to a different field).
while (script.ReadDocument()) { var document = script.Document; var value = document.GetFieldData(options.SourceField); if (!string.IsNullOrEmpty(value)) { document.SetFieldData(options.TargetField, value.Trim().ToUpper()); script.UpdateDocument(document); } }
Use GetOrCreateField() when your script needs a field that may not exist yet.
var field = script.GetOrCreateField("MyCustomField");
For typed fields, use CreateField() with a FieldItemType:
if (!script.HasField("PageCount")) { script.CreateField("PageCount", FieldItemType.Number); }
Access a document's native file, pages, or text resources:
if (document.HasNative()) { var native = document.GetNative(); var fullPath = native.GetFullPath(); var extension = native.FileExtension; } if (document.HasPages()) { var pages = document.GetPages(); foreach (var page in pages) { // page.GetFullPath(), page.Width, page.Height, etc. } }
Replace a native file:
var newNative = document.GetNewNative(newFilePath);
document.SetNative(newNative);
script.UpdateDocument(document);Work with document families (parent-child / attachment relationships):
if (document.HasFamily() && document.IsParent()) { var attachments = document.GetAttachments(); foreach (var attachment in attachments) { // Process child documents } }
Create new documents and attach them:
var newDoc = script.GetNewDocument("NEW-0001"); newDoc.SetNative(newDoc.GetNewNative(filePath)); script.CreateDocument(newDoc); document.AddAttachment(newDoc); script.UpdateDocument(document);
Wrap per-document operations in try-catch blocks so one bad document doesn't stop the entire script:
while (script.ReadDocument()) { var document = script.Document; try { // Process document script.UpdateDocument(document); } catch (Exception ex) { script.AddWarning(document, string.Format("Failed to process {0}: {1}", document.DocId, ex.Message)); } }
Use AddMessage() for informational output and AddWarning() for issues:
script.AddMessage("Processing complete. Updated 42 documents."); script.AddWarning(document, "Field was empty, skipped.");
Long-running scripts should check for cancellation:
while (script.ReadDocument()) { script.ThrowIfCancellationRequested(); // ... process document }
Or check manually:
if (script.IsCancellationRequested) break;
Use BatesCounter for parsing and incrementing bates numbers:
var bates = new BatesCounter(document.BatesBeg); string prefix = bates.Prefix; // e.g. "ABC-" long counter = bates.Counter; // e.g. 1234 string id = bates.Identifier; // e.g. "ABC-001234" bates.Increment();
Some scripts need to scan the full document set before processing. Use GetDocuments() for the first pass:
// First pass: collect data var lookup = new Dictionary<string, string>(); var documents = script.GetDocuments(); foreach (var doc in documents) { var key = doc.GetFieldData(options.KeyField); var value = doc.GetFieldData(options.ValueField); if (!string.IsNullOrEmpty(key)) lookup[key] = value; } // Second pass: apply while (script.ReadDocument()) { var document = script.Document; var key = document.GetFieldData(options.KeyField); if (lookup.TryGetValue(key, out var mapped)) { document.SetFieldData(options.TargetField, mapped); script.UpdateDocument(document); } }
Common pattern for extracting or matching text:
var pattern = new Regex(@"\b[A-Z]{3}-\d{6}\b"); var value = document.GetFieldData(options.SourceField); if (!string.IsNullOrEmpty(value)) { var match = pattern.Match(value); if (match.Success) { document.SetFieldData(options.TargetField, match.Value); script.UpdateDocument(document); } }