Thursday, September 15, 2005

 

Text Processing -- Fixing Styles

Because the intention of this script we're working on is to process an originally created document which has so far had nothing done to it except have a formatted table of data placed in it [thinks: I wonder if I could get by with unformatted; some of these table are huge so it's worth checking; all the "local formatting" is indicated by html tags, so we don't have to worry about that this time around -- that was not true last time). This still means that it has within it any default paragraph and character styles (also object styles, but I'm not using them, so they don't matter right now). What we need are the styles (more particularly, styles with the right names) that we created over the past couple of days. Remember: I saved them into a library item.

So, I wrote this function:
function fixStyles(theAsset) {
 // First, we clear out any existing paragraph or character styles
 var myStyles = myDoc.paragraphStyles; // myDoc is global
 try {
  for (var i = (myStyles.length -1); i > 0; i--) {
   myStyles[i].remove();
  }
 } catch (e) {} // CS2 can't delete [Basic Paragraph]
 myStyles = myDoc.characterStyles;
 for (i = (myStyles.length -1); i > 0; i--) {
  myStyles[i].remove();
 }
 app.select(null); // Make sure there is no selection
 var myTF = theAsset.placeAsset(myDoc)[0];
 myTF.remove();
}
This code is fairly self explanatory, but let me elaborate a little:
  1. The try construct when deleting paragraph styles is there because while InDesign CS allows all paragraph styles except [No Paragraph Style] to be deleted -- that's why the loop only goes down to item 1 -- CS2 also won't let you delete [Basic Paragraph]. So, when running this script with CS2, the last time around will create an error which is then ignored.
  2. For the character styles, we again avoid the first item because [No Character Style] also can't be deleted.
  3. Both these loops iterate backwards through the styles that's to avoid referring to an item that has already been removed -- by starting at the back end, this problem is avoided.
  4. Because of that, we don't actually need the myLim variable I usually create to control the loop because the first statement in the for loop control is only executed once; that's a good argument for always iterating backwards!
  5. The script makes sure there is no selection when placing the asset on the document because if there were a text selection at the time, that text would be obliterated and the reference to the placed asset wouldn't work.
  6. Because I just about never have multiple objects in the same library asset, I tend to forget that the results of placing an asset can lead to multiple items being added to the document. That's why I need to get the first item into the myTF reference so I can delete it after it has done its job (of bringing with it the paragraph and character styles that I need).
I expect to work more on this job later today.

Comments: Post a Comment

<< Home

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