Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Give New File a name with Script

Engaged ,
Jun 23, 2021 Jun 23, 2021

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";

 

TOPICS
Scripting
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Contributor , Jun 23, 2021 Jun 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);

Translate
Adobe
Community Expert ,
Jun 23, 2021 Jun 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. 

Screenshot 2021-06-23 at 13.07.36.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 23, 2021 Jun 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.  

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jun 23, 2021 Jun 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 23, 2021 Jun 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 23, 2021 Jun 23, 2021

Gersson,

 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 23, 2021 Jun 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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 23, 2021 Jun 23, 2021

@GerssonDelgado would you know how to also code it so the Units are set to Inches? and the art board size would be 90" x 90"?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 23, 2021 Jun 23, 2021

@BryanPagenkopf 

Check out this!

 

var doc = app.activeDocument;
var PTS_IN = 72;
var width = 90 * PTS_IN;
var height = 90* PTS_IN;
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;
docPreset.units = RulerUnits.Inches;
docPreset.width = width;
docPreset.height = height;
var doc = app.documents.addDocument(preset, docPreset, false);

 

let me know any question  🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 23, 2021 Jun 23, 2021
LATEST

Love it!!!   Thank you!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jun 23, 2021 Jun 23, 2021

Hi Bryan,

 

I have added a prompt box for you to enter a job number.

var myJobID = prompt("Enter Job#","");
if(myJobID == null)
{ 
exit();
}
if(myJobID == "")
 { 
alert ("Error!\nNo Job# Entered.");
exit();
}
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/'+ myJobID + '.ai'));

 

Regards,

Mike

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines