Skip to main content
Known Participant
July 11, 2018
Answered

Collect used document fonts and export them to folder

  • July 11, 2018
  • 1 reply
  • 677 views

Hello all!

I am needing a little bit of help with a script I am using to collect a

documents fonts. It is all working fine, except I don't like how it is

naming the folder it creates. I want the folder name to be simply "Document

fonts" and not include the InDesign file name. I would like the folder to be

in the same directory as the Indesign file. Would somebody be so kind as to

show me what I need to change in my code? I have the code I am using below.

If anything isn't clear enough, let me know.

var

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

myDocument = app.activeDocument,

len = myFonts.length,

cFile, cName, outFile, i,

movefolder = myDocument.fullName + "Document fonts"

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

}

Thanks in advance for your help!

Awitmer

This topic has been closed for replies.
Correct answer Laubender

Hi,

replace this:

cFile, cName, outFile, i,

movefolder = myDocument.fullName + "Document fonts"

movefolder = Folder(movefolder);  

if (!movefolder.exists) movefolder.create();

with that:

cFile, cName, outFile, i ;

var movefolder = File( myDocument.fullName).parent+"/"+ "Document fonts" ;

movefolder = Folder( movefolder );  

if (!movefolder.exists) movefolder.create();

Regards,
Uwe

1 reply

LaubenderCommunity ExpertCorrect answer
Community Expert
July 11, 2018

Hi,

replace this:

cFile, cName, outFile, i,

movefolder = myDocument.fullName + "Document fonts"

movefolder = Folder(movefolder);  

if (!movefolder.exists) movefolder.create();

with that:

cFile, cName, outFile, i ;

var movefolder = File( myDocument.fullName).parent+"/"+ "Document fonts" ;

movefolder = Folder( movefolder );  

if (!movefolder.exists) movefolder.create();

Regards,
Uwe

Community Expert
July 11, 2018

And you could check if the active document was ever saved.

If not, exit, because there is no fullName of the document.

if(app.documents.length == 0){ exit() };

if(!app.documents[0].saved){ exit() };

Put both lines at the beginning of your script.

However, it would be better to wrap all the code into a self-executing anonymous function like below.

Simply use return instead of exit() inside this function.

( function()

{

    // No document open:

    if(app.documents.length == 0){ return };

    // Document was never saved:

    if(!app.documents[0].saved){ return };

    var myDocument = app.activeDocument;

    var myFonts = myDocument.fonts.everyItem().getElements();

   

    var len = myFonts.length;

    var cFile, cName, outFile, i ;

   

    var movefolder = File( myDocument.fullName).parent+"/"+ "Document fonts" ;

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

       

    }

}() )

Regards,
Uwe

AwitmerAuthor
Known Participant
July 11, 2018

Thank you so much! This looks a lot cleaner and more readable.