Saturday, July 19, 2008

 

Delete "Empty" Pages

I often get into the state where a run of pages is dedicated to a particular story and because of editing the last pages of the story are now effectively empty; that is, the text frames on those pages are empty. This script seeks those pages and deletes them. So, the script must start with a framework that requires the selection to have a parentStory property:

//DESCRIPTION: Delete "empty" pages at end of selected story

(function() {

 if (app.documents.length > 0 &&
  app.selection.length == 1 &&
   app.selection[0].hasOwnProperty(
"parentStory")) {
  deleteEmptyPages(app.selection[0].parentStory);
 }
 
function deleteEmptyPages(story){
 }
}())

This is the first time I've used an anonymous function on this blog. I learned about them only recently (thanks Harbs and Kris). They help protect the global name space. In particular, you can have one script in this structure call another that uses the same variable or function names and there's no confusion because they're inside the anonymous function. Notice that the whole thing is inside parentheses. The pair of parentheses right at the end are the call to the function. Looks odd, but it works a charm.

So, now all we need do is write our function. This is a CS3 only script so we must use the textContainers property to get at the frames that constitute the story. Here's the function:

 
function deleteEmptyPages(story){
  var myFrames = story.textContainers;
  
for (var j = myFrames.length - 1; j >= 1; j--) {
   
if (myFrames[j].insertionPoints.length == 0 &&
    myFrames[j].parent
instanceof Page) {
    myFrames[j].parent.remove();
   }
  }
 }
Notice that the loop goes down to a value of 1. By doing this, it assumes that you don't want to completely delete the story even if it is empty. Notice also that it checks that the parent is indeed a page before deleting it. We don't want to delete a page where the frame, albeit empty, has been grouped with something else.

Comments:
very good script :-)
it's possible to create script to erase empty frames with dialog box. all textFrames, with color fill or not, style object, all pages or only current pages.
i work on it based with GREP and textFrames.erase(). but not success because i'm novice in Indesign scripting. it's be an idea of Script of the day :-).
sincerely.
Jul.
 
Post a Comment

<< Home

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