Hello again.
I am finishing up a simple script similar to one you've probably seen and more than likely used before (multiexporter.jsx by Tom Byrne). My production department has requested that we give them a single PDF per artboard in the document with specific export settings and a proper file-name (based on the name of the artboard).
i've written a loop to save out each artboard with all the right settings.. but now i'm running into the problem of duplicates. It's very likely that I will have many duplicate artboard names, but they all need to be saved. By default, the files are overwritten by the subsequent save function. I got past that by adding a sort of sequence number to the end of the filename if a duplicate is found.
The problem now, is that I would like the script to run correctly regardless of whether the destination folder exists or not. For example, I've already run the script and saved my .ai file and all the PDFs. Now i realize that i needed to make a change to the file and re-save (via the script) and overwrite the files i saved there the first time. Unfortunately, because of the sequence numbers i talked about above, i get all the old files as well as all the new files in the folder.
My first thought was to delete the existing destination folder and re-create it. However, it looks like Javascript cannot do that. Will i just have to tell my artists they must manually delete their folder before they re-run the script? or is there a way to overwrite the folder while still maintaining the sequencing for duplicate files inside the folder??
Here's the code:
#target Illustrator
//Script name: Save Document and Extract PDFs
//Author: William Dowling
//Creation Date: 10/15/15
/*
Saves active document to user's desktop in dated folder with order number as filename. Saves each artboard as a PDF with artboard name as filename.
*/
function container(){
var docRef = app.activeDocument; var layers = docRef.layers; var aB = docRef.artboards; var dest = findDest(); var date = getDate(); function findDest(){ var dest = new Folder("~/Desktop" + "/Today's Orders"); return dest.fsName; } function getDate(){ var today = new Date(); var dd = today.getDate(); var mm = today.getMonth()+1; var yyyy = today.getYear(); var yy = yyyy-100; if(dd<10) { dd='0'+dd } if(mm<10) { mm='0'+mm } return mm+'.'+dd+'.'+yy; } dest = "/Volumes/Macintosh HD" + dest + " " + date; if(!dest.exists){ var newFolder = new Folder(dest); newFolder.create(); } if(docRef.name.substring(0,2) == "Un"){ var oN = prompt("Enter Order Number", "1234567"); var fileName = oN; } else{ var fileName = docRef.name; } var saveFile = new File(dest + "/" + fileName); if(saveFile.exists){ if(!confirm("This File already exists.. Do you want to overwrite?", false, "Overwrite file?")){ return; } } docRef.saveAs(saveFile); var pdfdest = dest + "/" + fileName.substring(0,fileName.indexOf(".ai")) + "_PDFs"; var pdfFolder = new Folder(pdfdest); if(!pdfFolder.exists){ pdfFolder.create(); } else if(pdfFolder.exists){ if(!confirm("The PDFs folder for this order already exists.. Do you want to overwrite?", false, "Overwrite PDFs Folder?")){ return; } else{ //pdfFolder.Delete(); //within this else statement, i'd like to remove the folder or it's contents and start with a fresh folder pdfFolder = new Folder(pdfdest); pdfFolder.create(); } } //loop artboards to save individual PDFs var pdfSaveOpts = new PDFSaveOptions(); pdfSaveOpts.preserveEditability = false; pdfSaveOpts.viewAfterSaving = false; var seq = 1; for(var a=0;a<aB.length;a++){ var range = (a+1).toString(); pdfSaveOpts.artboardRange = range; var pdfFile = new File(pdfdest + "/" + aB[a].name); var thisPDF = new File(pdfFile + ".pdf"); if(thisPDF.exists){ thisPDF = new File(pdfFile + " " + seq.toString()); docRef.saveAs(thisPDF, pdfSaveOpts); seq++; } else{ docRef.saveAs(pdfFile, pdfSaveOpts); } }
}
container();