2 Replies Latest reply on Dec 23, 2011 12:32 PM by Dan Macy

    Font unavailable in script

    Dan Macy Level 1

      I have written the following script to generate a font index in an InDesign document:

       

      // Generate new document

      var doc = app.documents.add();

      var frm = doc.pages[0].textFrames.add();

      var w = doc.documentPreferences.pageWidth;

      var h = doc.documentPreferences.pageHeight;

      frm.geometricBounds = [0.5, 0.5, 10.5, 8];

       

      // Get list of all installed fonts and split into font family name and style arrays

      var appFonts = app.fonts.everyItem().name;

      var installedFonts = [];

      var installedStyles = [];

      var counter = -1;

      for (var i = 0; i < appFonts.length; i++) {

          appFonts[i] = appFonts[i].split("\t");

          if (i == 0 || appFonts[i][0] != appFonts[i-1][0]) {

              installedFonts.push(appFonts[i][0]);

              counter ++;

              installedStyles[counter] = [];

              }

          installedStyles[counter].push(appFonts[i][1]);

          }

       

      // Loop on all font families and styles

      var thisFont = "";

      var thisStyle = "";

      for (var i = 0; i < installedFonts.length; i++) {

          thisFont = installedFonts[i];

         

          for (var j = 0; j < installedStyles[i].length; j++) {

              thisStyle = installedStyles[i][j];

             

              frm.contents += thisFont + "-" + thisStyle + "\r";

              with (frm.paragraphs.item(-1)) {

                  try {

                      appliedFont = thisFont;

                     

                      try {

                          fontStyle = thisStyle;

                          }

                     

                      catch (errorVariable) {

                          appliedFont = "Times New Roman";

                          fontStyle = "Regular";

                          }

                      }

                 

                  catch (errorVariable) {

                      appliedFont = "Times New Roman";

                      fontStyle = "Regular";

                      }

                 

                  pointSize = 12;

                  justification = Justification.leftAlign;

                  }

              }

          }

       

      I added the try/catch statements because for certain fonts the script was aborting on the statement:

       

      appliedFont = thisFont;

       

      because it said the font was unavailable. Why can't I apply that font to some text if app.fonts.everyItem().name included it in the list?