Skip to main content
Known Participant
December 2, 2013
Question

script to put artboard names in text boxes

  • December 2, 2013
  • 2 replies
  • 13799 views

Hi there. I have an Illustrator document with ~100 artboards, each artboard named by me. Each artboard has one textbox in it. Is there a way to take whatever name I assign to the artboard and print it into the text box inside that artboard? So in the end I would be able to see the name associated with each artboard on the artboard itself. Ideally I could run the script once (i.e. "turn it on"), have it populate all the text boxes with the artboard names, and then have it dynamically update the textboxes when I change an artboard name regardless of whether the file has been closed and opened since I originally ran the script.

Maybe this is more of a plugin? Either way I don't have any idea how to approach the problem, nor much scripting experience (I've tried several times, but never succeeded! ).

Petros_

2 replies

Participating Frequently
November 26, 2020

I created script which is working as expected:

// This script generates artboard names captions below each artboard.
// it take artboard name and generates TextArea under artboard aligned
// center in white color / font size 12pt.

var myDocument = app.activeDocument;
for (var i = 0; i < myDocument.artboards.length; i++) {

	var artboard = app.activeDocument.artboards[i];
	var leftA = artboard.artboardRect[0];
	var topA = artboard.artboardRect[1];
	var rightA = artboard.artboardRect[2];
	var bottomA = artboard.artboardRect[3];
	var widthA = Math.abs(rightA - leftA);  

	// For each artboard, add a new text area of sizes and position below
	var l = leftA;
	var t = bottomA >= 0 ? bottomA + 5 : bottomA - 5; // 5pt from the bottom edge of Artboard
	var w = widthA
	var h = 30;
	
	var rectRef = myDocument.pathItems.rectangle(t, l, w, h); // rectangle which will become text area
	var myLabel = myDocument.textFrames.areaText(rectRef);  

	var color = new RGBColor();
	color.red = 255;
	color.green = 255;
	color.blue = 255;

	// make it be area type instead of point type

	// fill it with the artboard name
	myLabel.contents = artboard.name;


	// this line controls the font size

	myLabel.textRange.characterAttributes.size = 12;

	if(myLabel.textRange.length > 0)
	{
		var current_paragraphs = myLabel.textRange.paragraphs;
		for (j = 0; j < current_paragraphs.length; j++)
		{
			if(current_paragraphs[j].characters.length > 0)
			{
				current_paragraphs[j].fillColor = color;
				current_paragraphs[j].hyphenation = false;
				current_paragraphs[j].justification = Justification.CENTER;
			}
		}
	}  

}
Known Participant
December 2, 2020

This is great, is there anyway to change the position of the text and add a background behind it?

Participating Frequently
December 2, 2020

If you need do something more with script, I added some more parametrization for it 😛 

You welcome!

 

// This script generates artboard names captions below each artboard.
// it take artboard name and generates TextArea under artboard aligned
// center in white color / font size 12pt.

// ------------------------------------------------------------------ Some i params

var labelsWidth = 100; // width in % of the parent artboard width
                       // if there will be left/right margins added,
                       // this will be adjusted to the width left after adding margins

var labelFontSize = 12;
var labelHeight = labelFontSize * 2 + labelFontSize/3; // fixed height of the label

var labelsMargin = {
    top: 5,    // top label margin in pt (user when labels are below artboards)
    left: 0,   // left label margin in pt
    right: 0,  // right label margin in pt
    bottom: 5  // top label margin in pt (user when labels are above artboards)
};

var labelsBelowArtboard = false; // if false labels will be above artboards

var labelTextColor = '#FFFFFF';
var labelBackgroundColor = null; // null for transparent
var textAlignment = Justification.CENTER; // possibilities LEFT, RIGHT, CENTER, FULLJUSTIFY...and more

// --------------------------------------------------------------------

function hexToRgb(hex) {
    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex.toLowerCase());
    return result ? {
        r: parseInt(result[1], 16),
        g: parseInt(result[2], 16),
        b: parseInt(result[3], 16)
    } : null;
}

