Answered
Script that finds specific text and change base from filename
Is there a possible Batch Script that finds a specific text? for example,
Finds 4pt text in the file and changes the text base on the file name.
Thanks in advance.
Is there a possible Batch Script that finds a specific text? for example,
Finds 4pt text in the file and changes the text base on the file name.
Thanks in advance.
/*
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
*/
//@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) {}Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.