Save file in Illustrator with Javascript
Asked Answered
M

2

6

I'm trying to save a file in Illustrator using Javascript but I keep getting an error.

Here is what works, but is not what I want:

// save as
var dest = "~/testme.pdf";

saveFileToPDF(dest);

function saveFileToPDF (dest) {
    var doc = app.activeDocument;
    if ( app.documents.length > 0 ) {
        var saveName = new File ( dest );
        saveOpts = new PDFSaveOptions();
        saveOpts.compatibility = PDFCompatibility.ACROBAT5; 
        saveOpts.generateThumbnails = true; 
        saveOpts.preserveEditability = true;
        alert(saveName);
        doc.saveAs( saveName, saveOpts );
    }
}

The var "dest" saves the file to the root of my Mac user account. I simply want to save the file relative to the source document in a subfolder, so I tried this:

var dest = "exports/testme.pdf";

This brings up a dialogue with ".pdf" highlighted, properly awaiting input inside the "exports" folder that I already created. I can type something and it will save, but it ignores the file name "testme.pdf" that was specified in the code. I can type "cheese" over the highlighted ".pdf" it knows I want, and it will save "cheese.pdf" in the folder "exports".

I also tried these with no luck:

var dest = "exports/testme";
var dest = "/exports/testme.pdf";
var dest = "testme.pdf";

etc., etc.

What am I missing?

Musky answered 30/5, 2012 at 5:33 Comment(0)
A
6

To use saveAs without a dialog popping up, you need to use the global property userInteractionLevel:

var originalInteractionLevel = userInteractionLevel;
userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

...

userInteractionLevel = originalInteractionLevel;
Agronomy answered 25/8, 2012 at 8:11 Comment(0)
C
0

Since you want to save relative to your document, so first find the path for your current document as follows

var path = app.activeDocument.path;
var dest = path + "/exports/testme.pdf";

You can also check whether exports folder exists or not if not you can create with script as follows

    var path = app.activeDocument.path;
    var exportFolder = Folder(path + "/exports");
    if(!exportFolder.exists){
        exportFolder.create();
    }
    var dest = exportFolder + "/testme.pdf";
Chappy answered 9/9, 2017 at 19:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.