function doJob() {
    var myDocument = app.activeDocument;
    for (var i = 0; i < myDocument.artboards.length; i++) {

        var artboard = app.activeDocument.artboards[i];
        var leftA = artboard.artboardRect[0];
        var topA = artboard.artboardRect[1];
        var rightA = artboard.artboardRect[2];
        var bottomA = artboard.artboardRect[3];
        var widthA = Math.abs(rightA - leftA);

        // consider margins

        // For each artboard, add a new text area of sizes and position below
        var l = leftA + labelsMargin.left;
        var w = (widthA - labelsMargin.right - labelsMargin.left) * labelsWidth / 100.0; // use percentage width of artboard
        var h = labelHeight;

        // center label between margins - make it look nicer
        l += (widthA - labelsMargin.right - labelsMargin.left - w) / 2;

        if (labelsBelowArtboard) {
            var t = bottomA - labelsMargin.top;
        } else {
            var t = topA + labelsMargin.bottom + h;
        }

        // background color
        if (labelBackgroundColor != null) {
            var bkgRectRef = myDocument.pathItems.rectangle(t, l, w, h); // rectangle of background
            var backgroundColor = new RGBColor();
            var rgbColor = hexToRgb(labelBackgroundColor);
            backgroundColor.red = rgbColor.r;
            backgroundColor.green = rgbColor.g;
            backgroundColor.blue = rgbColor.b;
            bkgRectRef.filled = true;
            bkgRectRef.fillColor = backgroundColor;
        }

        var rectRef = myDocument.pathItems.rectangle(t, l, w, h); // rectangle which will become text area
        var myLabel = myDocument.textFrames.areaText(rectRef);

        // text color
        var textColor = new RGBColor();
        var rgbColorOfText = hexToRgb(labelTextColor);
        textColor.red = rgbColorOfText.r;
        textColor.green = rgbColorOfText.g;
        textColor.blue = rgbColorOfText.b;

        // fill it with the artboard name
        myLabel.contents = artboard.name;

        // this line controls the font size
        myLabel.textRange.characterAttributes.size = labelFontSize;

        if (myLabel.textRange.length > 0) {
            var current_paragraphs = myLabel.textRange.paragraphs;
            for (j = 0; j < current_paragraphs.length; j++) {
                if (current_paragraphs[j].characters.length > 0) {
                    current_paragraphs[j].fillColor = textColor;
                    current_paragraphs[j].hyphenation = false;
                    current_paragraphs[j].justification = textAlignment;
                }
            }
        }

    }
}

// RUN IT !
doJob();
Inspiring
December 2, 2013

Hi Petros

Yes, JavaScript can read the name of each artboard.

Try to develop your script and post your code here. So we can help you to correct the necessary problems.

A tip. I think the best way to develop such script is:

1. Make a loop that cycle between all the artboards.

2. In the loop, take the artboard name.

3. Create a new textFrame item and apply the artboard name as the content of this new object. also, define formatting properties for the text

4. Position the textFrame were you want in the artboard.

This would get what you need.

Best Regards

Gustavo.

Petros_Author
Known Participant
December 2, 2013

It would be rad if Illustrator had a user-friendly scripting gui like Cinema4D has in the form of xpresso. Xpresso is rad. You can literally drop references to objects into the xpresso window, then find and link the attributes. For instance, if I were doing this same type of thing in Cinema4D I would simply drop the artboard and the text field into xpresso, create an outgoing port in the artboard for artboard name, and then drag and drop that onto an incoming port on the text field for text entry. Illustrator could be this cool. Suppose I could feature request it, but what are the chances...

Inspiring
December 3, 2013

Petros

The scripting guides for Adobe Illustrator (http://www.adobe.com/devnet/illustrator/scripting.html) as well as other Adobe application's scripting guides are very clear on explaining properties and methods we can use with JavaSscript, Apple Script and VB Script.

The scripting supposes you already know the programming language, so it's very useful and easy to understand the explanations and descriptions.

Best Regards

Gustavo.