Wednesday, September 14, 2005

 

More on Processing Text -- Finding a Library

I'm picking up where I left off yesterday. I took the results of yesterday's script and used it to create a text frame that contains a sample of each paragraph style, having redefined them to match what I want. So, the next step is to write the part of the script that actually assigns the paragraph styles to the text in the columns. We have to watch out for overset text when doing this.

But first, I need the code to go fetch the styles from the library. This is a pick-up from earlier work. It's a function I wrote that searches for a particular library by name, starting in the folder that holds the active document and working upwards until it either finds the library or reaches the top of the folder hierarchy. In the latter case, it gives the user the chance to open a library from anywhere; there follows some heavy duty checking to make sure that the selected file really is a library. For my purposes on this project, I'm always going to find the library, so I suppose I could create a shorter version, but what's the point?
function getProjectLibrary(libName) {
 var myDoc, myLib, myFolder, myLibFile, myLibName
 myDoc = app.activeDocument;
 try {
  myLib = app.libraries.item(libName);
  myLib.name;
  return myLib
 } catch (e) {
  // Library is not open; go find it and open it
 }
 try {
  myFolder = app.activeDocument.filePath;
 } catch (e) {
   myFolder = null;
 }
 while (myFolder != null) {
  if (File(myFolder.fsName + "/" + libName).exists) {
   app.open(File(myFolder.fsName + "/" + libName));
   return app.libraries.item(libName);
  } else {
   myFolder = myFolder.parent;
  }
 }
 while (true) {
  myLibFile = File.openDialog("Please locate a Library file to use");
  if (myLibFile == null) {
   // User canceled so bye-bye
   exit();
  }
  myLibName = myLibFile.name;
  // If name contains "Archive.indl" double-check with user
  if (myLibName.indexOf("Archive.indl") != -1) {
   if (!confirm("Selected library is an archive; continue anyway?")) {
    continue; // Ironic name for this command, given the question
   }
  }
  if (myLibName.indexOf(".indl") == -1) {
   if (!confirm("File does not have a '.indl' extension; continue anyway?")) {
    continue; // Ironic name for this command, given the question
   }
  }
  // If we get here, either the name is good or the user said to continue
  try {
   app.open(File(myLibFile.fsName));
   if (app.activeDocument != myDoc) {
    app.activeDocument.close(SaveOptions.no);
    throw("Sorry. The file you selected was not a library file.");
   }
  } catch(e) {
   throw("Sorry. The file you selected was not a library file.");
  }
  try { // Necessary in case user opened a book file
   myLib = app.libraries.item(libName);
   myLib.name;
   return myLib
  } catch (e) {
   throw("Sorry. The file you selected was not a library file.");
  }
 }
}
I'm not going to provide a lot of commentary about this function (I don't have time), but it does show-up one interesting difference between the file system object model and the InDesign object model: at the top of the file system hierarchy, if you try to get the parent of the top item, you get a null response while in the InDesign object model, the application is its own parent -- that means that in InDesign you always have to watch out for infinite loops when searching up the parental chain.

Comments: Post a Comment

<< Home

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