Skip to content

Instantly share code, notes, and snippets.

@creold
Last active March 12, 2024 13:24
Show Gist options
  • Save creold/7b07c3698110a331c57f80f0fc40e57c to your computer and use it in GitHub Desktop.
Save creold/7b07c3698110a331c57f80f0fc40e57c to your computer and use it in GitHub Desktop.
Batch search for a TextFrame of a given size and replace its contents with the file name
/*
Batch search for a TextFrame of a given size and replace its contents with the file name
Discussion: https://community.adobe.com/t5/illustrator-discussions/script-that-finds-specific-text-and-change-base-from-filename/m-p/13365933#M344679
Author: Sergey Osokin, email: hi@sergosokin.ru
Check 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() {
var size = 4; // pt
var dir = Folder.selectDialog('Select the source folder...');
if (dir !== null) {
var files = getAllFiles(decodeURI(dir), '.ai');
for (var i = 0, len = files.length; i < len; i++) {
changeText(files[i], size);
}
}
}
function getAllFiles(dir, ext) {
var fList = Folder(dir).getFiles(),
files = [];
for (var i = 0, len = fList.length; i < len; i++) {
if (fList[i] instanceof Folder) {
files = files.concat(getAllFiles(fList[i], ext));
} else if (fList[i] instanceof File) {
if (fList[i].name.indexOf(ext) > -1) {
files.push(fList[i]);
}
}
}
return files;
}
function changeText(f, size) {
var doc = app.open(f);
var docName = doc.name.replace(/\.[^\.]+$/, '');
var tf = doc.textFrames;
for (var i = 0, len = tf.length; i < len; i++) {
if (tf[i].textRange.characterAttributes.size == size) {
tf[i].contents = docName;
}
}
doc.save();
doc.close();
}
// Run script
try {
main();
} catch (e) {}
@creold
Copy link
Author

creold commented Nov 23, 2022

change-from-filename

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