Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Illustrator Script for Multiple Layer Creation and Naming

Community Beginner ,
Oct 14, 2021 Oct 14, 2021

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

TOPICS
Scripting

Views

169

Likes

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

Enthusiast , Oct 15, 2021 Oct 15, 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/illus...

Likes

Translate

Translate
Adobe Community Professional ,
Oct 14, 2021 Oct 14, 2021

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

Likes

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
Participant ,
Oct 15, 2021 Oct 15, 2021

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

Likes

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 Beginner ,
Oct 19, 2021 Oct 19, 2021

Copy link to clipboard

Copied

Thank you Manan Joshi!  Works great!

Likes

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 Beginner ,
Oct 19, 2021 Oct 19, 2021

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!

Likes

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 Beginner ,
Oct 19, 2021 Oct 19, 2021

Copy link to clipboard

Copied

Thank you Manan Joshi!  Works great!

Likes

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
Enthusiast ,
Oct 15, 2021 Oct 15, 2021

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

 

Likes

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 Beginner ,
Oct 19, 2021 Oct 19, 2021

Copy link to clipboard

Copied

LATEST

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!

Likes

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
Adobe Community Professional ,
Oct 17, 2021 Oct 17, 2021

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.

 

Likes

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