If you’re a guest at someone’s house, wouldn’t you agree that it is bad manners to leave a mess behind you when you go?
Likewise, when a user runs your script, I’m sure you’d agree that the user shouldn’t have to tidy up after running your script!
So here’s one easy way of being a politer scripter: reset the find/replace dialog back to how it was before you script quits. Explanations follow.
InDesign scripting frequently involves executing text and GREP find/changes. Scripting a find/change operation in InDesign seems to be hard-wired to the find/change UI. I’m not sure why this is. I don’t think there’s any advantage to it really, and probably it is something Adobe should fix.
For example, if your script runs the following command to clear all find/change options:
app.findTextPreferences = app.changeTextPreferences = null;
this will be reflected in the find/change UI when the script finishes execution.
So if the user happens to have set up a complicated find/change operation in the UI, and they run your script, they’ll have to build that complicated find/change operation again.
In general, whatever happens to be the last find/change query your script has executed before it quits, it will show up in the find/change UI, forcing the user to have to delete all those settings before they can apply their own.
It’s not the end of the world of course – it doesn’t take long to clear the find/change dialog (and a while back InDesignSecrets.com published a tip of mine here about how to do that very easily), but since it is so simple for a script to tidy up after itself and leave no traces of its find/change operations when it quits, it seems a basic courtesy to do so.
This is how it’s done.
First, before your script does any searching, save the current state of the find/change UI:
app.saveFindChangeQuery("ScriptAutoGeneratedQuery", SearchModes.TEXT_SEARCH);
(This needs to be done for each tab in the UI: Text, GREP, Glyph, Object, and (if you’ve got the Middle Eastern version) Transliterate.)
You can call the query anything you like, of course. Probably some error checking is in order in case (longshot) that precise name exists already.
Then, before the script quits, simply load the query back up into the UI:
app.loadFindChangeQuery("ScriptAutoGeneratedQuery", SearchModes.TEXT_SEARCH);
To score bonus points, delete your query altogether before exiting:
app.deleteFindChangeQuery("ScriptAutoGeneratedQuery", SearchModes.TEXT_SEARCH);
Quick and simple! When the user next opens the find/change dialog, your script, like the model guest, will have left no traces.
Now I’ve just got to start practicing what I preach!
Leave a Comment