Saturday, December 17, 2005

 

Function Snippets

A Mac user, I can't begin to describe how useful iSnip is. I'm sure there must be similar utilities for Windows. Most of the snippets I keep are JavaScript related. Here are a couple of the more significant that are currently in my Functions folder:

Dumb Run Pages

function DumbRunPages(theDoc, theStory) {
  // What makes this "dumb" is that default master pages are used.
  var uRuler = theDoc.viewPreferences.rulerOrigin;
  theDoc.viewPreferences.rulerOrigin = RulerOrigin.spreadOrigin;
  while (theStory.textFrames[-1].overflows) {
    theDoc.documentPreferences.pagesPerDocument = theDoc.documentPreferences.pagesPerDocument + 1;
    var backPage = theDoc.pages[-1];
    app.activeWindow.activePage = backPage;
    backPage.appliedMaster = theDoc.pages[-2].appliedMaster;
    var myPbounds = backPage.bounds;
    var myNewTF = backPage.textFrames.add();
    if ((backPage.name % 2 == 1) || (!theDoc.documentPreferences.facingPages)) {
      myNewTF.geometricBounds =
        [myPbounds[0] + backPage.marginPreferences.top,
          myPbounds[1] + backPage.marginPreferences.left,
          myPbounds[2] - backPage.marginPreferences.bottom,
           myPbounds[3] - backPage.marginPreferences.right];
    } else {
      myNewTF.geometricBounds =
        [myPbounds[0] + backPage.marginPreferences.top,
          myPbounds[1] + backPage.marginPreferences.right,
          myPbounds[2] - backPage.marginPreferences.bottom,
          myPbounds[3] - backPage.marginPreferences.left];
    }
    myNewTF.itemLayer = theStory.textFrames[-1].itemLayer;
    myNewTF.previousTextFrame = theStory.textFrames[-1];
    myNewTF.textFramePreferences.textColumnCount = backPage.marginPreferences.columnCount;
    myNewTF.textFramePreferences.textColumnGutter = backPage.marginPreferences.columnGutter;
    if (myNewTF.characters.length == 0){
      theDoc.viewPreferences.rulerOrigin = uRuler;
      throw ("Permanently overset"); // This indicates a permanent overset condition so break out of loop
    }
  }
  theDoc.viewPreferences.rulerOrigin = uRuler;
}
I called this "dumb" to distinguish it from other scripts I've written where each master page had, in effect, a "next master" associated with it. That dates back to the days when I used to manage left/right masters to overcome the difficulties of bleeding on all four sides of the page when working with facing-pages documents. It's now over three years since I did any of that kind of work, so perhaps I shouldn't have called this function dumb at all. Mind you, within the context that I wrote this function, there were some smarts in the script that called this function to revisit the added pages and update the applied masters, so that too was part of the rationale for the name.

Basically, you call it with two parameters: the document you're working on and the story you care about. If that story is overset, then pages are added to the document until either it is not overset or until a permanent overset condition is detected (adding pages doesn't help because the story won't fit into the frames being added -- this might be for two reasons I'm aware of: (1) use of No Break making a line of text too wide (2) inclusing of an inline graphic that is too tall to fit in the added frames.

Get Project Library

function getProjectLibrary(libName) {
  try {
    var myLib = app.libraries.item(libName);
    myLib.name;
    return myLib
  } catch (e) {
    var myFolder = app.activeDocument.filePath;
    while (myFolder != null) {
    if (File(myFolder.fsName + "/" + libName).exists) {
      app.open(File(myFolder.fsName + "/" + libName));
      return app.libraries.item(libName);
    } else {
      myFolder = myFolder.parent;
    }
  }
  alert("Couldn't find library");
  exit();
  }
}
This too is a dumbed down version of a longer function that gives the user the opportunity to use the file manager to go find the library if it can't be found. But that other version is so long it makes all my scripts look somewhat overwhelming and so I usually prefer this simpler function that just gives up if it fails to find the library named in the call.

Why do I call it a Project library? Well, the concept is that your currently active document (there'd better be one when you call this function) is part of a project you're working on and the library for that project is either (a) already open or (b) somewhere in the folder hierarchy that leads to the active document. So, if the library is not open, it searches up the folder hierarchy until it either finds the libary or until it reaches the top.

A word of caution: most of the file systems that InDesign works with are case-independent but JavaScript is not, so if you get the case wrong in the name of the library file, the library may indeed be opened, but if you're using that other name to later try to access the opened library it won't work.

A second word of caution: when libraries appear in the palette, they do not display the .indl extension, but this is considered part of the name by JavaScript, so if you see "B-Library" in the palette, to access it you must use "B-Library.indl".

Comments:
Hi Dave,

I really like your DumbRunPages()-function, which helped a lot in some of my projects. Thanks for that!

Stephan
 
Post a Comment

<< Home

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