Skip to main content
tpk1982
Legend
January 17, 2017
Answered

Collect used fonts

  • January 17, 2017
  • 1 reply
  • 3093 views

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

This topic has been closed for replies.
Correct answer Jump_Over

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


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.location);

  if(!cFile.exists) continue;

  cName = cFile.name;

  outFile = File(movefolder + "/"+ cName);

  cFile.copy(outFile);  

  }

Jarek

1 reply

Peter Kahrel
Community Expert
Community Expert
January 17, 2017

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

P.

tpk1982
tpk1982Author
Legend
January 17, 2017

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

Peter Kahrel
Community Expert
Community Expert
January 17, 2017

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