Wednesday, October 26, 2005

 

Page Numbers are Squirrelly

The name of a page is a string and might include the section prefix, so subtracting 1 from it might fail.

It depends, I think, on three things:
  1. The state of the page numbering option in general preferences.
  2. The presence of a section prefix in the Section and Numbering Options panel.
  3. The state of the Include Prefix in Page Numbers option in the same panel.
Some scripts will clarify. This one sets the page numbering option and then displays an alert that cofirms what you did:
app.generalPreferences.pageNumbering = [PageNumberingOptions.section,
  PageNumberingOptions.absolute][0];
alert(decodePageNumbering(app.generalPreferences.pageNumbering,true));

function decodePageNumbering(theCode,verbose) {
 var theCodes = [[1096971116, ["absolute", "absolute numbering"]],
  nbsp;[1935897710, ["section", "section numbering"]]];
 var verbosePtr = 0;
 if (verbose) { verbosePtr = 1 }
 switch (theCode) {
  case PageNumberingOptions.absolute :
   return theCodes[0][1][verbosePtr];
  case PageNumberingOptions.section :
   return theCodes[1][1][verbosePtr];
  default :
   throw (String(theCode) + "not recognized")
 }
}
You can try both settings by changing the [0] to [1] at the end of the first statement (on the second line).

OK, so now let's try this script (with a new, single-page document open):
app.generalPreferences.pageNumbering = [PageNumberingOptions.section, 
  PageNumberingOptions.absolute][0];

var myPage = app.activeDocument.pages[0];
myPage.appliedSection.sectionPrefix = "Test-";
myPage.appliedSection.includeSectionPrefix = true;
alert(myPage.name);
and you'll quickly find that toggling the state of the page numbering options (by changing that [0] to [1] again) has no effect at all on what the alert says. So much for my assertion 1 above. I think that what that option does is determine the form you must use when you supply a page number to certain methods, for example if you want to export a pdf or print a page range from your document. Otherwise, it has no effect on working with page numbers.

Another test:
var myPage = app.activeDocument.pages[0];
myPage.appliedSection.sectionPrefix = "Test-";
myPage.appliedSection.includeSectionPrefix = false;
alert(myPage.name);
This gives you an alert that just says 1

But is it a number?
var myPage = app.activeDocument.pages[0];
myPage.appliedSection.sectionPrefix = "Test-";
myPage.appliedSection.includeSectionPrefix = false;
alert(myPage.name.constructor.name);
No it isn't; it's a string. But what if we try to treat it as a number?
var myPage = app.activeDocument.pages[0];
myPage.appliedSection.sectionPrefix = "Test-";
myPage.appliedSection.includeSectionPrefix = false;
alert(myPage.name - 1);
Well, what do you know, it works! This is one of those things about JavaScript that on the one hand allows you to be lazy; after all, most people have a prefix of "" in most of their single-section documents because that's the default when a new document is created, and so most scripts that just treat the page name as a number just work. And that leads you down the path of thinking that the page name is a number -- but it isn't. It's a string, that most of the time contains the string representation of a number, so most of the time a script that thinks it's a number actually does work.

But it has a bug as big as your nose right there staring you in the face and you won't ever find it until you start working with multi-section documents or you take advantage of the section prefix in one of your documents.

Comments: Post a Comment

<< Home

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