Thursday, December 15, 2005

 

Replace with Clipboard

One oft-requested feature for InDesign is the ability to replace found text with the current contents of the clipboard. It came to me this morning that this is a case where the UI can be used to set-up a script, and consequently, the script itself is very simple.

The standard advice for a scripter doing a search is to use:
app.findPreferences = null;
app.changePreferences = null;
before doing a search. This clears out any settings left over from the last time that Find/Change was used. But what if you want to use the search options that are already there? Well, this can certainly be done using the following workflow:
  1. Open the Find/Change dialog and set-up a search of whatever complexity you wish.
  2. Click Done. Or, move the dialog aside.
  3. Run the following script.
app.search();
And that's it. But not really all that useful in this form because you could as easily have achieved this result by simply clicking Change All.

But what is not available in Find/Change is the ability to replace the found text with the current contents of the clipboard. So, let's modify our workflow:
  1. Copy to the clipboard that which you wish to change the found text into.
  2. Open the Find/Change dialog and set-up a search of whatever complexity you wish.
  3. Click Done. Or, move the dialog aside.
  4. Run the following script.
myFinds = app.search() 
for (j = myFinds.length - 1; j >= 0; j--) {
  app.select(myFinds[j]);
  app.paste();
}
This version assumes that you used the dialog to set-up the change-side formatting. But if that is implicit in the contents of the clipboard, then you should run this minor variation of the script:
app.changePreferences = null;
myFinds = app.search()
for (j = myFinds.length - 1; j >= 0; j--) {
  app.select(myFinds[j]);
  app.paste();
}
This version wipes out any settings on the change side before running the search.

So, when might you use this script? Two possibilities leap to mind:
  1. If you want to replace with an alternate glyph that can't be inserted into the Change side in the Find/Change dialog.
  2. If you want to replace text with an inline graphic or frame of some kind.
Note that this second possibility allows you to replace bullets in bulleted paragraphs with an icon of some kind provided that the bullets were actually typed and not created by the Bulleted List feature of paragraph styles. Although, don't overlook the fact that you can convert such bullets to text using the contextual menu when appropriate text is selected.

Comments:
Really useful script, ideal for adding anchored frames in a catalogue I'm working on. Only problem is that the 'Search' field in the Find/replace dialogue always reverts to Document, rather than story, selection or whatever I've selected. This means that it becomes difficult to re-run the script for new sections of text. Or am I just doing it wrong?
 
I suspect that this is caused by the way that Find/Change works in CS2.

This script is obsoleted by CS3; I'd recommend upgrading if this feature is important to you.
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?