Skip to main content
Luís Henrique de Almeida
Inspiring
January 5, 2023
Answered

app.activeDocument.saveAs but without opening the saved file

  • January 5, 2023
  • 2 replies
  • 1600 views

I started learning JSX and made this basic save as PDF Script: 

function saveAsPDF() {
    var f = new Folder(app.activeDocument.path + "/" + app.activeDocument.name.split('.')[0]);
    if ( ! f.exists ) {
        f.create()
    }
    var pathBeforeSaving = app.activeDocument.path + "/" + app.activeDocument.name;

    var pdfFile = new File(app.activeDocument.path + "/" + app.activeDocument.name.split('.')[0] + "/" + app.activeDocument.name.split('.')[0] + '.pdf');

    var pdfOptions = new PDFSaveOptions();
    pdfOptions.viewAfterSaving = false;
    pdfOptions.pDFPreset = "[Impressão de alta qualidade]";

    app.activeDocument.saveAs(pdfFile, pdfOptions, true);

    alert("Saved");

    app.activeDocument.close();
    open(File(pathBeforeSaving));
}
saveAsPDF();

 

It saves a file in a folder with the same name of the life.
The Problems is that after saving the file, it replaces the current app.activeDocument to the new PDF file, but i still need to make chages into the original .ai file.

There is a way to save a new PDF file without opnening it after saving?

 

 

This topic has been closed for replies.
Correct answer Charu Rajput

I got it, it is artboardRange, I never noticed that it will not keep pdf document open.

Thank you @m1b 🙂

 

2 replies

Charu Rajput
Community Expert
Community Expert
January 6, 2023

Hi @Luís Henrique de Almeida ,

You can try ExportForScreen option, but you have less cabibility to set the PDF options.

function saveAsPDF() {
    var f = new Folder(app.activeDocument.path + "/" + app.activeDocument.name.split('.')[0]);
    if (!f.exists) {
        f.create()
    }
    var itemToExport = new ExportForScreensItemToExport();
    itemToExport.artboards = '';
    itemToExport.document = true;
    var pdfOptions = new ExportForScreensPDFOptions();
    pdfOptions.viewAfterSaving = false;
    pdfOptions.pDFPreset = "[Impressão de alta qualidade]";

    app.activeDocument.exportForScreens(f, ExportForScreensType.SE_PDF, pdfOptions, itemToExport);

}
saveAsPDF();
Best regards
Charu Rajput
Community Expert
Community Expert
January 6, 2023

Hi @m1b ,

I am curious why your script does not keep PDF document open. I mean you and @Luís Henrique de Almeida, both are uisng the saveAs method. Which property keep the document open as ai? 

 

Best regards
m1b
Community Expert
Community Expert
January 6, 2023

Hi @Luís Henrique de Almeida I have my own "saveDocumentAsPDF" function. There are some small differences. When I use my function, I don't have to open or close anything—it just exports the pdf and leaves my open document untouched.

Here is the function:

/**
 * Saves document as PDF.
 * @author m1b
 * @version 2022-09-18
 * @param {Object} options
 * @param {Document} options.doc - an Illustrator Document.
 * @param {String} options.path - export path including filename.
 * @param {String} options.pdfPresetName - the PDF preset name.
 * @returns {File} - the exported pdf file.
 */
function saveDocumentAsPDF(options) {

    if (
        options.doc == undefined
        || options.path == undefined
        || options.pdfPresetName == undefined
    )
        return;

    var saveOptions = new PDFSaveOptions();

    var doc = options.doc,
        exportPath = options.path;

    saveOptions.saveMultipleArtboards = true;
    saveOptions.pDFPreset = options.pdfPresetName;
    saveOptions.viewAfterSaving = false;

    // must set the range, or will rename the document, too
    saveOptions.artboardRange = '1-' + doc.artboards.length;

    var f = File(exportPath);

    doc.saveAs(f, saveOptions);

    if (f)
        return f;

};

 

To use it, you must specify a document, a pdfPresetName, and the path to the final pdf.

var exportedPDFFile = saveDocumentAsPDF(
    {
        doc: app.activeDocument,
        pdfPresetName: '[High Quality Print]',
        path: '~/Desktop/test.pdf'
    }
);

I hope that helps. - Mark

Charu Rajput
Community Expert
Charu RajputCommunity ExpertCorrect answer
Community Expert
January 6, 2023

I got it, it is artboardRange, I never noticed that it will not keep pdf document open.

Thank you @m1b 🙂

 

Best regards
m1b
Community Expert
Community Expert
January 6, 2023

Yes it seems to be the only thing that tells Illustrator I want to "Save a copy", vs "Save as". - Mark