Skip to main content
Known Participant
April 25, 2018
Answered

Open an AIT and give it a name

  • April 25, 2018
  • 2 replies
  • 1651 views

Hello everyone,

I'm struggling with this script. I don't know if it's possible and I just can't find it in the documentation, or it's just not possible simply, as my approach is. I've searched the forum, but could not find anything similar to this.

I run a script to open a template, but I'd like to give it a name when it's opened. The name is something I choose with a prompt. The script I have below opens the template, asks for the name, but doesn't give me an error on the naming. The script works just fine without the naming convention.

Is this possible without much scripting of like saving it as an AI to a folder and reopening it and then renaming it?

Thanks everyone.

var newName;
newName = prompt("What would you like to name this file?",0);

function openFile()
{
var fileRef = new File("/s/scripts/Drawing (7x9).ait")
app.open(fileRef);
}
openFile();

var doc = app.activeDocument;
File(doc).rename(newName);

This topic has been closed for replies.
Correct answer Disposition_Dev

try this instead. It should still do exactly what the version you just posted does, but i removed a bunch of unnecessary stuff and enclosed everything inside  the function scope to prevent stubborn global variables from being created and clogging up the JS engine.

function test() 

    var newName = prompt("What would you like to name this file?",0);  }

    var myAitFile = File("/s/MyFolder/template/Template-7x3.ait"); 

    app.open(myAitFile); 

    var destFolder = Folder("/s/MyFolder/graphics");

    if(!destFolder.exists)

    {

        destFolder.create();

    }

    var dest = File(destFolder + "/" + newName + "/" + newName + ".ai"); 

    app.activeDocument.saveAs(dest); 

test();

2 replies

Disposition_Dev
Community Expert
April 26, 2018

Correct. the name of the active document is read only. The only way is to either rename the file before you open it (i think you can do that) or to saveAs and set the name to whatever you want.

Known Participant
April 26, 2018

Ok, this works for me. It opens the template file, names it what I type into the prompt, and saves it to a new folder that is created with the same name of the AI file.

Thanks Williamadowling for the help.

var newName;
var newName = prompt("What would you like to name this file?",0);
var folder1 = new Folder("/s/MyFolder/graphics/" + newName);
    if(!folder1.exists) folder1.create();
var myAitFile;
var destFolder;
var destFileName;
var dest;

function test()
{
    var myAitFile = File("/s/MyFolder/template/Template-7x3.ait");
    app.open(myAitFile);
    var destFolder = Folder("/s/MyFolder/graphics");
    var dest = File(destFolder + "/" + newName + "/" + newName + ".ai");
    app.activeDocument.saveAs(dest);
}
test();

MichaelK ¯\_(ツ)_/¯
Disposition_Dev
Disposition_DevCorrect answer
Community Expert
April 26, 2018

try this instead. It should still do exactly what the version you just posted does, but i removed a bunch of unnecessary stuff and enclosed everything inside  the function scope to prevent stubborn global variables from being created and clogging up the JS engine.

function test() 

    var newName = prompt("What would you like to name this file?",0);  }

    var myAitFile = File("/s/MyFolder/template/Template-7x3.ait"); 

    app.open(myAitFile); 

    var destFolder = Folder("/s/MyFolder/graphics");

    if(!destFolder.exists)

    {

        destFolder.create();

    }

    var dest = File(destFolder + "/" + newName + "/" + newName + ".ai"); 

    app.activeDocument.saveAs(dest); 

test();

pixxxelschubser
Community Expert
April 25, 2018

Hi michaelk97081953​,

do you really want to rename the *.ait?

Or do you want to work with that template and save as eg *.ai?

In the second case: how about with saveAs()

Known Participant
April 25, 2018

Ah, yes. The second option. I don't want to rename the AIT file itself. Just the file when it opens in Illustrator, which comes in as Untitled-01 or something similar. I'd like it named when it opens in Illustrator whatever I type in the prompt.

I played around with the saveAs() but couldn't get that to work, but I guess I was thinking of just renaming the new document, and saveAs will actually save the file to disk. I have an action that does a few things and saves and closes at the end.

How would a saveAs look? Maybe I'm using it incorrectly.

Thanks pixxxel

MichaelK ¯\_(ツ)_/¯
Disposition_Dev
Community Expert
April 25, 2018

function test()

{

    var myAitFile = File("path/to/file");

    app.open(myAitFile);

    var destFolder = Folder("path/to/dest/folder");

    var destFileName = "My_Dest_File.ai";

    var dest = File(destFolder + "/" + destFileName);

    app.activeDocument.saveAs(dest);

}

test();