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":
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):
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
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 FileThe 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).
myDoc = app.activeDocument;
var myFldrName = myDoc.filePath;
var myTextName = myDoc.name.split(".indd").join("Styles.txt");
var myFile = File(myFldrName + "/" + myTextName);
myFile.open("w");
myFile.write("Paragraph Styles " + myDoc.paragraphStyles.length + "\n");
myFile.write(myDoc.paragraphStyles.everyItem().name.join("\n"));
myFile.write("\n\nCharacter Styles " + myDoc.characterStyles.length + "\n");;
myFile.write(myDoc.characterStyles.everyItem().name.join("\n"));
myFile.write("\n\nObject Styles " + myDoc.objectStyles.length + "\n") ;
myFile.write(myDoc.objectStyles.everyItem().name.join("\n"));
myFile.close();
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 139So, 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.
[No Paragraph Style]
[Basic Paragraph]
AssessNL
BasicSkillBL
BasicSkillHd
[snipped 132 style names]
~Footer-Recto
~Footer-Verso
Character Styles 46
[None]
Activity_Cont
BasicSkillLeadoff
Bold
[snipped 40 style names]
~FooterFolio
~FooterTriangleDownshift
Object Styles 12
[None]
[Normal Graphics Frame]
[Normal Text Frame]
[Normal Grid]
FeatheredRoundedImage
OutlinedRndCrnrImage
FrameImageInGallery
Note
ReviewAssessmentFrame
MathHolder
ProcedureHolder
SpicesGalleryImages
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(myDoc.name + "\nParagraph Styles " + myDoc.paragraphStyles.length + "\n");
Comments:
<< Home
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 !
thanks!
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 !
thanks!
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.
Dave
Post a Comment
Visit the Adobe U2U InDesign Scripting forum. There's been some dicussion on listing document styles there in the past week or two.
Dave
<< Home