Skip to main content
Emac_trademark
Inspiring
April 22, 2022
Answered

Modified Illustrator Script Needed for SaveDocsAsPDF

  • April 22, 2022
  • 1 reply
  • 828 views

Is there a way to modify the default SaveDocsAsPDF script to have it open a dialog box for my PDF presets so I can choose each time which preset I want to use for the open .ai files I want to convert and save as PDF's?  Thanks in advance if someone could tweek this script!

This topic has been closed for replies.
Correct answer femkeblanco

 

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

 

1 reply

Emac_trademark
Inspiring
April 25, 2022

Here is the script I would like modified.

 

/**********************************************************

ADOBE SYSTEMS INCORPORATED
Copyright 2005-2006 Adobe Systems Incorporated
All Rights Reserved

NOTICE: Adobe permits you to use, modify, and
distribute this file in accordance with the terms
of the Adobe license agreement accompanying it.
If you have received this file from a source
other than Adobe, then your use, modification,
or distribution of it requires the prior
written permission of Adobe.

*********************************************************/

/** Saves every document open in Illustrator
as a PDF file in a user specified folder.
*/

// Main Code [Execution of script begins here]

try {
// uncomment to suppress Illustrator warning dialogs
// app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

if (app.documents.length > 0 ) {

// Get the folder to save the files into
var destFolder = null;
destFolder = Folder.selectDialog( 'Select folder for PDF files.', '~' );

if (destFolder != null) {
var options, i, sourceDoc, targetFile;

// Get the PDF options to be used
options = this.getOptions();
// You can tune these by changing the code in the getOptions() function.

for ( i = 0; i < app.documents.length; i++ ) {
sourceDoc = app.documents[i]; // returns the document object

// Get the file to save the document as pdf into
targetFile = this.getTargetFile(sourceDoc.name, '.pdf', destFolder);

// Save as pdf
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);
}

/** Returns the options to be used for the generated files.
@Return PDFSaveOptions object
*/
function getOptions()
{
// Create the required options object
var options = new PDFSaveOptions();
// See PDFSaveOptions in the JavaScript Reference for available options

// Set the options you want below:

// For example, uncomment to set the compatibility of the generated pdf to Acrobat 7 (PDF 1.6)
// options.compatibility = PDFCompatibility.ACROBAT7;

// For example, uncomment to view the pdfs in Acrobat after conversion
// options.viewAfterSaving = true;

return options;
}

/** Returns the file to save or export the document into.
@9397041 docName the name of the document
@9397041 ext the extension the file extension to be applied
@9397041 destFolder the output folder
@Return File object
*/
function getTargetFile(docName, ext, destFolder) {
var newName = "";

// if name has no dot (and hence no extension),
// just append the extension
if (docName.indexOf('.') < 0) {
newName = docName + ext;
} else {
var dot = docName.lastIndexOf('.');
newName += docName.substring(0, dot);
newName += ext;
}

// Create the file object to save to
var myFile = new File( destFolder + '/' + newName );

// Preflight access rights
if (myFile.open("w")) {
myFile.close();
}
else {
throw new Error('Access is denied');
}
return myFile;
}

femkeblanco
femkeblancoCorrect answer
Legend
April 25, 2022

 

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

 

Emac_trademark
Inspiring
April 25, 2022

Thank you!  It appears to be working!