Sunday, November 20, 2005

 

En space after Period

I've been toying with writing this script all day, and I finally got fed up with doing this manually and broke down. I'm working on a document where certain paragraphs require an en space after the first period, but the Word documents from the client have a regular space there. Here's the script:
//DESCRIPTION: Utility to insert en space after first period in each para of selection

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 != 0)) {
  var mySel = app.selection[0];
  if   (mySel.isPureText) {
    var myParas = mySel.paragraphs;
    for (var j = myParas.length - 1; j >= 0; j--) {
      var myInd = myParas[j].contents.indexOf(". ");
      if ( myInd != -1) {
        myParas[j].characters[myInd + 1].contents = SpecialCharacters.enSpace;
      }
    }
  }
} else {
  errorExit();
}

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

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.
}
There are probably more elegant ways of doing this; perhaps even more efficient; but I'm operating on selections and they're never likely to be more than a few paragraphs.

Comments: Post a Comment

<< Home

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