Skip navigation
Currently Being Moderated

Modify a script help?

May 10, 2012 6:45 AM

I have a script that needs to be tweaked very slightly.  Right now this script is setup to save out files and rename them as follows:

current document file name + "-" + artboard name + (rename files from .ai to .pdf)

 

I need this script tweaked so that the exported file names are as follows:

artboard name + "-" + current document file name + (rename from .ai to .pdf)

 

Here's the script that needs to be tweaked:

 

 

#target illustrator

 

function artboardsToPDFs() {

 

     if (app.documents.length = 0) {

 

          return;

 

     } else {

 

          var docRef = app.activeDocument;

         

          var docName = docRef.name;

 

          var baseName = docName.replace(/.ai$/,'');

 

     var dF = Folder(Folder.desktop + "/AI PDF's");

         

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

       

          var aB = docRef.artboards;

         

          var autoNames = Array();

         

          for (var i = 0; i < aB.length; i++) {

         

               var abName = aB[i].name;

         

               autoNames.push(baseName + '_' + abName + '.ai');

         

          }

         

          if (!docRef.saved) docRef.save();

         

          var aiOpts = new IllustratorSaveOptions();

         

          aiOpts.compatibility = Compatibility.ILLUSTRATOR13;

          aiOpts.pdfCompatible = true;

          aiOpts.saveMultipleArtboards = true;

         

          var saveFile = File(dF.fsName + '/' + docName);

         

          docRef.saveAs(saveFile, aiOpts);

         

          docRef.close(SaveOptions.DONOTSAVECHANGES);

         

          saveFile.remove();

         

          renameFiles(dF, autoNames);

     }

}

 

artboardsToPDFs();

 

 

function renameFiles(dir, fL) {

 

     for (var i = 0; i < fL.length; i++) {

              

          var f = File(dir.fsName + '/' + fL[i]);

         

          if (f.exists) {

                        

               var reName = f.name.replace('_','-');

    

               f.rename(reName);

              

               f = File(dir.fsName + '/' + reName);

              

               reName = f.name.replace(/\.ai$/,'.pdf');

              

               t = File(dir.fsName + '/' + reName);

              

               if (t.exists) t.remove();

    

               f.rename(reName);

              

          }

     }

}

 
Replies
  • Currently Being Moderated
    May 10, 2012 9:25 AM   in reply to timlogochair

    Tim

     

    Should be as easy as

     

    autoNames.push(baseName + '_' + abName + '.ai');

     

    to

     

    autoNames.push(abName + '-' + baseName + '.ai');

     
    |
    Mark as:
  • Currently Being Moderated
    May 10, 2012 10:53 AM   in reply to timlogochair

    Sorry Tim,

    I seem to be running into the same problem as you. It's obviously not doing the function at the end of the file. I'll try to explore  more. I wish I understood how to step through the script better.

     
    |
    Mark as:
  • Currently Being Moderated
    May 16, 2012 1:51 PM   in reply to timlogochair

    Not sure if this may be the problem but don't you want

     

    if (app.documents.length = 0) {

    to be

    if (app.documents.length == 0) {

    ?

     
    |
    Mark as:
  • Currently Being Moderated
    May 17, 2012 4:46 AM   in reply to Larry G. Schneider

    Actually its NOT the auto names array that you want to alter… Thats a list of names that the app will by default will create ( based on doc content ) The script goes and finds these files and changes the names afterwards…

     
    |
    Mark as:
  • Currently Being Moderated
    May 18, 2012 6:58 AM   in reply to timlogochair

    This should do you… I updated it a touch… And made it simpler to edit…

     

    #target illustrator
     
     
    artboardsToPDFs();
     
    function artboardsToPDFs() {
     
        if ( app.documents.length == 0 ) { return; }
     
        var aiOpts, artBds, baseName, doc, fileDef, fileRep, i, names, obj, saveFile, saveFold;
     
        doc = app.activeDocument;
     
        baseName = doc.name.replace( /.ai$/,'' );
     
        saveFold = Folder( Folder.desktop + "/AI PDF's" );
     
        if ( ! saveFold.exists ) { saveFold.create(); }
     
        artBds = doc.artboards;
     
        names = Array();
     
        for ( i = 0; i < artBds.length; i++ ) {
     
            obj = Object();
     
            obj.defaultName = baseName + '_' + artBds[i].name + '.ai'; // Default AI naming convention
     
            obj.replaceName = artBds[i].name + '-' + baseName + '.pdf'; // Script swap about
     
            names.push( obj );
     
        };
     
        if ( ! doc.saved ) { doc.save(); }
     
        aiOpts = new IllustratorSaveOptions();
     
        aiOpts.compatibility = Compatibility.ILLUSTRATOR13;
        aiOpts.pdfCompatible = true;
        aiOpts.saveMultipleArtboards = true;
     
        saveFile = File( saveFold + '/' + doc.name );
     
        doc.saveAs( saveFile, aiOpts );
     
        doc.close( SaveOptions.DONOTSAVECHANGES );
     
        saveFile.remove();
     
        for (  i = 0; i < names.length; i++  ) {
     
            fileDef = File( saveFold + '/' + names[i].defaultName );
     
            if ( fileDef.exists ) {
     
                fileRep = File( saveFold + '/' + names[i].replaceName );
     
                if ( fileRep.exists ) { fileRep.remove(); }
     
                fileDef.rename( names[i].replaceName );
     
            };
     
     
        };
     
    };
     
    
     
    |
    Mark as:
  • Currently Being Moderated
    May 18, 2012 9:29 AM   in reply to Muppet Mark

    Mark,

     

    I'm still having a problem with the renaming portion. Here's a picture of my file called test_5.ai and the contents of the folder AI PDF's on my desktop

     

    Screen shot 2012-05-18 at 9.26.13 AM.png

     

     

    Screen shot 2012-05-18 at 9.25.41 AM.png

     

    It seems as though the second rename is not taking. Mac OSX 10.6.8, AICS5

     
    |
    Mark as:
  • Currently Being Moderated
    May 18, 2012 9:41 AM   in reply to Larry G. Schneider

    Larry you need to have named your artboards first… It looks like yours are AI defaults… If I recall corrrectly this was always the case for Tim…

     
    |
    Mark as:
  • Currently Being Moderated
    May 18, 2012 9:53 AM   in reply to Muppet Mark

    Thanks, Mark. I had tried earlier with named artboards and the earlier version of the script but didn't name them when testing this version.

     
    |
    Mark as:
  • Currently Being Moderated
    May 18, 2012 9:57 AM   in reply to Larry G. Schneider

    Yeah, I didn't need to bother myself with testing for Tim… Named artboards default to strings, un-named to digits never tried with mixed…? Did it work OK once you named them?

     
    |
    Mark as:
  • Currently Being Moderated
    May 18, 2012 10:25 AM   in reply to Muppet Mark

    Worked perfectly with the named artboards.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points