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

Open an AIT and give it a name

Explorer ,
Apr 25, 2018 Apr 25, 2018

Copy link to clipboard

Copied

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

MichaelK ¯\_(ツ)_/¯
TOPICS
Scripting

Views

872

Translate

Translate

Report

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

Community Expert , Apr 26, 2018 Apr 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"

...

Votes

Translate

Translate
Adobe
Community Expert ,
Apr 25, 2018 Apr 25, 2018

Copy link to clipboard

Copied

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()

Votes

Translate

Translate

Report

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
Explorer ,
Apr 25, 2018 Apr 25, 2018

Copy link to clipboard

Copied

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 ¯\_(ツ)_/¯

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 25, 2018 Apr 25, 2018

Copy link to clipboard

Copied

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();

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 25, 2018 Apr 25, 2018

Copy link to clipboard

Copied

michaelk97081953​

I don't know why a prompt or a dialog should 'break' the script - but maybe this could be also a solution for you

var fileRef = new File("/s/scripts/Drawing (7x9).ait");

//var fileRef = new File("/c/Program Files/Adobe/Adobe Illustrator CC 2018/Cool Extras/de_DE/Vorlagen/Leere Vorlagen/Etiketten.ait");

app.open(fileRef);

// do whatever you want

new File(fileRef.path+"/NewName").saveDlg ("enter a name and save", "*.ai");

If so, have fun

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 26, 2018 Apr 26, 2018

Copy link to clipboard

Copied

i think he's saying that he's inputting some text into the prompt dialog, then attempting to use the resulting string to rename the file via the File.rename() method.

Votes

Translate

Translate

Report

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
Explorer ,
Apr 26, 2018 Apr 26, 2018

Copy link to clipboard

Copied

I was just working on it guys. Pixxxel, yours, as it stands, is weird because it doesn't save the file, even though I click Save. No file is created.

William, yours does more or less what I want it to do. I would have it to save in the same location every time, which should be fine. I'm thinking I can make the destFolder a variable from a prompt if I want, as well as the destFileName. This may be the best option. I was just thinking maybe I could prompt for the file name, and create a folder with that same name and save the file in that new folder. I'm going to give it a try.

I'm guessing there jut isn't a way to change the name of the active document then? Or can it not be done in the same JavaScript? I thought it was possible.

MichaelK ¯\_(ツ)_/¯

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 26, 2018 Apr 26, 2018

Copy link to clipboard

Copied

michaelk97081953  schrieb

I was just working on it guys. Pixxxel, yours, as it stands, is weird because it doesn't save the file, even though I click Save. No file is created.

William, yours does more or less what I want it to do. I would have it to save in the same location every time, which should be fine. I'm thinking I can make the destFolder a variable from a prompt if I want, as well as the destFileName. This may be the best option. I was just thinking maybe I could prompt for the file name, and create a folder with that same name and save the file in that new folder. I'm going to give it a try.

I'm guessing there jut isn't a way to change the name of the active document then? Or can it not be done in the same JavaScript? I thought it was possible.

You misunderstood my posting. That was only a possible way to go, because of I was not sure if you really want to go this way.

Note: saveDlg() does not save your file. It only opens a dialog, you can change the path and/or the filename of your active document (if necessary) – but the dialog only returns a file object. You have to save the file afterwards as next step in your script.

eg like this

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

var aDoc = app.open(fileRef);

// do whatever you want

var saveFol = new Folder(fileRef.parent.path +"/graphics");

if (!saveFol.exists) { saveFol.create(); }

var saveFile = new File(saveFol+"/NewName.ai").saveDlg ("Save as", "*.ai");

aDoc.saveAs(saveFile);

Try this snippet. This should do what you want.

If so, have fun

Votes

Translate

Translate

Report

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
Explorer ,
Apr 27, 2018 Apr 27, 2018

Copy link to clipboard

Copied

LATEST

I tried what you posted. I does work, but I think I'm going to keep what I have. Thanks for your interest in my question.

MichaelK ¯\_(ツ)_/¯

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 26, 2018 Apr 26, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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
Explorer ,
Apr 26, 2018 Apr 26, 2018

Copy link to clipboard

Copied

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 ¯\_(ツ)_/¯

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 26, 2018 Apr 26, 2018

Copy link to clipboard

Copied

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();

Votes

Translate

Translate

Report

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
Explorer ,
Apr 26, 2018 Apr 26, 2018

Copy link to clipboard

Copied

Hello William. Thanks for the comment. As you wrote it, it doesn't work. The debugger said there were too many braces. I removed the brace from the prompt line. It works, but then pops up a SaveAs dialog box to save my file, and doesn't make the folder.

I'm no JavaScript guru, but that's why I placed the var's before my function. Unless I'm missing something. For me, the way my script I placed above works the way I want.

Thanks

MichaelK ¯\_(ツ)_/¯

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 26, 2018 Apr 26, 2018

Copy link to clipboard

Copied

woops. you're right. the offending brace is at the end of line number 3:

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

Remove that brace and you should be fine.

The reason you want to enclose your variables inside the function scope is to prevent global variables from being created. You want the variables to get thrown out when the script is finished executing so they don't accidentally interfere with any other script you may be running. Additionally, anecdotal evidence on this forum has shown a marginal benefit in performance and reliability when keeping global variables out of the system.

If the other way is working for you, by all means keep using it. I'm just trying to give you the most reliable code possible. In the code you posted, you run a check to see whether folder1 exists, and if not, you create it, but then you don't use that folder again.

To figure out why you're getting the file save dialog, add an alert right before the "app.activeDocument.saveAs()" line that says this:

alert(dest.fsName);

Make sure that's a valid path.

Votes

Translate

Translate

Report

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