8 Replies Latest reply on Feb 22, 2016 2:46 PM by biozaichenko

    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 Level 3

          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 Level 1

            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 Level 1

              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 Level 3

                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 Level 1

                  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 Level 3

                    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 Level 4

                      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

                      • 8. Re: Font Replacement
                        biozaichenko Level 1

                        Hi Marc,

                        I know it is a very old post but hopefully you are following up on it. I'm writing a script using your functions (above) however I would like to use a composite font of my own making in it. I'm trying to create a Composite font thru javascript. It seems to work but I can't get it to show up in the Font menus or lists in InDesign and subsequently it throws an error. What command do I use for this?

                         

                        I have this so far:

                        var fnt = app.activeDocument.compositeFonts.add ({name:"Text"});

                        var fntEng = fnt.compositeFontEntries.add ({appliedFont:app.fonts[1]});

                        ...

                         

                        Thanks in advance.

                         

                        Michael