Tuesday, January 31, 2006

 

Subselecting a quote

My client sent me a set of corrections in this form:

Correct Figure Legend should read “Revised text for figure legend”

Have you ever tried selecting text between quotes? If the text is large enough, it's not that big a deal, but when you want to keep as much information on screen as possible, it is very fiddly. So, I wrote this quick script (would have been quicker had I realized that I needed the parent text flow, not the parent story -- the information was inside a table):
//DESCRIPTION: Subselect to within first pair of quotes

Object.prototype.isPureText = function() {
  switch(this.constructor.name){
    case "InsertionPoint":
    case "Character":
    case "Word":
    case "TextStyleRange":
    case "Line":
    case "Paragraph":
    case "TextColumn":
    case "Text":
      return true;
    default :
      return false;
  }
}

if ((app.documents.length != 0) && (app.selection.length == 1)) {
  var mySel = app.selection[0];
  if (!mySel.isPureText()) { errorExit("Please select some text.") }
  app.findPreferences = null;
  app.changePreferences = null;
  var myStart = mySel.search("^{",false,false)[0].index + 1
  var myEnd = mySel.search("^}",false,false)[0].index - 1
  var myStory = getParentTextFlow(mySel);
  app.select(myStory.characters.itemByRange(myStart,myEnd));
} else {
  errorExit();
}

// +++++++ Functions Start Here +++++++++++++++++++++++

function getParentTextFlow(theTextRef) {
// Returns reference to parent story or text of cell, as appropriate
  if (theTextRef.parent.constructor.name == "Cell") {
    return theTextRef.parent.texts[0];
  } else {
    return theTextRef.parentStory;
  }
}

function errorExit(message) {
  if (arguments.length > 0) {
    if (app.version != 3) { beep() } // CS2 includes beep() function.
    alert(message);
  }
  exit(); // CS exits with a beep; CS2 exits silently.
}
So, all I have to do is select the paragraph (or the cell contents) and the script sub-selects for me.

You'll notice that I've done no error-checking. If the quotes aren't there, the script will give a run-time error. I'll leave that as an exercise for the reader!

Also, of course, it is trivially easy to change this script to sub-select between the first occurrences of any two different characters.

Comments: Post a Comment

<< Home

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