Skip to main content
Participating Frequently
January 11, 2010
Answered

Text or Grep in Find.change script by list?

  • January 11, 2010
  • 1 reply
  • 3943 views

Would like to use the grep formating in this query (instead of text) but the script shows error:

text {findWhat:" )"} {changeTo:")"} {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}

Why here coexist two ways of writing?

How to decide?

This topic has been closed for replies.
Correct answer Jongware

Jong:

Superb explanation!

I was afraid of mixing text/grep because I was getting inside darker waters: java scripting.

(that file text used to run the find.change by list is the proof...)

But was trying with grep as this can combine more characters in a search, for example finding space before point, period, semicolon, etc. Or space after ( or before)... Was trying to reduce too many operations in an script and pursuing a cleaner homework.

Thank you very much for your time.


@Laurent: heh heh. After that long story I was still paying attention to the original question...

Mariana:

... as this can combine more characters in a search, for example finding space before point, period, semicolon, etc. Or space after ( or before)...

Yes, that's possible with GREP. Try this for the first one:

grep {findWhat:" ([.,:;])"} {changeTo:"$1"} {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false} Remove space before period et al.

(Remember the script needs TABS between the sections, as in the original file.)

This one is a bit challenging. Actually, the period is a special character in GREp (it stands for "any character"), so normally you would need to escape it:

(space)\.

to search for a space followed by a period. However, the square brackets [...] define an OR group, and that removes the magic properties from the period. Apparently, there is no use for a wildcard character in an OR group! (That would be something like, "The following characters are allowed: a comma, a colon, any at all, a semi-colon ...")

There is a more advanced way to remove spaces like these. You want to look for spaces which are followed by any of these characters, and replace them with nothing. That would be

{findWhat:" (?=[.,:;])"} {changeTo:""}

You can try this in the interface to see it works.

---

The second can be done using the Unicode value for an ellipsis ("\x{2026}" -- another notation than in the regular Text find), or with the special escape code "~e". Yup -- there is another escape character. The backslash is used by GREP itself, but GREP does not understand InDesign's own special characters -- page breaks, smart single and double quotes, Indent-To-Here, em dashes, and a few others. They can also be inserted from the menu on the right side of the Find field. These characters need a tilde, so InDesign can "translate" them internally to something GREP can use, and GREP never sees them. Since the tilde is nothing special for Javascript, you can safely insert only one in the script data file ...

To remove a space before or after an ellipsis, you could use this:

grep {findWhat:" ?~e ?"} {changeTo:"~e"}

... but in this case it may again be simpler to stick to regular text mode (where you would use "^e" for the ellipsis, by the way).

1 reply

Participating Frequently
January 11, 2010

grep

Jongware
Community Expert
Community Expert
January 11, 2010

Why here coexist two ways of writing?

How to decide?

GREP does a lot more than plain text search-and-replace, and it uses meta-characters for that -- regular characters made special by GREP itself.

You might not be aware of this, but the regular text search also uses this. You cannot simply type a Tab or Return in the Find Text field, as that moves the cursor around and closes the dialog. Instead, you must use "^t" and "^p". Other examples are "^=" for an en dash, "^i" for an indent-to-here -- you can use the handy pop-up menu to the right of the Find and Replace text fields to insert them. How would you insert just a caret "^", if this character is 'special' in Find text? Simple: escape it with the designated escape character (which happens to be the same: "^", so you end up with two: "^^" searches for a single caret).

What does this have to do with your GREP find? The parentheses are special inside GREP expressions -- more specific, they are special only in find expressions, but do nothing in replace. So to search for a literal opening parenthesis, you have to escape it with GREP's own escape character: the backslash.

So use "\)" in the find text, plain ")" in the replace.

Am I done yet? Nope.

Because this file is read by Javascript, it must also obey to the internal rules of Javascript. Now in Javascript, that escape backslash is also special -- and it does the same as in GREP, forcing the next character to be read literally, instead of special. So to have the script actually read one single backslash, you must insert two backslashes in the find text.

"Now hold on", you might say, "in the examples in that file I see only one single backslash at points!" Ah. Well spotted. What actually happens is that the character combinations you see in there, "\t" and "\r", are expanded into their respective character codes "tab" and "return" on reading the script. As it happens, this is not a problem at all, because it feeds IDs GREP function with the character codes "[Tab]" and "[Return]", rather than their escaped variants. It's not a problem because (apparently!) ID can also work with the actual character codes, even though you cannot insert them from the keyboard.

If the original script writer had thought of inserting two backslashes everywhere you see one, it might have made a little bit more sense, because then it's easier to grasp the double-escaping; you write "\\r", InDesign loads the GREP with "\r", which is a hard return.

After this slight detour: which characters do you need to escape, and which ones not? Ehm. Here is a shortcut: "Every character that's not a-z or 0-9 is potentially a special character". That oughta cover about 90%, I guess. So if it doesn't work, add a backslash or two. You can always try the search expression in InDesign, and if it doesn't seem to work, add a backslash before suspicious characters. If it then does work, add two backslashes for each one in the script data file.

I probably should end on this note: there is no reason to use GREP to replace a space plus parenthesis with a single parenthesis. It works just fine in regular Text mode, no escapes needed or anything.

January 12, 2010

I probably should end on this note: there is no reason to use GREP to replace a space plus parenthesis with a single parenthesis. It works just fine in regular Text mode, no escapes needed or anything.

I particularly appreciate this conclusion