Skip to main content
j.khakase
Inspiring
October 9, 2023
Answered

copy selected layer names (sub layers), by Script ?

  • October 9, 2023
  • 2 replies
  • 608 views

Helllo Friends,

is there a way to copy selected layer names (sub layers), by Script ?

 

This topic has been closed for replies.
Correct answer Sergey Osokin
//@target illustrator
app.preferences.setBooleanPreference('ShowExternalJSXWarning', false); // Fix drag and drop a .jsx file

// Main function
function main() {
  if (!documents.length) return;
  if (!selection.length || selection.typename === 'TextRange') return;

  var doc = app.activeDocument;
  var sel = app.selection;
  var names = [];

  for (var i = 0; i < sel.length; i++) {
    var str = getName(sel[i]);
    if (!isEmpty(str)) names.push(getName(sel[i]));
  }

  if (names.length) setClipboard(names.join('\n'));
}

// Get item name of different types
function getName(item) {
  var str = '';
  if (item.typename === 'TextFrame' && isEmpty(item.name) && !isEmpty(item.contents)) {
    str = item.contents;
  } else if (item.typename === 'SymbolItem' && isEmpty(item.name)) {
    str = item.symbol.name;
  } else {
    str = item.name;
  }
  return str;
}

// Check empty string
function isEmpty(str) {
  return str.replace(/\s/g, '').length == 0;
}

// Copy dummy TextFrame content to clipboard
// Function by Tom Scharstein https://github.com/Inventsable
function setClipboard(str) {
  var prev = app.selection;
  selection = null;
  var idoc = app.activeDocument;
  var tlayer = idoc.layers.add();
  var tframe = tlayer.textFrames.add();
  tframe.contents = str.replace(/^\s*||\s*$/, "");
  tframe.translate(10);
  tframe.selected = true;
  app.copy();
  tlayer.remove();
  app.selection = prev;
}

// Run script
try {
  main();
} catch (e) {}

2 replies

Sergey Osokin
Inspiring
October 13, 2023

I also have an update to BatchRenamer v.1.4 where you can export object names to TXT instead of copying them to the clipboard.

j.khakase
j.khakaseAuthor
Inspiring
October 17, 2023

Thank you, this will be great help

Sergey Osokin
Sergey OsokinCorrect answer
Inspiring
October 10, 2023
//@target illustrator
app.preferences.setBooleanPreference('ShowExternalJSXWarning', false); // Fix drag and drop a .jsx file

// Main function
function main() {
  if (!documents.length) return;
  if (!selection.length || selection.typename === 'TextRange') return;

  var doc = app.activeDocument;
  var sel = app.selection;
  var names = [];

  for (var i = 0; i < sel.length; i++) {
    var str = getName(sel[i]);
    if (!isEmpty(str)) names.push(getName(sel[i]));
  }

  if (names.length) setClipboard(names.join('\n'));
}

// Get item name of different types
function getName(item) {
  var str = '';
  if (item.typename === 'TextFrame' && isEmpty(item.name) && !isEmpty(item.contents)) {
    str = item.contents;
  } else if (item.typename === 'SymbolItem' && isEmpty(item.name)) {
    str = item.symbol.name;
  } else {
    str = item.name;
  }
  return str;
}

// Check empty string
function isEmpty(str) {
  return str.replace(/\s/g, '').length == 0;
}

// Copy dummy TextFrame content to clipboard
// Function by Tom Scharstein https://github.com/Inventsable
function setClipboard(str) {
  var prev = app.selection;
  selection = null;
  var idoc = app.activeDocument;
  var tlayer = idoc.layers.add();
  var tframe = tlayer.textFrames.add();
  tframe.contents = str.replace(/^\s*||\s*$/, "");
  tframe.translate(10);
  tframe.selected = true;
  app.copy();
  tlayer.remove();
  app.selection = prev;
}

// Run script
try {
  main();
} catch (e) {}

j.khakase
j.khakaseAuthor
Inspiring
October 10, 2023

Yes, Perfectly work as per need, Thank you so much @Sergey Osokin Amezing!