Skip to main content
Emac_trademark
Inspiring
October 14, 2021
Answered

Illustrator Script for Multiple Layer Creation and Naming

  • October 14, 2021
  • 4 replies
  • 5661 views

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

This topic has been closed for replies.
Correct answer m1b

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

 

4 replies

Participant
January 9, 2023

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 )

Known Participant
December 2, 2023

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.");
}
Kurt Gold
Community Expert
Community Expert
October 17, 2021

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.

 

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
October 16, 2021

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

 

Emac_trademark
Inspiring
October 19, 2021

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!

Community Expert
October 15, 2021

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

-Manan
GerssonDelgado
Inspiring
October 15, 2021

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