Skip to main content
Emac_trademark
Inspiring
March 8, 2023
Question

Batch "Print to .pdf" illustrator script

  • March 8, 2023
  • 1 reply
  • 896 views

I have a script that runs a batch "save as" with preset dialog box on all open tabs/files, but was wondering if someone could tweek it to batch "print to .pdf" with preset dialog box on all open tabs/files?

 

Here is my current script:

 

var w = new Window("dialog", "Choose a Preset");
var dropdownlist = w.add("dropdownlist", undefined, app.PDFPresetsList);
dropdownlist.preferredSize.width = 200;
dropdownlist.selection = 0;
var choice = dropdownlist.selection;
dropdownlist.onChange = function () {
choice = dropdownlist.selection;
};
var button = w.add("button", undefined, "OK");
button.onClick = function () {
saveDocsAsPDF(choice);
w.close();
};
w.show();

function saveDocsAsPDF(choice) {
// original SaveDocsAsPDF script
try {
if (app.documents.length > 0 ) {
var destFolder = null;
destFolder = Folder.selectDialog('Select folder for PDF files.', '~');
if (destFolder != null) {
var options, i, sourceDoc, targetFile;
options = getOptions();
for ( i = 0; i < app.documents.length; i++ ) {
sourceDoc = app.documents[i];
targetFile = getTargetFile(sourceDoc.name, '.pdf', destFolder);
sourceDoc.saveAs( targetFile, options );
}
alert('Documents saved as PDF');
}
} else {
throw new Error('There are no document open!');
}
} catch(e) {
alert( e.message, "Script Alert", true);
}
function getOptions() {
var options = new PDFSaveOptions();
options.pDFPreset = choice;
return options;
}
function getTargetFile(docName, ext, destFolder) {
var newName = "";
if (docName.indexOf('.') < 0) {
newName = docName + ext;
} else {
var dot = docName.lastIndexOf('.');
newName += docName.substring(0, dot);
newName += ext;
}
var myFile = new File( destFolder + '/' + newName );
if (myFile.open("w")) {
myFile.close();
}
else {
throw new Error('Access is denied');
}
return myFile;
}
}

This topic has been closed for replies.

1 reply

Emac_trademark
Inspiring
March 14, 2023

Anyone gonna respond to this?  Crickets...really?

 

I did find out about Illustrator's batch actions, which are pretty cool, but it appears that it doesn't quite do what I was hoping for.  It appears one can only choose a folder or data set...I need to batch "print to .pdf" from all open tabs.

 

Can anyone help tweek above script?  Or guide me to a resource that I can try and tweek it myself?  I love using scripts, I just don't how to write them.

m1b
Community Expert
Community Expert
March 15, 2023

Hi @Emac_trademark, I haven't ever done a dive into print to pdf via scripting myself but I suspect that if no-one has answered here, then it might be a difficult proposition.

 

On MacOS (I'm not familiar with Windows) you can either have a PDF printer (a virtual Printer setup to only print pdf files) or not. If you have one, then printing to pdf should be as easy as normal printing via script from Illustrator (sorry I've never done that, but I think it's doable).

 

But if you don't have a PDF Printer then—still on MacOS, Windows is different—you have to do the following:

1. invoke Print menu item

2. click on "Setup" button

3. Click "Continue" button when advised to "please set all print options from the Print dialog box"

4. From the PDF dropdown menu choose "Save as PDF..." and provide a filename and press "Save".

 

I'm almost certain that these options aren't exposed via the scripting API, so it would require third party UI automation tool to do it that way.

 

Bear in mind that I am not knowledgable on the printing topic, and I'm only answering because you didn't get any responses yet. 🙂

- Mark