Friday, January 11, 2008

 

Speeding up a Script

Most of the time, I pay scant attention to how long a script takes to run because most of them are so quick it hardly matters, but every now and then a simple script comes along that takes serious time to run. I just hit one as I sit here working on a catalog. I'm getting near the end of the job, making corrections and changes (mainly changes).

I realized that I've done so much dickering around to get things to fit on the page in earlier passes that it's time to reset the space before/after of every paragraph. Well, that's easy:
//DESCRIPTION: Restore vertical paragraph style spacing

if (app.documents.length > 0 &&
app.selection.length == 1 &&
app.selection[0].hasOwnProperty("baseline")) {
restoreParagraphSpacing(app.selection[0]);
}

function restoreParagraphSpacing(myText) {
var myParas = myText.paragraphs;
for (var j = 0; myParas.length > j; j++) {
myParas[j].spaceBefore = myParas[j].appliedParagraphStyle.spaceBefore;
myParas[j].spaceAfter = myParas[j].appliedParagraphStyle.spaceAfter;
}
}
If I were just running this on the odd page here and there, it wouldn't take too long, but document has 43 pages and the story has 782 paragraphs, so this loop interacts with InDesign 1564 times.

Clearly, we could cut that in half by changing the loop to this:
  for (var j = 0; myParas.length > j; j++) {
myParas[j].properties = {
spaceBefore:myParas[j].appliedParagraphStyle.spaceBefore,
spaceAfter:myParas[j].appliedParagraphStyle.spaceAfter
}
}
But this still requires 782 interactions with InDesign. And all of them happen even if the script does nothing because I'm running it for a second time. There's one more thing worth trying:
//DESCRIPTION: Restore vertical paragraph style spacing

if (app.documents.length > 0 &&
app.selection.length == 1 &&
app.selection[0].hasOwnProperty("baseline")) {
restoreParagraphSpacing(app.selection[0]);
}

function restoreParagraphSpacing(myText) {
var myParas = myText.paragraphs;
var mySBs = myParas.everyItem().spaceBefore;
var mySAs = myParas.everyItem().spaceAfter;
for (var j = 0; myParas.length > j; j++) {
if (myParas[j].spaceBefore != mySBs[j] || myParas[j].spaceAfter != mySAs[j]) {
myParas[j].properties = {
spaceBefore:myParas[j].appliedParagraphStyle.spaceBefore,
spaceAfter:myParas[j].appliedParagraphStyle.spaceAfter
}
}
}
}
The script still does a lot of interacting, but most of it is reading. It only writes a value into the document if there is a change needed, so right now the script runs very quickly because there are no longer any changes to be made.

Ah well, back to work!

Comments:
Hello Dave,

this is an interesting approach.
But I think you should compare spaceBefore/spaceAfter of the appliedParagraphStyles of the paragraphs with the collected values of spaceBefore/spaceAfter of your selected paragraphs instead of comparing spaceBefore/spaceAfter of the selected paragraphs with the collected values of spaceBefore/spaceAfter of your selected paragraphs.

The condition would be true every time if you compare the actual values with the collection of the actual values. ;-)

---
Sometimes I change the values of glyphScaling and wordSpacing and sometimes I have to restore the values like you did with spaceBefore and spaceAfter. So I have adepted your script for my request in this way:

//--------------------------------------
if ( app.documents.length > 0 &&
app.selection.length == 1 &&
app.selection[0].hasOwnProperty( "baseline" ) )
{
restoreGlyphScaling( app.selection[0] );
restoreWordSpacing( app.selection[0] );
}

function restoreGlyphScaling( myText )
{
var myParas = myText.paragraphs;
var myMiniGS = myParas.everyItem( ).minimumGlyphScaling;
var myDesiredGS = myParas.everyItem( ).desiredGlyphScaling;
var myMaxiGS = myParas.everyItem( ).maximumGlyphScaling;
for ( var j = 0; myParas.length > j; j++ )
{
if ( myParas[j].appliedParagraphStyle.minimumGlyphScaling != myMiniGS[j] ||
myParas[j].appliedParagraphStyle.desiredGlyphScaling != myDesiredGS[j] ||
myParas[j].appliedParagraphStyle.maximumGlyphScaling != myMaxiGS[j] )
{
myParas[j].properties =
{
minimumGlyphScaling:myParas[j].appliedParagraphStyle.minimumGlyphScaling,
desiredGlyphScaling:myParas[j].appliedParagraphStyle.desiredGlyphScaling,
maximumGlyphScaling:myParas[j].appliedParagraphStyle.maximumGlyphScaling,
}
}
}
}

function restoreWordSpacing( myText )
{
var myParas = myText.paragraphs;
var myMiniWS = myParas.everyItem( ).minimumWordSpacing;
var myDesiredWS = myParas.everyItem( ).desiredWordSpacing;
var myMaxiWS = myParas.everyItem( ).maximumWordSpacing;
for ( var j = 0; myParas.length > j; j++ )
{
if ( myParas[j].appliedParagraphStyle.minimumWordSpacing != myMiniWS[j] ||
myParas[j].appliedParagraphStyle.desiredWordSpacing != myDesiredWS[j] ||
myParas[j].appliedParagraphStyle.maximumWordSpacing != myMaxiWS[j] )
{
myParas[j].properties =
{
minimumWordSpacing:myParas[j].appliedParagraphStyle.minimumWordSpacing,
desiredWordSpacing:myParas[j].appliedParagraphStyle.desiredWordSpacing,
maximumWordSpacing:myParas[j].appliedParagraphStyle.maximumWordSpacing,
}
}
}
}
//--------------------------------------


Thanks
Martin
 
Hey,

I have been trying to dowload MakeBooklet.zip for CS3 but can't get a connection to the server.

/Bastian
 
Very interesting and awesome approach. I got huge of important idea from your post. A big thanks for sharing with us !!
 
Am so late to your blog. Your post is so helpful.


background removal service
 
Really you create a amazing professional tutorial. Thanks for sharing.

clipping path service
deep etching
photo manipulation service
 
Post a Comment

<< Home

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