Skip to main content
Participating Frequently
July 6, 2020
Answered

Saving layer names as file names

  • July 6, 2020
  • 7 replies
  • 2814 views

Hello everyone and thank you in advance for your time.

I have been trying to figure this out on my own with very little javascript experience. I'll try to explain this as best as i can. A friend created a CSV file to use, created variables and created a batch from the action menu. The issue is, illustrator only gives us 'File + Number', 'File + Data Set Name' and 'Data Set Name' which isn't anything i want shown as the file name when i go to save. I would like certain layers saved as the file name. The variables create a number of different layers but i would like to use the layers 'invoice_#' and 'customer' as my file name so it would look something like this "123_customer.pdf". Is this possible with a script?

This topic has been closed for replies.
Correct answer femkeblanco

Try this:

 

var part1 = app.activeDocument.layers["Invoice_#"].textFrames[0].contents;
var part2 = app.activeDocument.layers["Customer"].textFrames[0].contents;
var address = "‪/C/Users/asparaccio/Desktop/" + part1 + "_" + part2;
var file1 = new File(address);
var PDF = new PDFSaveOptions;
app.activeDocument.saveAs(file1, PDF);

 

The last one only saved as "_.pdf" because the textFrames are not named (but showing their contents). 

7 replies

pixxxelschubser
Community Expert
Community Expert
July 6, 2020

Here is a small modification for saving in your wished path with the wished name. (Hope you are under Windows - like I said before: to little informations)

 

var aDoc = app.activeDocument;
var part1 = aDoc.layers["Invoice_#"].textFrames[0].contents;
var part2 = aDoc.layers["Customer"].textFrames[0].contents;
var address = aDoc.path + "/" + part1 + "_" + part2 + ".pdf";
var File2save = new File(address);
var PDFopts = new PDFSaveOptions;
app.activeDocument.saveAs(File2save, PDFopts);

 

 

If that works for you

have fun

😉

pixxxelschubser
Community Expert
Community Expert
July 6, 2020

😄

The problem was not to save the file. You can find dozens of methods here in the forum.

 

Glad that my snippet was helpful to find the wished name and helpful to complete the saving routine.

Participating Frequently
July 6, 2020

I was searching for 3 hours last night about something else and couldn't find a solution, so instead of figuring it out on my own (at work) i thought it would be easier to ask the experts and save some time. Thanks again!

pixxxelschubser
Community Expert
Community Expert
July 6, 2020

Yes. That is how is works.

😉

My snippet gives you only the name you want, but nothing more so far.


There are still too many open questions:

Was your file already been saved under a different name? - As AI or as PDF?

What save options?

Should the complete file be saved under the name "part1_part2.pdf"? - Or just the layers as a new file?

Where? On the same file path as your other file?

 

 

Participating Frequently
July 6, 2020

The file is a template theat i'm working off of called invoice.pdf

It's a Save As:

Saved under the name "part1_part2.pdf" but also a new PDF file

Same file path

 

I am going to use the script in an action

 

 

pixxxelschubser
Community Expert
Community Expert
July 6, 2020

You can get the required name in this way:

var aDoc = app.activeDocument;
var part1 = aDoc.layers.getByName("Invoice_#").textFrames[0].contents;
var part2 = aDoc.layers.getByName("Customer").textFrames[0].contents;
alert(part1+"_"+part2+".pdf")

 

Have fun

😉

Participating Frequently
July 6, 2020

I actually got this to work but it only shows in a dialogue box but doesn't actually save the PDF which is what i need.

femkeblanco
femkeblancoCorrect answer
Legend
July 6, 2020

Try this:

 

var part1 = app.activeDocument.layers["Invoice_#"].textFrames[0].contents;
var part2 = app.activeDocument.layers["Customer"].textFrames[0].contents;
var address = "‪/C/Users/asparaccio/Desktop/" + part1 + "_" + part2;
var file1 = new File(address);
var PDF = new PDFSaveOptions;
app.activeDocument.saveAs(file1, PDF);

 

The last one only saved as "_.pdf" because the textFrames are not named (but showing their contents). 

pixxxelschubser
Community Expert
Community Expert
July 6, 2020

Hi @asparaccio,

thank you for the image.

 

 

@asparaccio wrote: "… but i would like to use the layers 'invoice_#' and 'customer' as my file name so it would look something like this "123_customer.pdf". Is this possible with a script?"

 

One question: Where the part "123_" comes from?

 

edited by pixxxel_schubser

Strange. My browser didn't show the last answers. Now I can see - the number is written in (the only) text frame in layer "Invoice_#"

Participating Frequently
July 6, 2020

Yes sorry, it's text. I tried the script but it only saves as "_.pdf". so i must be missing something.

 

var item1 = app.activeDocument.layers[1].pageItems[0];
var item2 = app.activeDocument.layers[2].pageItems[0];
var address = "‪/C/Users/asparaccio/Desktop/" + item1.name + "_" + item2.name;
var file1 = new File(address);
var PDF = new PDFSaveOptions;
activeDocument.saveAs(file1, PDF);

 

femkeblanco
Legend
July 6, 2020

The file name is the last part of the path.  So you can use a layer's name as the last part of the path.  For example, you can use the name of the layer which is active:  (you will need to fill in "..." in your path)

 

var layer1 = app.activeDocument.activeLayer;
var address = "‪/C/Users/.../Desktop/" + layer1.name;
var file1 = new File(address);
var PDF = new PDFSaveOptions;
app.activeDocument.saveAs(file1, PDF);

 

Or you can use a joining of the names of the first and second layers:

 

var layer1 = app.activeDocument.layers[0];
var layer2 = app.activeDocument.layers[1];
var address = "‪/C/Users/.../Desktop/" + layer1.name + layer2.name;
var file1 = new File(address);
var PDF = new PDFSaveOptions;
app.activeDocument.saveAs(file1, PDF);

 

Et cetera.  (Of course, you can also enter the layers' names directly into the path if you want.)

Participating Frequently
July 6, 2020

Thank You for that. I didn't expand the layers and it's actually the sub layer information i need. Very sorry i didn't mention that before. I hope it doesn't make it harder.

 

 

femkeblanco
Legend
July 6, 2020
Going by your photo, you want to target the first sublayer in the second and third layers. So try this:  (again fill in "..." in your path)

 

var sublayer1 = app.activeDocument.layers[1].layers[0];
var sublayer2 = app.activeDocument.layers[2].layers[0];
var address = "‪/C/Users/.../Desktop/" + sublayer1.name + "_" + sublayer2.name;
var file1 = new File(address);
var PDF = new PDFSaveOptions;
app.activeDocument.saveAs(file1, PDF);

 

 

pixxxelschubser
Community Expert
Community Expert
July 6, 2020

For clarification, please show us a screenshot of the Layers panel with the layer names. Or upload a sample file (without confidential data) to a host of your choice (e.g. Dropbox ...) and link to here.

Participating Frequently
July 6, 2020

Hello,

I attached a photo of the layers