Skip to main content
Known Participant
October 8, 2020
Answered

Auto Rename Artboards Based On Placed Image Filename

  • October 8, 2020
  • 3 replies
  • 4133 views

Is there a way wether it be through a script or third party plugin to auto rename a series of artboards within a file based on the placed image within each artboard?

 

Scenario: If I am doing t-shirt mockups and have 3 artboards each with a different shirt images and want to export jpgs of each artboard and have them named based off of the filenames of each shirt image. Would be very convenient to have the artboards renamed automatically based on the placed shirt image filename vs manually changing the name of each artboard. 


Thanks!

 

 

This topic has been closed for replies.
Correct answer femkeblanco

You beat me to it, Silly-V.  This is what I cam up with:

 

var doc = app.activeDocument;
for (var i = 0; i < doc.artboards.length; i++) {
  doc.artboards.setActiveArtboardIndex(i);
  doc.selectObjectsOnActiveArtboard();
  for (var j = 0; j < selection.length; j++) {
    if (selection[j].typename == "PlacedItem") {
      doc.artboards[i].name = selection[j].name;
    }
  }
}

 

 


Copy and paste it in a jsx file. (You can create a txt file and change the extension to jsx.)

 

Then, while your document is open in Illustrator, go to File > Scripts > Other Script (Ctrl+F12). Find your script and open it.

3 replies

WA_ARTAuthor
Known Participant
February 9, 2023

This is the script that has that has worked great for me in regard to naming artboards based on unembedded images within them:

#target illustrator
function test(){
var doc = app.activeDocument;
var boards = doc.artboards;
var thisBoard, artItemOnBoard, placedItemName;
for (var i = 0; i < boards.length; i++) {
thisBoard = boards[i];
doc.artboards.setActiveArtboardIndex(i);
doc.selection = null;
doc.selectObjectsOnActiveArtboard();
for (var j = 0; j < doc.selection.length; j++) {
artItemOnBoard = doc.selection[j];
if (artItemOnBoard.typename == "PlacedItem") {
placedItemName = decodeURI(artItemOnBoard.file.name).replace(/\.[^\.]+$/, "");
thisBoard.name = placedItemName;
}
}
}
}
test();

 

I do occasionally get the error:

Error 9062: There is no file associated with this item

Line: 14

->            placedItemName =

decodeURI(artItemOnBoard.file.name).replace(/\.[^\.]+$/,"");

 

If I quit illustrator and reopen I get the same error.

 

If I restart the mac and open the same file with the unembedded images it works great. Any idea how to fix this without restarting?

I still can't get it to work with embedded images but that is minor.

Known Participant
August 5, 2021

Hello,

 

Again, Thank you for this wonderful script you made for me.

 

If possible, I want to make a minor change, I really don't know how to code.

 

The pointText start from the TOP LEFT corner, I just want to make it start at the BOTTOM LEFT corner, that's all.

 

This is the current script.

var doc = app.activeDocument;
var name1 = app.activeDocument.name;
for (var i = 0; i < doc.artboards.length; i++) {
  doc.artboards.setActiveArtboardIndex(i);
  doc.selectObjectsOnActiveArtboard();
  for (var j = 0; j < selection.length; j++) {
    if (selection[j].typename == "PlacedItem") {
      var name2 = decodeURI(selection[j].file.name).replace(/\.[^\.]+$/, "");
      doc.artboards[i].name = name2;
      var d = doc.artboards[i].artboardRect;
      var text1 = doc.textFrames.pointText([d[0] + 20, d[1] - 20]);
      text1.contents = name1 + " - " + name2;
      text1.textRange.characterAttributes.size = 8;
      text1.paragraphs[0].paragraphAttributes.justification = Justification.LEFT;
      break;
    }
  }
}

 

Please see image for reference:

 

Thank you very much!

 

femkeblanco
Legend
August 5, 2021
var doc = app.activeDocument;
var name1 = app.activeDocument.name;
for (var i = 0; i < doc.artboards.length; i++) {
  doc.artboards.setActiveArtboardIndex(i);
  doc.selectObjectsOnActiveArtboard();
  for (var j = 0; j < selection.length; j++) {
    if (selection[j].typename == "PlacedItem") {
      var name2 = decodeURI(selection[j].file.name).replace(/\.[^\.]+$/, "");
      doc.artboards[i].name = name2;
      var d = doc.artboards[i].artboardRect;
      var text1 = doc.textFrames.pointText([d[0] + 20, d[3] + 20]);
      text1.contents = name1 + " - " + name2;
      text1.textRange.characterAttributes.size = 8;
      text1.paragraphs[0].paragraphAttributes.justification = Justification.LEFT;
      break;
    }
  }
}
Silly-V
Legend
October 8, 2020

This should do the trick:

 

#target illustrator
function test(){
	var doc = app.activeDocument;
	var boards = doc.artboards;
	var thisBoard, artItemOnBoard, placedItemName;
	for (var i = 0; i < boards.length; i++) {
		thisBoard = boards[i];
		doc.artboards.setActiveArtboardIndex(i);
		doc.selection = null;
		doc.selectObjectsOnActiveArtboard();
		for (var j = 0; j < doc.selection.length; j++) {
			artItemOnBoard = doc.selection[j];
			if (artItemOnBoard.typename == "PlacedItem") {
				placedItemName = decodeURI(artItemOnBoard.file.name).replace(/\.[^\.]+$/, "");
				thisBoard.name = placedItemName;
			}
		}
	}
}
test();
WA_ARTAuthor
Known Participant
October 8, 2020

Sorry, what is the best way to add this in? I have used scripts before but previously just downloaded them. Thanks for your help!

 

femkeblanco
Legend
October 8, 2020

You beat me to it, Silly-V.  This is what I cam up with:

 

var doc = app.activeDocument;
for (var i = 0; i < doc.artboards.length; i++) {
  doc.artboards.setActiveArtboardIndex(i);
  doc.selectObjectsOnActiveArtboard();
  for (var j = 0; j < selection.length; j++) {
    if (selection[j].typename == "PlacedItem") {
      doc.artboards[i].name = selection[j].name;
    }
  }
}