Saturday, December 17, 2005

 

Breaking a Text Reference

When I wrote this code, it crossed my mind that it might not work. It didn't take long to discover that it doesn't.
function processFoundClone(theText, theAsset) {

  // [snip]

  // Check to see if asset already there, if so replace it, if not add it
  if (theText.characters[0].groups.length > 0) {
    theText.characters[0].remove();
  }
  app.select(theText.insertionPoints[0]);
  theAsset.placeAsset(app.activeDocument);
}
The problem is that if the first character contains (consists of?) an anchored clone already, I want to replace it with the new one from the library (that's what the parameter theAsset is all about). But, if I delete it in the way shown here, the reference to the text breaks and so I'm unable to select that insertion point.

It's possible that this is breaking only because the paragraph I happen to be working with is the last in its story and so by deleting the character I'm causing the text reference to point beyond the end of the story. If that's the cause, then fixing this is easy: put the new one in and then take the old one out. Let me try that ...

Well what do you know! How's this for real-time blogging. That was the problem and this reorganization fixes it:
function processFoundClone(theText, theAsset) {

  // [snip]

  // Add new asset
  app.select(theText.insertionPoints[0]);
  theAsset.placeAsset(app.activeDocument);
  // Check to see if there was one already there; if so, delete it
  if (theText.characters[1].groups.length > 0) {
    theText.characters[1].remove();
  }
}
Notice that because I added a character at the beginning of theText, I now have to check to see if the second character contains a group, and if so delete that. Because the story was first made longer, the reference to theText is still valid (albeit it doesn't point at the exact same set of characters) whereas removing the character first invalidated it.

Comments: Post a Comment

<< Home

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