When adding a Document in Adobe Illustrator's Extendscript is there a way to set it's name?

Viewed 612

Everytime I run my script and add a document the default filename is "Untitled-x*". I would like to be able to provide a default name for the document. Is there a way to do this using Extendscript?

Here is how I'm adding a Document currently:

var doc = app.documents.add(DocumentColorSpace.RGB, width, height, 1);

I was hoping for a parameter to provide a name, but the Javascript Illustrator Extendscript PDF reference doesn't show anything under "Document".

3 Answers

There is the object DocumentPreset it has the property title. Here's how it works:

var docPreset        = new DocumentPreset;
    docPreset.colorMode = DocumentColorSpace.RGB;
    docPreset.title  = "Your Title Is Here";
    docPreset.width  = width;
    docPreset.height = height;

// Startup Preset Options:
//
// 0 - Print
// 1 - Film & Video
// 2 - Web
// 3 - Art & Illustration
// 4 - Mobile
// 5 - Film and Video
var presetArt = app.startupPresetsList[3];

var doc = app.documents.addDocument(presetArt, docPreset);

The reference you linked shows a name property for the Document object, but as you can see, it is readonly. In cases like this, it often helps to think about how the same thing is achieved in the UI.

The only way to name an Illustrator document in Illustrator's UI is to save it somewhere under a certain name. And that's exactly what you will have to do in your script as well:

var doc = app.documents.add(DocumentColorSpace.RGB, width, height, 1);
doc.saveAs(File("~/Desktop/myIllustratorDoc.ai");

Document can have name only after saving it once. And you can save the document as saveAs command as mentioned by @mdomino

Related