Thursday, August 2, 2018

Script of the Day List Document Styles

Script of the Day List Document Styles


Just a quickie this morning because Im 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 dont 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 doesnt 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 Im 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 = myDoc.name.split(".indd").join("Styles.txt");
var myFile = File(myFldrName + "/" + myTextName);
myFile.open("w");
myFile.write("Paragraph Styles " + myDoc.paragraphStyles.length + " ");
myFile.write(myDoc.paragraphStyles.everyItem().name.join(" "));
myFile.write(" Character Styles " + myDoc.characterStyles.length + " ");;
myFile.write(myDoc.characterStyles.everyItem().name.join(" "));
myFile.write(" Object Styles " + myDoc.objectStyles.length + " ") ;
myFile.write(myDoc.objectStyles.everyItem().name.join(" "));
myFile.close();
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 doesnt exist, we get a new file created for us).

By opening the file for write access (thats 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 " " 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 Im working on and it indeed produced a text file named CulArtsChapter10Styles that looks like this (Ive snipped out a lot of the detail):
Paragraph Styles 139
[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
So, there you have it. A quick script that should save me a lot of hunting around through completed documents. One thing youll notice about these "quick" scripts is that they dont put a lot of stock in error checking. If I try to run this script with no document open or with an untitled document, Ill 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(myDoc.name + " Paragraph Styles " + myDoc.paragraphStyles.length + " ");


go to link download