Copy link to clipboard
Copied
Not good at writing scripts, was hoping someone could write a simple illustator script for creating multiple layers (via a dialog box) and then pulling from a word file or text file for the naming of each layer (via a dialog box).
Maybe it's not simple? Don't really know. I often get client word docs for various artwork names or a message schedule and need a layer created for each...sometimes this is hundreds of layers, very tedious by hand. I found and modified a script for creating multiple layers, but still need the naming part. Any help much appreciated! Script I modified below.
//Apply to myDoc the active document
var layerName = LayerOrderType;
var myDoc = app.activeDocument;
//define first character and how many layers do you need
var verticalTolerance = 0;
verticalTolerance = prompt ("How many layers would you like to create?",10); //points
var layerName
var numberOfLayers=verticalTolerance;
//Create the layers
for(var i=1; i<=numberOfLayers; i++)
{ var layerName = "Layer Name"; var myLayer = myDoc.layers.add(); myLayer.name = layerName; }
2 Correct answers
Try the following, it will ask to select a txt file as input and use each line of this file as the name for the layers that are being created
function fileFilter(f)
{
var reg = new RegExp("\.(txt)$")
if(reg.test(f.name.toString()))
return true
else
return false
}
var myDoc = app.activeDocument;
var numberOfLayers = prompt ("How many layers would you like to create?",10);
var fl = File.openDialog("Choose the text with layer names", fileFilter, false)
var layerName = "Layer Name"
if(fl)
{
...
Hi @Emac_trademark, you already have good answers, but since you are having a go at scripting, for your learning, here is another approach that I like. I make a dialog box and you paste your text into it. The dialog itself can be repurposed by supplying different parameters including the callback function that actually makes the layers. It's a quick script that I haven't tested much at all, so hopefully works ok.
- Mark
// by m1b, here: https://community.adobe.com/t5/illustrator-discussions/illus
...
Explore related tutorials & articles
Copy link to clipboard
Copied
Try the following, it will ask to select a txt file as input and use each line of this file as the name for the layers that are being created
function fileFilter(f)
{
var reg = new RegExp("\.(txt)$")
if(reg.test(f.name.toString()))
return true
else
return false
}
var myDoc = app.activeDocument;
var numberOfLayers = prompt ("How many layers would you like to create?",10);
var fl = File.openDialog("Choose the text with layer names", fileFilter, false)
var layerName = "Layer Name"
if(fl)
{
fl.open("r")
for(var i=1; i<=numberOfLayers; i++)
{
var ln = fl.readln()
var layerName = ln ? ln : layerName;
var myLayer = myDoc.layers.add(); myLayer.name = layerName;
}
fl.close()
}
-Manan
Copy link to clipboard
Copied
based on your code
another option
var myDoc = app.activeDocument;
var lay=myDoc.layers
var textFile = File.openDialog("Select text file", "*.txt")
textFile.open("r")
var ln = textFile.read()
var ln2=ln.split("\n");
textFile.close()
for(var i=0; i<ln2.length; i++){
var layerName = ln2[i]
var myLayer = lay.add();
myLayer.name = layerName;
}
Copy link to clipboard
Copied
Thank you Manan Joshi! Works great!
Copy link to clipboard
Copied
Sorry, thought I was replying for above response! Thank you Gerrson! It didn't let me pick a text file, but no matter, the other 2 responses work great, thank you for response!
Copy link to clipboard
Copied
Thank you Manan Joshi! Works great!
Copy link to clipboard
Copied
Hi @Emac_trademark, you already have good answers, but since you are having a go at scripting, for your learning, here is another approach that I like. I make a dialog box and you paste your text into it. The dialog itself can be repurposed by supplying different parameters including the callback function that actually makes the layers. It's a quick script that I haven't tested much at all, so hopefully works ok.
- Mark
// by m1b, here: https://community.adobe.com/t5/illustrator-discussions/illustrator-script-for-multiple-layer-creation-and-naming/m-p/12451899
dialogWithTextArea({
// explanatory text
intro: 'Enter layer names here, delimited by returns',
// the starting text of text area (optional)
text: 'Layer A\nLayer B\nLayer C',
// the button title (optional)
buttonTitle: 'Make Layers',
// the window title (optional)
windowTitle: 'Make layers',
// the function that executes when button clicked (optional)
callback: makeLayersFromTextList
});
function makeLayersFromTextList(text) {
var layerNames = text.split('\n');
for (var i = layerNames.length - 1; i >= 0; i--) {
if (layerNames[i].length > 0)
makeLayer(layerNames[i]);
}
}
function makeLayer(name) {
// makes a new layer in active document, if not present
var newLayer;
try {
newLayer = app.activeDocument.layers.getByName(name);
} catch (e) {
// no layer by that name, so make it
newLayer = app.activeDocument.layers.add()
newLayer.name = name;
}
return newLayer;
}
function dialogWithTextArea(p) {
var dialog = makeUI(p);
dialog.center();
dialog.show();
dialog = null;
function makeUI(p) {
var windowTitle = p.windowTitle || '',
text = p.text || '',
buttonTitle = p.buttonTitle || 'OK',
width = p.width || 350,
w = new Window("dialog", windowTitle);
if (p.intro != undefined)
w.add('statictext', undefined, p.intro, { alignment: 'left' });
var textArea = w.add('edittext', undefined, text, { multiline: true, scrolling: true }),
row = w.add("Group {orientation:'row', alignment:['right','top'] }");
if (p.callback != undefined)
row.add('button', undefined, 'Cancel', { name: 'cancel' });
var okButton = row.add('button', undefined, buttonTitle, { name: 'ok' });
textArea.preferredSize.height = w.maximumSize.height - 400;
textArea.preferredSize.width = width;
textArea.minimumSize.height = 250;
textArea.minimumSize.width = 200;
okButton.onClick = function () {
// pass the text to the callback function
if (p.callback != undefined) p.callback(textArea.text);
w.close(1);
};
return w;
}
}
Copy link to clipboard
Copied
Thank you M1b! This is EXACTLY what I was hoping for...ding, ding, ding, we have a winner! And thanks for having the explanation for the scripting code, hopefully will help me in the future!
Copy link to clipboard
Copied
Hi @m1b - I used your script on a regular base for my work and this week, after upgrading to AI-2022 it doesnt work anymore. 😥
Seems like Adobe changed some switches for his scripting method...
Script now terminates with message:
| Error 2: LayerOrderType is undefined.
| Line 3:
| -> var layerName = LayerOrderType
Would be very nice from you to post an update for this script! 😍
Copy link to clipboard
Copied
Hi @sebastianf15538645, glad you are getting some use out of my script! I tested just now and it still works fine for me though.
Your error is because you are using "LayoutOrderType" which is an enum used with ExportOptionsFlash.layerOrder. My guess is that Adobe have killed off ExportOptionsFlash object—certainly in my test just now it was gone. But nobody uses Flash any more do they?
So, assuming you are using LayoutOrderType just out of convenience, you can just make your own declaration to match Adobe's. Put this in your script before line 3:
var LayerOrderType = {
BOTTOMUP: 1,
TOPDOWN: 2
};
If that doesn't work, I'll need some more info, including some code.
- Mark
Copy link to clipboard
Copied
Yaaaaah! That's it! 😍
Now it works again like a charm! 😊
I checked on the current Adobe Scripting Guide, page 123 - but just like Emac I'm not really adept with scripting skills - I couldn't get the wording right for this param...
So a big THANK YOU! for your quick and expedient help, @m1b !!
👍 👍 👍 👍 👍
Copy link to clipboard
Copied
Glad it's working for you. 🙂
Copy link to clipboard
Copied
Mark, you are saying that your script is a quick one, but as far as I can see it works pretty well.
I like the approach with a dialog box.
Thanks for sharing it.
Copy link to clipboard
Copied
Hello, could you tell me: how to make, a new layer be made from a selected object (which is already in the document) and the layer is called the name of the object? Thank you )
Copy link to clipboard
Copied
I've made this, to transform groups into layers:
// Get the current selection
var selection = app.activeDocument.selection;
// Check if there is a selection
if (selection.length > 0) {
// Get the first selected item (assuming you are only dealing with one item at a time)
var selectedItem = selection[0];
// Check if the selected item is a group
if (selectedItem.typename === "GroupItem") {
// Get name of item, or ask for name
if (selectedItem.name) {
var layername = selectedItem.name;
}else{
var layername = prompt("Layer name?");
}
// Get the parent layer of the selected item
var parentLayer = selectedItem.layer;
// Add layer and name it
var newLayer = parentLayer.layers.add();
newLayer.name = layername;
// Move new layer to the location of the selected item
newLayer.move(selectedItem,ElementPlacement.PLACEAFTER);
selectedItem.move(newLayer,ElementPlacement.INSIDE);
// I want to ungroup, but this does't work
app.executeMenuCommand('ungroup');
} else {
alert("Not a group.");
}
} else {
alert("No item selected.");
}

