Copy link to clipboard
Copied
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.
This is a simple way if all characters of the string are 4 pt
(function () {
var doc = app.activeDocument;
var docName = doc.name.replace(/\.[^\.]+$/, '');
var tf = doc.textFrames;
var size = 4; // pt
for (var i = 0, len = tf.length; i < len; i++) {
if (tf[i].textRange.characterAttributes.size == size) {
tf[i].contents = docName;
}
}
})();
/*
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
...
Copy link to clipboard
Copied
Hi @Roel Verano, could you clarify?
1. find all 4pt text
2. do what to it? Set baseline?
3. Something to do with filename?
If you are having trouble describing, you could try posting a sample file showing before and after texts.
- Mark
Copy link to clipboard
Copied
First, find 4pt font size text
Second change the 4pt font size text into the file name base on the file name
The output should be like this.
Copy link to clipboard
Copied
This is a simple way if all characters of the string are 4 pt
(function () {
var doc = app.activeDocument;
var docName = doc.name.replace(/\.[^\.]+$/, '');
var tf = doc.textFrames;
var size = 4; // pt
for (var i = 0, len = tf.length; i < len; i++) {
if (tf[i].textRange.characterAttributes.size == size) {
tf[i].contents = docName;
}
}
})();
Copy link to clipboard
Copied
This is actually working somehow can u make it for a batch for example all of AI files under one folder?
Copy link to clipboard
Copied
/*
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) {}
Copy link to clipboard
Copied
This is amazing! Thanks a lot!
Copy link to clipboard
Copied
Great job, Sergey.👍