Skip to main content
Inspiring
June 23, 2021
Answered

Give New File a name with Script

  • June 23, 2021
  • 10 replies
  • 1210 views

Hello,

 

I have a simple script that creates a new document and a couple of Layers.  How would I add in code to be able to give the new document a name other than "Untitled-1"?

 

Thank you!

 

 

var myDocument = app.documents.add();

// PROD Layer
var prodLayer = myDocument.layers.add();
    prodLayer.name = "PROD";

// SPEC Layer
var proofLayer = myDocument.layers.add();
    proofLayer.name = "PROOF";

 

Correct answer GerssonDelgado

@BryanPagenkopf 

try this,

 

var doc = app.activeDocument;
var presets = app.startupPresetsList;
var preset = presets[0];
var enterName=prompt("Please Type document Name");
var docPreset = new DocumentPreset();
//add doc Name
docPreset.title = enterName;
var doc = app.documents.addDocument(preset, docPreset, false);

10 replies

GerssonDelgado
Inspiring
June 23, 2021

hey @BryanPagenkopf 

add new document with name, check out those lines maybe can help you!

var doc = app.activeDocument;
var presets = app.startupPresetsList;
var preset = presets[0];
var docPreset = new DocumentPreset();
//add doc Name
docPreset.title = "Nombre de documento";

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

 

best Regards

Gersson Delgado

Inspiring
June 23, 2021

Gersson,

 

That worked, however, the name is hardcoded and I would need to enter a job number.

GerssonDelgado
GerssonDelgadoCorrect answer
Inspiring
June 23, 2021

@BryanPagenkopf 

try this,

 

var doc = app.activeDocument;
var presets = app.startupPresetsList;
var preset = presets[0];
var enterName=prompt("Please Type document Name");
var docPreset = new DocumentPreset();
//add doc Name
docPreset.title = enterName;
var doc = app.documents.addDocument(preset, docPreset, false);

Legend
June 23, 2021

Hello,

You can use the saveAs giving the file a path and name to save to.

var myDocument = app.documents.add();

// PROD Layer
var prodLayer = myDocument.layers.add();
    prodLayer.name = "PROD";

// SPEC Layer
var proofLayer = myDocument.layers.add();
    proofLayer.name = "PROOF";
    myDocument.saveAs(File('~/Desktop/myDocument.ai'));

Regards,

Mike

c.pfaffenbichler
Community Expert
Community Expert
June 23, 2021

I am admittedly not strong in Illustrator Scripting but I would have assumed this to be necessary, alas apparently you need to define the name when saving the file.  

The Documents-method »add« does not seem to include a »name«-parameter. 

femkeblanco
Legend
June 23, 2021

@c.pfaffenbichler is right.  A document's "name" property is read-only.  But if you save the document, you give it a file name.