Skip to main content
Known Participant
October 12, 2017
Question

"Save A Copy" scripting problem

  • October 12, 2017
  • 3 replies
  • 5931 views

I'm trying to create a "Save As PDF" script for Illustrator that will allow me to keep my active document as an Illustrator (.AI) file open.

In my workflow, I need to create compressed PDF version of my Illustrator file for clients, WHILE I'm working on an Illustrator file. In other words, I need to keep the .AI file open and have the .PDF file saved in the background.

Currently this is how I do this:

1. Choose "Save A Copy..."

2. Choose PDF

3. Choose PDF Preset from the drop-down menu.

4. Use the keyboard to delete " copy" which is automatically appended.

5. Choose "Save  PDF"

Of course I can easily just choose "Save As.." and choose pdf, but then my file is no longer an Illustrator (.ai) file.

I've been able to code a script which will automatically save my document as a pdf with a specific preset but it only works on the "Save As..." command. I need it to work on the "Save A Copy..." command. If I could just figure out a way to automatically remove the " copy" suffix that's automatically appended to the filename it would be a breeze.

Can you assist me in figuring this out?

This topic has been closed for replies.

3 replies

Inspiring
October 18, 2019

Based on the replies above, here is a script that does the export in one step.

 

 

function exportPDF_IL(pdfPath, preset ) {

        var docRef = app.activeDocument;
        var options = new PDFSaveOptions();
        
        if ((preset !== undefined) && (preset.length > 0))
            options.pDFPreset = preset;

        var numArtboards = docRef.artboards.length;
        var range = "1";
        if (numArtboards > 1) range += "-" + numArtboards;
            
        // If you supply a range then the saveAs becomes a saveACopy.
        pdfSaveOptions.artboardRange = range;

        // Prevent open after export
        pdfSaveOptions.viewAfterSaving = false;

        var myFile = new File(pdfPath);
        docRef.saveAs(myFile, pdfSaveOptions);
};

exportPDF_IL( "e:\\pdfs\\sample.pdf", "");

 

Participant
November 24, 2023

Grfeat script. Please, correct the "pdfSaveOptions" - has to be: "PDFSaveOptions"... May confuse...

Known Participant
March 12, 2018

Thanks for this script. I've been using it for a few days and I really like it. However I remember now one of the reasons why I've been asking Adobe to add an Export PDF function to Illustrator as a new feature. Using your script (or one that I wrote myself a while back) causes the active document to lose it's UNDO history. Since we're saving the active document to PDF then reopening the original document from disk, all history is lost. It would be great if we could truly Export the document instead of Save As. If Adobe would just write modify their Export menu item to allow for this, all would be good.

I've been using your script, I just make sure I'm happy with the current state before running it, since I will lose the history.

Silly-V
Legend
March 12, 2018

Well, you have to do this via a dynamic action:

Creating a dynamic action to use with app.doScript() method.

Participant
March 8, 2018

var doc = app.activeDocument; // Name of the active document

var original_file = doc.fullName; // we need to store the original file and path

var pdfOption = 'Ansichts-PDF 150dpi'; // Name of PDF settings

var pdfSuff = '_lowres.pdf'; // Suffix for PDF

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

var extension = "";

if (arr.length>1) extension = "." + arr.pop();

var filename = arr.join("."); // Just generate a clean file name

var name_pdfpreview = filename + pdfSuff; // new name and suffix for PDF

var export_folder = "~/Desktop/"; // Define path where to save PDF

// save as PDF and tell function which options to use and how to name the PDF

function saveCopyAsPDF (setPDF, namePDF) {

        var destFolder = null;

            destFolder = export_folder;

        var options = null;   

            options = new PDFSaveOptions ();

            options.pDFPreset = setPDF;

            options.viewAfterSaving = false; // just to be sure not to open Acrobat

        var targetFile = null;

            targetFile = new File (destFolder + namePDF);   

        doc.saveAs (targetFile, options); // here we save the PDF

        doc.close (); // now we close it in Illustrator ...

        app.open (File (original_file)); // ... and re-open the Illustrator file

}

saveCopyAsPDF (pdfOption, name_pdfpreview);

Hope this will help.

It's a workaround: just store the original file info in a variable, write the pdf, close it, and return to the orginal file.

Known Participant
March 8, 2018

Oh wow! Thanks for writing that script.

Question: I've changed the variable "pdfOption" to one of my custom settings but I'm getting an error when I run the script.

Error 1200:an Illustrator error occurred: 1129270854 ('FNOC') on Line 25

-> doc.saveAs (targetFile, options); // here we save the PDF

Any ideas what's causing the error?