Last active
November 8, 2024 14:04
-
-
Save creold/c5c36e9e6daf49082437aea5059e0462 to your computer and use it in GitHub Desktop.
Rename each group from text frame contents. Adobe Illustrator script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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: [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 | |
// 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) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More about script in Telegram channel.