Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save file in Illustrator with Javascript

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?

like image 716
dbonneville Avatar asked May 30 '12 05:05

dbonneville


Video Answer


1 Answers

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

var originalInteractionLevel = userInteractionLevel;
userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

...

userInteractionLevel = originalInteractionLevel;
like image 55
Marcus Downing Avatar answered Oct 25 '22 05:10

Marcus Downing