7 Replies Latest reply: Sep 15, 2009 8:30 AM by Marc Autret RSS

    Font Replacement

    jeffmlevy

      I hope there is an easy solution to this problem.

       

      I've got some 200 InDesign documents that are "missing" fonts.

       

      When I iterate over document.fonts, I look into the status attribute, and if I find a font to be "NOT AVAILABLE", I would like to replace it with another.

       

      Thusfar, I have been unable to find a simplistic means by which to accomplish this.

       

      Pointers / advice?

       

      Thanks.

        • 1. Re: Font Replacement
          AdobeScripts Community Member

          hi

           

          first you need to iterate CharacterStyles and ParagraphStyles collections and find these missing fonts in definitions and change it

          then you need to check again document's font collection and if you still have missing status in any font - you need to use Search&Replace to replace all what left in text

           

          robin

           

          www.adobescripts.co.uk

          • 2. Re: Font Replacement
            jeffmlevy Community Member

            If it wouldn't be too terrible an imposition, an example (in js) would be most appreciated.

             

            I believe I have already tried an iteration of myDoc.characterStyles, or some derivation thereof. What I found was that the .appliedFont attribute returned a string, null in the case of my missing fonts, and I abandoned that approach. I have since gone off in another direction,

             

            app.findTextPreferences.appliedFont = Font; // where Font is the actual Font object as returned by a loop of myDoc.fonts. I test for .status == 'NOT_AVAILABLE'

            app.changeTextPreferences.appliedFont = 'Arial';

            myDoc.changeText();

             

            ... sadly, this seems to have failed, as well.

             

            Really, I'm at a loss what to do here. A practical examle would be most appreciated.

            • 3. Re: Font Replacement
              jeffmlevy Community Member

              OK. Here's what I know so far.

               

              My document has 2 text boxes in it. 1st text box has some text set to Aachen Bold. 2nd Text box has some text set to Arial (OTF).

               

              NOTE: I have moved Arial (OTF) out of my font path so InDesign Server loses track of it.

               

              doc.paragraphStyles.length = 2 // ok, that makes sense.

              doc.characterStyles.length = 1 // unsure what it should be, but 1 doesn't seem right to me.

               

              I execute this loop,


              for(j=0;j<doc.paragraphStyles.length;j++)

              {

                  alert(doc.paragraphStyles[j].appliedFont.name;

                        doc.paragraphStyles[j].appliedFont = app.fonts[1];

              }


              doc.close(SaveOptions.YES, doc.fullName)

               

              I see Times New Roman, Aachen Bold, and that's it. Nothing else shows up. I think I see Times New Roman, because that's what the text was originally typeset as. Arial OTF doesn't register in paragraphStyles (so, how am I supposed to change it?)

               

              So, I then did the same loop over characterStyles, thus,

               

              for(j=0;j<doc.characterStyles.length;j++)

              {

                  alert(doc.characterStyles[j].appliedFont.name;

                        doc.characterStyles[j].appliedFont = app.fonts[1];

              }


                  doc.close(SaveOptions.YES, doc.fullName)

               

              This loop gives me all kinds of grief, so apparently the same loop, as in paragraphStyles doesn't apply,

              doc.characterStyles[j].appliedFont is undefined. Wierd(!)

               

              I'm still in a nasty spin cycle here. I've tried many permutations of this same logic, and no effect. I can successfully change the fonts if InDesign knows about them. But, as mentioned above, if paragraphStyles doesn't have an entry for my missing font, Arial (OTF), how am I supposed to replace it?

              • 4. Re: Font Replacement
                AdobeScripts Community Member

                hi

                 

                sorry for a little misunderstanding I forgot to mention something

                 

                property AppliedFont in CharacterStyle could be undefined - because like in UserInterface - you can leave it unselected in CharacterStyle definition - like many other parameters - so when you are checking CharacterStyle which doesn't have selected Font - AppliedFont will be undefined

                 

                but if you will check ParagraphStyles definitions - there is always AppliedFont

                 

                so if CharacterStyles have undefined AppliedFont property - you can skip this CharacterStyle

                 

                robin

                 

                www.adobescripts.co.uk

                • 5. Re: Font Replacement
                  jeffmlevy Community Member

                  Yes, I understand. That makes sense, thanks for the extra clarification on that, it is appreciated.

                   

                  I'm still having some issue, though, with best practices for rectifying fonts idenitfied in the document.fonts array as 'NOT_AVAILABLE'.

                   

                  The good news is, the document.fonts at least shows me the font is not available. What I continue to be flummoxed by is how I traverse the document itself and find the text that references the missing font. If the paraStyles & charStyles have no mention of the missing font, how then am I supposed to identify it for replacement?

                   

                  Based on your last reply, my assumption is that the 'undefined' characterStyle.appliedFont property may indeed be the font I'm missing, and if it is, I do not know how to identify the name/family of that missing font (alas, because charStyle.appliedFont is undefined, there's no property to query for a name or family).

                   

                  So, with all this discovered, the root issue still persists. How to successfully match the doc.fonts array's "NOT_AVAILABLE" font with the corresponding document text/paragraph that refers to the font so I can replace it with another?

                   

                  Thanks again for your replies.

                  • 6. Re: Font Replacement
                    AdobeScripts Community Member

                    jeffmlevy wrote:

                     

                    The good news is, the document.fonts at least shows me the font is not available. What I continue to be flummoxed by is how I traverse the document itself and find the text that references the missing font. If the paraStyles & charStyles have no mention of the missing font, how then am I supposed to identify it for replacement?

                    like I said in my first post first step is to look at Char/ParaStyles definitions and then use Search&Replace to find font in text applied as local formatting

                     

                    there is:

                     

                    Property AppliedFont As Variant
                        Member of InDesign.FindPreference
                        The applied font. Either a Font object or the name of font family as Font, String or idNothingEnum enumerator

                     

                    then:

                     

                    myMissingFontName = "Otello"

                    myIndi.FindPreferences.AppliedFont = myMissingFontName

                     

                    and you will get collection of all texts with this missing font applied

                    jeffmlevy wrote:

                     

                    Based on your last reply, my assumption is that the 'undefined' characterStyle.appliedFont property may indeed be the font I'm missing, and if it is, I do not know how to identify the name/family of that missing font (alas, because charStyle.appliedFont is undefined, there's no property to query for a name or family).

                    no - if AppliedFont of CharacterStyle is undefined - than this means that no font is selected in CharacterStyle definition

                     

                    robin

                     

                    www.adobescripts.co.uk

                    • 7. Re: Font Replacement
                      Marc Autret MeganK

                      I don't know if it could help, but this solution works for me (ID CS4):

                       

                      Document.prototype.changeMissingFontsBy = function(/*str|Font*/fontOrFontName)
                      {
                      var asFont = function(/*var*/f)
                           {
                           if (!f) return null;
                           if (typeof f == 'string') f = app.fonts.item(f);
                           if (f.constructor != Font) return null;
                           return(f);
                           };
                      
                      var missing = function(/*Font*/f)
                           {return ( f.status != FontStatus.INSTALLED );}     
                           
                      var substFont = asFont(fontOrFontName);
                      if ((!substFont) || missing(substFont))
                           {
                           alert( "["+ fontOrFontName + "] is not installed!" );
                           return;
                           }
                      
                      var changeMissingFont = function(obj)
                           { // <obj> : any object with appliedFont prop
                           var f = asFont(obj.appliedFont);
                           if ((!f) || (!missing(f))) return;
                      
                           try{obj.appliedFont = substFont;}
                           catch(ex){}
                           };
                           
                      var scope = this.allCharacterStyles
                           .concat(this.allParagraphStyles)
                           .concat(this.stories.everyItem().textStyleRanges.everyItem().getElements());
                      
                      var s;
                      while ( s=scope.shift() ) changeMissingFont(s);
                      }
                      
                      // test
                      app.activeDocument.changeMissingFontsBy("Times New Roman");
                      
                      

                       

                      @+

                      Marc