Save file in Illustrator using Javascript

I am trying to save a file in Illustrator using Javascript, but I get an error all the time.

Here is what works, but 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 );
    }
}

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

var dest = "exports/testme.pdf";

A dialog will appear with ".pdf", highlighted, waiting for input inside the "export" folder that I have already created. I can print something and it will save, but ignore the testme.pdf file name specified in the code. I can type “cheese” over the selected “.pdf”, it knows what I want and it will save “cheese.pdf” in the “export” folder.

I also tried them with no luck:

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

etc. etc.

What am I missing?

+5
2

saveAs , userInteractionLevel:

var originalInteractionLevel = userInteractionLevel;
userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

...

userInteractionLevel = originalInteractionLevel;
+5

,

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

, , script

    var path = app.activeDocument.path;
    var exportFolder = Folder(path + "/exports");
    if(!exportFolder.exists){
        exportFolder.create();
    }
    var dest = exportFolder + "/testme.pdf";
0

All Articles