Copy link to clipboard
Copied
I have over 300 different ai files with about 50 artboards in each one and need to export each artboard to a separate PDF file. I'm on a windows machine so the script would have to be JavaScript. Is this possible?
I found the following script on the forums, which is close, but I really need actual PDF files (not just "PDF compatible" ai files):
Thanks so much!
Tim
//splits the activeDocument Artboards into individual files
var doc = app.activeDocument;
var docName = doc.name;
var docPath = doc.path;
var fullName = docPath + "/" + docName;
var abRange = ""
for (i=1; i<=doc.artboards.length;i++)
{
abRange = abRange + i + ","
}
IllustratorSaveOptions.saveMultipleArtboards = true;
IllustratorSaveOptions.artboardRange = abRange;
var newFile = new File(fullName);
app.activeDocument.saveAs (newFile, IllustratorSaveOptions);
alert ("Artboards Saved to current document's Folder", "Split Arboards");
Replace the last function with…
function renameFiles(dir, fL) { for (var i = 0; i < fL.length; i++) { var f = File(dir.fsName + '/' + fL); if (f.exists) { var reName = f.name.replace('_','-'); f.rename(reName); f = File(dir.fsName + '/' + reName); reName = f.name.replace(/\.ai$/,'.pdf'); t =
...Copy link to clipboard
Copied
can you post a screen shot showing your Artboards palette?
Copy link to clipboard
Copied
No problem. Just curious, do you have another idea? Does my previous post make sense?
Copy link to clipboard
Copied
I do, you can accomplish it without scripting. Just save your master file (102.ai) and check the option "Save each artboard as separate file". That will give you
102.ai
102_21.ai
102_70.ai
102_71.ai
102_74.ai
102_80.ai
etc...
then, go to Bridge, select the files and batch rename to PDF.
Copy link to clipboard
Copied
That does work to prove it is possible to export artboards to separate AI files. But we need to automate the task into one
step because we revise about 10-20 different master AI files each day (and export the artboards). Just need one step to
save the master file, export the artboards to separate AI files, and have those exported ai files renamed to .pdf.
Really the last step is another script that constantly runs through the destination folder to automatically rename any AI file with .pdf
Also, need the name of the exported files to be 102-11.ai instead of an underscore (102_11.ai), and I think that can only be done via a script. Right?
Copy link to clipboard
Copied
saving artboards to separate files is proven, that's what my script in your first post do...now renaming...I think is possible too.
How is the scriptlance bidding going? I can't believe we're here scratching our heads trying to find a solution and there's someone willing and able to do it for $10...I guess they know they're doing.
Copy link to clipboard
Copied
Carlos, that option added to the Illustrator save options was well spotted… I looked everywhere or so I thought!!!! for something like that. Anyhows it is available to scripting too. So if you don't mind renamed '.ai' files as '.pdf' then the job is very easy now. Here how I would change the names of a folder of files…
#target illustrator var dF = Folder('~/Desktop'); var f = Folder.selectDialog('Where are the AI files?',dF); var fL = f.getFiles(/.ai$/); for (var i = 0; i < fL.length; i++) { fL.rename(fL.name.replace('-','_')); fL.rename(fL.name.replace(/.ai$/,'.pdf')); }
Of cause you would not bother will any of that selection stuff and just add this after the save so its all done in the one go…
Copy link to clipboard
Copied
Oh wow - that's great if I can do it all at the same time.
Mark - from post #5, what lines do I need to update in that script to change the export to .ai files, and then add your new rename script to it?
I'd much rather use this solution than the "monitor a folder and rename" solution I mentioned earlier. I just need a little help tieing this all together.
Thanks so much guys!!
Copy link to clipboard
Copied
Tim, Im at work at the moment n Im stuck with CS2 when at this junk box… Given Carlos's save method for splitting… The revised script is as good as done (Well so I think!!!). The only part I have NOT put in place yet is the savePath… As Im unsure how you are expected to pass a single path for multi files. I can however check this out later… when I get home… Here is how I was trying to go about it…
#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 outFiles = Array(); for (var i = 0; i < aB.length; i++) { outFiles.push(File(dF.fsName + '/' + baseName + '-' + abName + '.ai')); } var aiOpts = new IllustratorSaveOptions(); aiOpts.compatability = Compatability.ILLUSTRATOR13; aiOpts.pdfCompatible = true; aiOpts.saveMultipleArtboards = true; var saveFile = File(dF.fsName + '/' + docName); docRef.saveAs(saveFile, aiOpts); renameFiles(outFiles); } } artboardsToPDFs(); function renameFiles(fL) { for (var i = 0; i < fL.length; i++) { if (fL.exists) { fL.rename(fL.name.replace('-','_')); fL.rename(fL.name.replace(/\.ai$/,'\.pdf')); } } }
It may however choke at the saveAs() line?
Copy link to clipboard
Copied
Thanks so much Mark. Getting an error at line 27. See attached.
Copy link to clipboard
Copied
Yup I missed aline out when cutting and pasting (no artboards here to test with)…
#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 outFiles = Array(); for (var i = 0; i < aB.length; i++) { var abName = aB.name; outFiles.push(File(dF.fsName + '/' + baseName + '-' + abName + '.ai')); } var aiOpts = new IllustratorSaveOptions(); aiOpts.compatability = Compatability.ILLUSTRATOR13; aiOpts.pdfCompatible = true; aiOpts.saveMultipleArtboards = true; var saveFile = File(dF.fsName + '/' + docName); docRef.saveAs(saveFile, aiOpts); renameFiles(outFiles); } } artboardsToPDFs(); function renameFiles(fL) { for (var i = 0; i < fL.length; i++) { if (fL.exists) { fL.rename(fL.name.replace('-','_')); fL.rename(fL.name.replace(/\.ai$/,'\.pdf')); } } }
Copy link to clipboard
Copied
Now line 35 saying Compatability is underfined. See attached.
Thanks again!
Copy link to clipboard
Copied
It's probably best waiting until I get home if you can be bothered try putting two slashes // at the front of that line to comment it out? What Am I on about its a typo 'Compatability' should of cause be 'Compatibility' in both cases… That's why a muppet…
Copy link to clipboard
Copied
Thanks Mark. I'll wait for you to get home because there are still a few issues after commenting out line 35.
Thanks so much for your help! You rock.
Copy link to clipboard
Copied
Yeah most of that I pretty much expected… It's difficult when you haven't got anything in front of you… This does however do all of your request at this end… Let me know how you get on…
#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.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); if (f.exists) { var reName = f.name.replace('_','-'); f.rename(reName); f = File(dir.fsName + '/' + reName); reName = f.name.replace(/\.ai$/,'.pdf'); f.rename(reName); } } }
The name replacement is based on you having named your artboards… Had you not it will be broken by Illustrators auto numbering. What is nice is the dialog about replacing files better that my Q&A…
Copy link to clipboard
Copied
This is great. One thing that I guess hadn't thought through very well...once the .pdf file exists, how to replace the pdf file with the newer .pdf when we export changes. Right now, if the .pdf file exists in the folder, it can't rename the new .ai files to pdf because the filename already exists.
Any ideas?
Copy link to clipboard
Copied
Hi Mark, you really rock!!, the script works beautifully...it took a couple of seconds but no chocking with my humble 5 artboard test, let see how it goes with the real world file.
I took the liberty of fixing the errors for you, I swapped the underscore with the dash, and added a couple of lines to include the artboardRange property, you would have easily spotted if you had CS5.
a couple of notes on the whole exercise,
saving as...and checking the option to use "All" artboards in the UI (empty string in JS), saves the active file + all separate artboards. Entering the Range manually 1-5 for example, saves only the arboards, and leaves the active document alone and open.
#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 outFiles = Array();
for (var i = 0; i < aB.length; i++) {
var abName = aB.name;
outFiles.push(File(dF.fsName + '/' + baseName + '_' + abName + '.ai')); //changed '-' to '_'
}
var aiOpts = new IllustratorSaveOptions();
//aiOpts.compatability = Compatability.ILLUSTRATOR13;
aiOpts.pdfCompatible = true;
aiOpts.saveMultipleArtboards = true;
//************************************ added 2 lines
var abRange = "1-"+aB.length;
aiOpts.artboardRange = abRange;
//************************************
var saveFile = File(dF.fsName + '/' + docName);
docRef.saveAs(saveFile, aiOpts);
renameFiles(outFiles);
}
}
artboardsToPDFs();
function renameFiles(fL) {
for (var i = 0; i < fL.length; i++) {
if (fL.exists) {
fL.rename(fL.name.replace('_','-')); // swapped dash with underscore
fL.rename(fL.name.replace(/\.ai$/,'\.pdf'));
}
}
}
Copy link to clipboard
Copied
oops!! I took a little too long to compose the msg, above post Z-Order should have been # 40
Copy link to clipboard
Copied
Is there a way to rename a file in a directory to a name of a file that already exists in that directory? Basically name the new file and delete the old pdf that has the same name?
Right now I'm getting ai files that can't be renamed to PDF upon export because the pdf file name already exists in that directory. Once I get this workaround solved, I think I'll be home free...
Thoughts?
Copy link to clipboard
Copied
Replace the last function with…
function renameFiles(dir, fL) { for (var i = 0; i < fL.length; i++) { var f = File(dir.fsName + '/' + fL); 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); } } }
Copy link to clipboard
Copied
Mark - thank you so much. Your help on this has been incredible. Works perfectly.
Copy link to clipboard
Copied
preserveEditability here means to include an entire copy of the proprietary AI file in the PDF as well as the PDF data. It does not change the original AI file. Do you really need this?
Copy link to clipboard
Copied
No I do not need the other artboards retained in the PDF, but I do need to access grouped objects in the PDF file. What I mean is, when you do not "perserve illustrator editing capabilities", the art is flattened, masked, and grouped into an unselectable/usable format. When you open the resulting PDF back up in Illustrator and click on an object, the whole artboard is selected (because of a group/mask I suppose). Does that make sense?
Copy link to clipboard
Copied
Mark - do you know any reference sites or information I could look at to learn the structure of the simple string/filepath edit that I need to use change the location of these exported files? Right now it's using/creating a folder on the desktop called AI pdfs.
Thanks!
Copy link to clipboard
Copied
How would I specifiy the following location for the exported files to go:
S:\Graphics\Graphics Dept Internal Use\Manufacturing - Production\Regular Orders
instead of this location:
var baseName = docName.replace(/.ai$/,'');
var dF = Folder(Folder.desktop + "/AI PDF's");
Can someone point me in the right direction on filepath syntax?
Thanks!
Copy link to clipboard
Copied
Tim you should be able to put your path sting 'quoted' in the dF variable like so…
var dF = Folder('S:\Graphics\Graphics Dept Internal Use\Manufacturing - Production\Regular Orders');
and loose the line below about creating a new folder… Im NOT sure about that colon in the path thou as I don't normally have to deal with PC paths…
Find more inspiration, events, and resources on the new Adobe Community
Explore Now