Skip to content

Instantly share code, notes, and snippets.

@creold
Last active March 12, 2024 13:25
Show Gist options
  • Save creold/c5c36e9e6daf49082437aea5059e0462 to your computer and use it in GitHub Desktop.
Save creold/c5c36e9e6daf49082437aea5059e0462 to your computer and use it in GitHub Desktop.
Rename each group from text frame contents. Adobe Illustrator script
/*
The script changes each group name in the active layer/selection to its first TextFrame content
Discussion: https://community.adobe.com/t5/illustrator-discussions/change-layer-name-by-name-text-in-group/td-p/13224168
Author: Sergey Osokin, email: hi@sergosokin.ru
Check my other scripts: https://github.com/creold
Donate (optional):
If you find this script helpful, you can buy me a coffee
- via Buymeacoffee: https://www.buymeacoffee.com/aiscripts
- via Donatty https://donatty.com/sergosokin
- via DonatePay https://new.donatepay.ru/en/@osokin
- via YooMoney https://yoomoney.ru/to/410011149615582
*/
//@target illustrator
app.preferences.setBooleanPreference('ShowExternalJSXWarning', false); // Fix drag and drop a .jsx file
// Main function
function main() {
if (!/illustrator/i.test(app.name)) return;
if (!documents.length) return;
var doc = app.activeDocument;
var lay = doc.activeLayer;
var arr = selection;
var prefix = "";
var suffix = "";
if (!arr.length || arr.typename === 'TextRange') {
var endMsg = 'or select groups manually';
if (!lay.visible) {
alert('Enable ' + lay.name + ' visibility ' + endMsg, 'Error');
return;
} else if (lay.locked) {
alert('Unlock ' + lay.name + ' ' + endMsg, 'Error');
return;
}
arr = lay.groupItems;
}
renameGroups(arr, prefix, suffix);
// Force name update
if (parseInt(app.version) <= 23) {
try {
var tmp = doc.pathItems.add();
tmp.remove();
} catch (err) {}
}
}
// Get first TextFrame in GroupItem
function getFirstTF(grp) {
var tf;
if (grp.textFrames.length) {
for (var i = 0; i < grp.textFrames.length; i++) {
if (!isEmpty(grp.textFrames[i].contents)) {
return grp.textFrames[i];
}
}
}
if (grp.groupItems.length) {
for (var i = 0; i < grp.pageItems.length; i++) {
if (grp.pageItems[i].typename === 'GroupItem') {
tf = getFirstTF(grp.pageItems[i]);
}
}
}
return tf;
}
// Check empty string
function isEmpty(str) {
return str.replace(/\s/g, '').length == 0;
}
// Rename groups to their first TextFrame content
function renameGroups(arr, prefix, suffix) {
for (var i = 0, len = arr.length; i < len; i++) {
var item = arr[i];
if (item.typename === 'GroupItem') {
var tf = getFirstTF(item);
var str = '';
if (tf == undefined) continue;
str = tf.contents.replace(/(\r\n|\n|\r|\u0003)/gm, ' ').substr(0, 240);
item.name = prefix + str + suffix;
}
}
}
// Run script
try {
main();
} catch (err) {}
@creold
Copy link
Author

creold commented Mar 17, 2023

  • Renames the selected groups or all groups of the active layer.
  • Replaces line breaks with spaces.
  • Limits the name to 240 characters if there is a lot of content in the text frame.
    GroupNameByTopText

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment