Skip to main content
sebastianf15538645
Inspiring
April 26, 2018
Answered

How can I modify the "Save as PDF" script to avoid the save dialogue? [Locked – Duplicate post]

  • April 26, 2018
  • 2 replies
  • 1463 views

Okay, I found out how to modify the "Save as PDF" javascript supplied with Illustrator so that it renders the .pdf with my preferred .joboption (e.g. X3) - what I'm lacking and don't seem to be able to find out via google is how to suppres the save dialogue (asking me about the location where to save the file every time I invoke that script).

I'd like the script to save the PDF

- in exactly the same location as the .ai document

- overwrite any probably existing .pdf file with the same name without asking

Can anybody here tell me how to do this trick?

Thx. in advance!

[duplicate thread locked by moderator] --> How can I modify the "Save as PDF" script to avoid the save dialogue?

This topic has been closed for replies.
Correct answer q3player

var doc = app.activeDocument;

var pdfOption = 'PDFX3_CanDo_X3'; // Name der PDF-Settings

var pdfSuff = '.pdf'; // Suffix für PDF

function getOptions (set) {

    var options = new PDFSaveOptions ();

    options.pDFPreset = set;

    return options;

    }

function saveCopyAsPDF (set, suff) {

        var original_file = doc.fullName;

        var arr = doc.name.split(".");

        var extension = "";

        if (arr.length > 1) {

            extension = "." + arr.pop()

            };

        var filename = arr.join(".");

        var destFolder = null;

            destFolder = activeDocument.path

        var options = this.getOptions (set);

        var targetFile;  

            targetFile = this.getTargetFile (filename, suff, destFolder);

            doc.saveAs (targetFile, options);

        doc.close ();

        app.open (File (original_file));

        }

function getTargetFile (docName, ext, destFolder) {

    var newName = "";

        newName = docName + ext;

    var myFile = new File (destFolder + '/' + newName);

    return myFile;

}

saveCopyAsPDF (pdfOption, pdfSuff);

You'll have to edit the 2nd line and put in the exact name of your PDF settings. The script saves a PDF in the same folder as the .ai file and after that re-opens the original .ai file.

2 replies

Disposition_Dev
Legend
May 8, 2018

you can set a variable for the location of the activeDocument with the following line:

var savePath = app.activeDocument.path;

Assuming the file has been previously saved (i.e., not a brand new unsaved document), you can programmatically save a PDF version of the current document int he same folder by appending the file name to the end of this variable and then passing the resulting string to the File() object, like so:

var pdfSaveOpts = new PDFSaveOptions();

var mySaveFile = File(savePath + "/My_Save_File_Name.pdf");

app.activeDocument.saveAs(mySaveFile,pdfSaveOpts);

assuming the savePath exists, it should save with no dialog.

you can also hard code the savePath if you know the exact location in which you want to save the file.

var mySaveFile = File("/Path/To/Save/Location/My_Save_File_Name.pdf");

hope this helps

q3playerCorrect answer
Inspiring
May 9, 2018

var doc = app.activeDocument;

var pdfOption = 'PDFX3_CanDo_X3'; // Name der PDF-Settings

var pdfSuff = '.pdf'; // Suffix für PDF

function getOptions (set) {

    var options = new PDFSaveOptions ();

    options.pDFPreset = set;

    return options;

    }

function saveCopyAsPDF (set, suff) {

        var original_file = doc.fullName;

        var arr = doc.name.split(".");

        var extension = "";

        if (arr.length > 1) {

            extension = "." + arr.pop()

            };

        var filename = arr.join(".");

        var destFolder = null;

            destFolder = activeDocument.path

        var options = this.getOptions (set);

        var targetFile;  

            targetFile = this.getTargetFile (filename, suff, destFolder);

            doc.saveAs (targetFile, options);

        doc.close ();

        app.open (File (original_file));

        }

function getTargetFile (docName, ext, destFolder) {

    var newName = "";

        newName = docName + ext;

    var myFile = new File (destFolder + '/' + newName);

    return myFile;

}

saveCopyAsPDF (pdfOption, pdfSuff);

You'll have to edit the 2nd line and put in the exact name of your PDF settings. The script saves a PDF in the same folder as the .ai file and after that re-opens the original .ai file.

Larry G. Schneider
Community Expert
Community Expert
April 26, 2018

Please try asking here

Illustrator Scripting

sebastianf15538645
Inspiring
April 27, 2018

Did so - thx for the tip.