Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

app.activeDocument.saveAs but without opening the saved file

Community Beginner ,
Jan 05, 2023 Jan 05, 2023

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?

 

 

TOPICS
Import and export , Scripting
1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Jan 05, 2023 Jan 05, 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} optio
...
Translate
Community Expert , Jan 05, 2023 Jan 05, 2023

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

Thank you @m1b 🙂

 

Translate
Adobe
Community Expert ,
Jan 05, 2023 Jan 05, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 05, 2023 Jan 05, 2023

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

Thank you @m1b 🙂

 

Best regards
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 05, 2023 Jan 05, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 05, 2023 Jan 05, 2023

Thanks, this also solved another problem i would have when some times i need to save specific panels like "1,2,5".
My code ended like that:

function saveAsPDF() {
    var allFiles = new Folder(app.activeDocument.path).getFiles();
    var folder;
    for (var i = 0; i < allFiles.length; i++) {

        thisFile = Folder(allFiles[i]);
    
        if (thisFile instanceof Folder) { 
            var folderName = decodeURI(thisFile.name)
            if((app.activeDocument.name + "").indexOf(folderName) != -1){
                alert("existe");
                folder = thisFile;
            }
        }
    }
 
    if(folder == undefined) {
        folder = new Folder(app.activeDocument.path + "/" + app.activeDocument.name.split('.')[0]);
        folder.create(SaveOptions.DONOTSAVECHANGES);
    }

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

    var pdfOptions = new PDFSaveOptions();
    pdfOptions.viewAfterSaving = false;
    var artoboardRange = prompt("Definir intervalo de pranhcetas:", "1-4");

    if(artoboardRange ==  null) return;

    try {
        pdfOptions.artboardRange = artoboardRange;
        app.activeDocument.saveAs(pdfFile, pdfOptions, false);
        alert("Salvo como PDF");
    } catch (err) {
        pdfOptions.artboardRange = '1-' + app.activeDocument.artboards.length;
        app.activeDocument.saveAs(pdfFile, pdfOptions, false);
        alert("Intervalo ilegal, salvando com intervalo padrão.\n\nSalvo como PDF");
    }
    pdfOptions.pDFPreset = "[Impressão de alta qualidade]";
}
saveAsPDF();

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 05, 2023 Jan 05, 2023
LATEST

Nice! And well done on your scripting!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 05, 2023 Jan 05, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 05, 2023 Jan 05, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines