Skip to content

Instantly share code, notes, and snippets.

@creold
Last active November 8, 2024 14:08
Show Gist options
  • Save creold/5575fcf1d7b9c8d4196763958dfd9fc0 to your computer and use it in GitHub Desktop.
Save creold/5575fcf1d7b9c8d4196763958dfd9fc0 to your computer and use it in GitHub Desktop.
Renames layers using internal TextFrames. Adobe Illustrator script
/*
The script renames the layers using the content of the TextFrame or its custom name
Discussion: https://community.adobe.com/t5/illustrator-discussions/script-to-rename-layer-from-text-in-the-layer/td-p/13865554
Author: Sergey Osokin, email: [email protected]
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
function main() {
if (!/illustrator/i.test(app.name)) return;
if (!documents.length) return;
// UI
var win = new Window('dialog', 'Rename layer as text');
win.alignChildren = ['fill', 'center'];
var opts = win.add('group');
opts.alignChildren = ['fill', 'center'];
var isFirstRb = opts.add('radiobutton', undefined, 'Use first text');
isFirstRb.value = true;
var isLastRb = opts.add('radiobutton', undefined, 'Last text');
var btns = win.add('group');
var currBtn = btns.add('button', undefined, 'Active Layer', { name: 'ok' });
var allBtn = btns.add('button', undefined, 'All');
allBtn.onClick = function () {
var _layers = app.activeDocument.layers;
for (var i = 0, len = _layers.length; i < len; i++) {
renameLayer(_layers[i], isFirstRb.value);
}
win.close();
}
currBtn.onClick = function () {
renameLayer(app.activeDocument.activeLayer, isFirstRb.value);
win.close();
}
win.center();
win.show();
}
function renameLayer(lay, isFirst) {
var tfs = getTextFrames(lay.pageItems);
if (!tfs.length) return;
var tf = isFirst? tfs[0] : tfs[tfs.length - 1];
if (tf.name != '') lay.name = tf.name;
else lay.name = tf.contents.slice(0, 100);
}
function getTextFrames(coll) {
var tfs = [];
for (var i = 0, len = coll.length; i < len; i++) {
if (/text/i.test(coll[i].typename)) {
tfs.push(coll[i]);
} else if (/group/i.test(coll[i].typename)) {
tfs = tfs.concat( getTextFrames(coll[i].pageItems) );
}
}
return tfs;
}
// Run script
try {
main();
} catch (err) {}
@creold
Copy link
Author

creold commented Jun 15, 2023

More about script in Telegram channel.

RenameLayerAsText

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