9 Replies Latest reply on Jan 18, 2017 12:55 AM by tpk1982

    Collect used fonts

    tpk1982 Level 4

      Hello,

       

      Is it possible to collect the used fonts in the active document?

       

      Tried coding:

       

      var  myFonts=app.documents[0].stories.everyItem().textStyleRanges.everyItem().appliedFont;
        len = myFonts.length;  
      var  destFolder = "~/Desktop/usedFonts/";  
      if ( !Folder(destFolder).create() ){ exit();  }
      
      
      while (len-->0) {  
        myfontpath = myFonts[len].filePath;  
        currFile = File( destFolder + File(myfontpath).name );  
        if ( !currFile.exists && File(currLinkFName).exists )  
        myfontpath[len].copyLink(currFile);  
        }  
      
      

      Regards,

      K

        • 1. Re: Collect used fonts
          Peter Kahrel Adobe Community Professional & MVP

          Yes, should be possible. What's the problem with your code?

           

          P.

          • 2. Re: Collect used fonts
            tpk1982 Level 4

            Hi Peter,

             

            Thank you for your reply. The problem is, it is not collecting the fonts as expected.

             

            I am not sure we can take the fonts using filePath, because it will work for images.

             

            var  myFonts=app.documents[0].stories.everyItem().textStyleRanges.everyItem().appliedFont;  
              len = myFonts.length;    
            var  destFolder = "~/Desktop/usedFonts/";    
            if ( !Folder(destFolder).create() ){ exit();  }  
                
            while (len-->0) {    
              myfontpath = myFonts[len].filePath;    
              currFile = File( destFolder + File(myfontpath).name );    
              if ( !currFile.exists)    
              myfontpath[len].copy(currFile);    
              }    
            

             

             

            Regards,

            K

            • 3. Re: Collect used fonts
              Peter Kahrel Adobe Community Professional & MVP

              Well, if you check one of the object-model viewers, you'll see that 'font' doesn't have the property 'filePath'. Try 'location'. Also, to get a handle on the fonts you can use

               

              myFonts = app.documents[0].fonts.everyItem().getElements();

               

              Getting them from the textStyleRanges gives you multiple instances of each font, so your script ends up doing all kinds of unnecessary things. And finally, when you know the length of what you're processing it's better to use a for-loop, that's quicker than a while-loop. Though on modern computers you don't notice the difference much anymore.

               

              Peter

              1 person found this helpful
              • 4. Re: Collect used fonts
                tpk1982 Level 4

                Thanks Peter, i will try as you adviced

                • 5. Re: Collect used fonts
                  tpk1982 Level 4

                  Hello Peter,

                   

                  I tried the below coding and it is also not copying the fonts

                   

                  var  myFonts = app.documents[0].fonts.everyItem().getElements();
                  len = myFonts.length;    
                  var  movefolder = "~/Desktop/usedFonts/";      
                      for(i=0;i<len;i++){
                      movefolder = Folder(movefolder);   
                      if (!movefolder.exists) movefolder.create();  
                          if (movefolder.exists){
                              myFonts[0].copy(movefolder);  
                          }
                      }
                  

                   

                  Thanks,

                  K

                  • 6. Re: Collect used fonts
                    Jump_Over Level 5

                    Hi,

                     

                    Notice that a file is the target - not a font.

                     

                    var 
                      myFonts = app.documents[0].fonts.everyItem().getElements(),
                      len = myFonts.length, 
                      cFile, cName, outFile, i,
                      movefolder = "~/Desktop/usedFonts/",
                      movefolder = Folder(movefolder);   
                    if (!movefolder.exists) movefolder.create();
                    
                    for(i=0;i<len;i++) { 
                      cFile = File(myFonts[i].location);
                      if(!cFile.exists) continue;
                      cName = cFile.name;
                      outFile = File(movefolder + "/"+ cName);
                      cFile.copy(outFile);   
                      } 
                    

                     

                    Jarek

                    • 7. Re: Collect used fonts
                      Peter Kahrel Adobe Community Professional & MVP

                      No need to create the movefolder multiple times. And the destination is a file path, not a string:

                       

                      var myFonts = app.documents[0].fonts.everyItem().getElements();
                      var len = myFonts.length;
                      var movefolder = "~/Desktop/usedFonts/";
                      if (!movefolder.exists) Folder (movefolder).create();
                      for (var i=0;i<len;i++) {
                        var name = File(myFonts[i].location).name;
                        File (myFonts[i].location).copy(File(movefolder+'/'+name));
                      }
                      

                       

                      P.

                       

                      (Jarek beat me to it!)

                      1 person found this helpful
                      • 8. Re: Collect used fonts
                        tpk1982 Level 4

                        Thank you Jarek.. It is working fine..

                        • 9. Re: Collect used fonts
                          tpk1982 Level 4

                          Thank you Peter.. Your's also working good..

                           

                          Thanks again for your timely help..

                           

                          Hereafter forum should have the facility to give correct answer option for two or more people..