Saturday, October 01, 2005
Regex Tester Revisited
It has come to me that for this script to be really useful, it needs to remember the previous version of your Regex find string and use it from run to run so you can refine the string until you get it the way you really want it.
So, where to keep it between runs? I'm thinking perhaps as a label to the document you're working on or perhaps the object containing the text you're working on.
Thinks: do stories support labels? Yes, but text in cells don't. On the other hand, text in cells have parent stories, so that's one possibility. While in CS2, cells support labels.
So many choices!
Perhaps to get started I should just use the document itself. After all, you're not likely to be working on more than one of these at any particular time, and when you've got it right you can call the script one last time and copy it out of the prompt to where you really need it (probably in some other script).
OK, but let's not screw up the possibility that you are already using the document label, so I'll use a keyed label. I'll give it the key "PDSregex".
Changing the script is pretty simple. Once it is determined that there is an active document, I can create a reference to it:
So, the first time you use the script with a particular document open, you'll get a blank field to start your Regex string, but after that, each time you run the script it will pick up the most recent version of your string and you can continue refining it until you get it right!
I've updated the linked script to include this logic.
So, where to keep it between runs? I'm thinking perhaps as a label to the document you're working on or perhaps the object containing the text you're working on.
Thinks: do stories support labels? Yes, but text in cells don't. On the other hand, text in cells have parent stories, so that's one possibility. While in CS2, cells support labels.
So many choices!
Perhaps to get started I should just use the document itself. After all, you're not likely to be working on more than one of these at any particular time, and when you've got it right you can call the script one last time and copy it out of the prompt to where you really need it (probably in some other script).
OK, but let's not screw up the possibility that you are already using the document label, so I'll use a keyed label. I'll give it the key "PDSregex".
Changing the script is pretty simple. Once it is determined that there is an active document, I can create a reference to it:
if ((app.documents.length != 0) && (app.selection.length != 0)) {And then, the part that issues the prompt needs a couple of extra lines:
var myDoc = app.activeDocument;
// Invite user to type regular expression
var curExp = myDoc.extractLabel("PDSregex");
app.activate();
var myREtext = prompt("Type your regular expresion",curExp);
if (myREtext == null) { errorExit() }
myDoc.insertLabel("PDSregex",myREtext);
So, the first time you use the script with a particular document open, you'll get a blank field to start your Regex string, but after that, each time you run the script it will pick up the most recent version of your string and you can continue refining it until you get it right!
I've updated the linked script to include this logic.