Monday, August 29, 2005


Script of the Day -- List Document Styles

Just a quickie this morning because I'm up to my eyes in deadlines. However, even today, I find myself banging out a quick script to address an immediate problem:

Because my workflow has at its base the much-frowned-upon technique of basing each document in a series the previous member of the series, I don't have a central template for my current book project. I also tend to "clean up" the documents when I finish them -- something I mentioned yesterday. Well, this cleaning up process can cause valuable styles to disappear if a particular chapter doesn't happen to use it. So I need a quick reference of the styles used by each chapter. [Of course, I could just open each chapter and look in its styles palettes, but opening each takes a while when your documents are as complex as the ones I'm producing and anyway, what I really need is to get the lists all into one place.]

So, as a starting point, I created this quickie script to export all the style names of a document to a text file in the same folder as the document. For a document named "MyDocument.indd", the script produces a text file named "MyDocumentStyles.txt":
//DESCRIPTION: Export Style Names to Text File

myDoc = app.activeDocument;
var myFldrName = myDoc.filePath;
var myTextName =".indd").join("Styles.txt");
var myFile = File(myFldrName + "/" + myTextName);"w");
myFile.write("Paragraph Styles " + myDoc.paragraphStyles.length + "\n");
myFile.write("\n\nCharacter Styles " + myDoc.characterStyles.length + "\n");;
myFile.write("\n\nObject Styles " + myDoc.objectStyles.length + "\n") ;
The first part of this script shows how to manipulate folder and file names. We then create a file object (at the point we do that, the file itself might or might not exist -- when we open it, if it exists, we open it; if it doesn't exist, we get a new file created for us).

By opening the file for write access (that's what the "w" argument means), we overwrite anything that might already be in the file. If we wanted to append to existing data, we would open it for editing using an "e" argument, while "r" opens for reading.

There is a possible point of confusion at this stage (and indeed, I might be confused myself). When writing to text files, the "\n" construction produces a new line while when working with InDesign text it would produce a forced line break.

As a test, I ran the script against the current document I'm working on and it indeed produced a text file named CulArtsChapter10Styles that looks like this (I've snipped out a lot of the detail):
Paragraph Styles 139
[No Paragraph Style]
[Basic Paragraph]

[snipped 132 style names]


Character Styles 46

[snipped 40 style names]


Object Styles 12
[Normal Graphics Frame]
[Normal Text Frame]
[Normal Grid]
So, there you have it. A quick script that should save me a lot of hunting around through completed documents. One thing you'll notice about these "quick" scripts is that they don't put a lot of stock in error checking. If I try to run this script with no document open or with an untitled document, I'll get a run time error. So be it.

By way of a postscript: how much better the output would have been had it identified the document? Geez, how easy it is to overlook simple stuff. Obviously, that first write statement should be
myFile.write( + "\nParagraph Styles " + myDoc.paragraphStyles.length + "\n");

This is great! Just what I was looking for.

One question, I can't get the script to list Style Groups within the paragraph styles. I have several of those set up for special tables, etc.

Any help would be appreciated !

As you can see from the date of the entry, this is a CS or CS2 script. With CS3 and later, things became more complicated.

Visit the Adobe U2U InDesign Scripting forum. There's been some dicussion on listing document styles there in the past week or two.

Post a Comment

<< Home

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