-
Notifications
You must be signed in to change notification settings - Fork 16
/
DomTextParcelsTest.jsx
84 lines (70 loc) · 2.66 KB
/
DomTextParcelsTest.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#target 'indesign'
#include '../$$.jsxinc'
#include '../etc/$$.Progress.jsxlib'
#include '../etc/$$.Dom.TextParcels.jsxlib'
// Load the framework in TRACE mode, so you will see every step
// processed by TextParcels. Remove the '-1' to speed up the script!
// ---
$$.load(-1);
// =============================================================================
// DomTextParcelsTest [230723]
// Getting started with the TextParcels class :-)
// ---
// Demonstrates:
// - Calling the error() function.
// - Using a simple progress bar (via $$.Progress).
// - Creating a TextParcels (TP) instance w/ default options.
// - Running TP.consolidate(), then TP.getSamples() with a custom handler.
// - Using the Log to collect data (Log.push).
// =============================================================================
try
{
const doc = app.properties.activeDocument;
if( !doc ) error("No document available!");
const pgs = doc.pages;
const TP = $$.Dom.TextParcels;
const mySampleHandler = function(/*str|str[]*/input,/*pageID*/_pid)
//----------------------------------
// 1. This handler can receive either a simple string or an array.
// 2. Whenever it 'accepts' one (or more) strings, it should
// increase its SIZE property in accordance. This hidden counter
// allows the caller (TP.getSamples) to take good decisions.
// Rem: `_pid` is the page ID prefixed by a `_`.
{
var pg = pgs.itemByID(+_pid.slice(1)).name;
var sample = input && input instanceof Array
? input[~~(input.length*Math.random())] // Pick up a string at random
: input;
// Change line breaks into U+21A9, truncate to 100 chars.
sample = sample.replace(RegExp.LINEs,'\u21A9').trunc(100);
++callee.SIZE;
// Rem: `Log.push()` is unconditional, it works even if IdExtenso
// is NOT loaded in trace/warn mode.
$$.Log.push(__("Page %1, found: %2", pg, sample));
};
$$.Progress.title("Testing TextParcels");
// Consolidation may take some time depending on your document;
// better is to load a simple progress bar (without % though)
$$.Progress.message( "Scanning the document..." );
var myTP = new TP(doc);
myTP.consolidate();
// Step 2 is extracting samples. Here we just take a few strings
// (at most 100). Adjust those params to your wish!
$$.Progress.message( "Getting random samples..." );
var options =
{
maxSampleSize:500, // No need to load 10K strings!
maxCount:100, // Max number of accepted strings (cf mySampleHandler.SIZE)
};
myTP.getSamples(options, mySampleHandler);
// Close the pb.
$$.Progress(false);
}
catch(e)
{
$$.receiveError(e);
}
// =============================================================================
// Unload the framework.
// ---
$$.unload();