Reply To: GREP search to increment numbers?

Home Page / Forums / General InDesign Topics (CLOSED) / GREP search to increment numbers? / Reply To: GREP search to increment numbers?

#54206

Hank, I don't have the remotest idea how you think it would add 1 to any of these numbers …

David is right, of course — GREP Is Not Magic. It cannot do calculations, nor can it search an item from a list[*] and replace it with an equivalent from another list. You cannot have a similar list in the replace field.

[*] Well, it can, using the OR | operator, but “Replace” doesn't know which item it found.

Also, you can enter a 'range' in the Find field in the form [a-z] — this will find anything from 'a' to 'z'. But GREP does not know maths — you cannot search for [69-82]. (Go ahead, try. You will see it treats this as '6', '9-8', and '2' — and its anybodies guess what it will do with the malformed range in the middle.)

A script to search and change the numbers in your range could be something like this (a rough attempt but it seems to work):

//DESCRIPTION: Change some numbers and leave some else. In a selection as well.

// A Jongware production. No guarantees.

var ranges = [

[54, 103, 1],

[104, 178, 2]

];

// step 0: verify selection

if (app.selection.length != 1)

{

alert (“Well, I warned you to select something!”);

exit();

}

// step 1: find ALL numbers

app.findChangeGrepOptions = null;

app.findGrepPreferences = null;

app.findGrepPreferences.findWhat = “\d+”;

numberList = app.selection[0].findGrep(true);

// alert ('found '+numberList.length);

// step 2: change certain ranges

for (a=0; a<numberList.length; a++)

{

thisVal = Number(numberList[a].contents);

for (b=0; b<ranges.length; b++)

{

if (thisVal >= ranges[b][0] && thisVal <= ranges[b][1])

{

numberList[a].contents = String(thisVal+ranges[b][2]);

break;

}

}

}

// step 3: … erm. That's all!

(David, how to format code?)

This article was last modified on December 10, 2009

Comments (0)

Loading comments...