Copy link to clipboard
Copied
Hello Everyone,
I am struggling to develope the script for find and replacing text in the ai file.
I need to replace the text "2021" to "2022" in 100's of artworks. If anyone help me to figure out the script, it would be helpful.
Kindly help in this concern.
You can try this one:
//@target illustrator
function test (){
var SEARCH_TEXT = "2021"; // TODO: Add other code to escape regular expressions for search text which has special characters other than digits/numbers.
var REPLACE_TEXT = "2022";
var SEARCH_RX = new RegExp(SEARCH_TEXT, "g");
var folderWithFiles = Folder.selectDialog("Choose a folder with files");
if (!folderWithFiles) { return; }
var allFiles = folderWithFiles.getFiles();
var thisFile, foundAiFiles = [];
for (var i
...
This should help to do just the active one.
var SEARCH_TEXT = "2021";
var REPLACE_TEXT = "2022";
var SEARCH_RX = new RegExp(SEARCH_TEXT, "g");
var thisDoc = app.activeDocument, thisText;
var currentFileWasReplaced = false;
for (var j = 0; j < thisDoc.textFrames.length; j++) {
thisText = thisDoc.textFrames[j];
if (SEARCH_RX.test(thisText.contents)) {
thisText.contents = thisText.contents.replace(SEARCH_RX, REPLACE_TEXT);
currentFileWasReplaced = true;
}
}
if (currentFileWasReplaced) {
thi
...
Copy link to clipboard
Copied
You can try this one:
//@target illustrator
function test (){
var SEARCH_TEXT = "2021"; // TODO: Add other code to escape regular expressions for search text which has special characters other than digits/numbers.
var REPLACE_TEXT = "2022";
var SEARCH_RX = new RegExp(SEARCH_TEXT, "g");
var folderWithFiles = Folder.selectDialog("Choose a folder with files");
if (!folderWithFiles) { return; }
var allFiles = folderWithFiles.getFiles();
var thisFile, foundAiFiles = [];
for (var i = 0; i < allFiles.length; i++) {
thisFile = allFiles[i];
if (thisFile.name.substr(-3) == ".ai") {
foundAiFiles.push(thisFile);
}
}
var fileReplacementLog = [], textReplacementLog = [];
var thisDoc, thisText, currentFileWasReplaced;
for (var i = 0; i < foundAiFiles.length; i++) {
currentFileWasReplaced = false;
thisFile = foundAiFiles[i];
thisDoc = app.open(thisFile);
textReplacementLog = [];
for (var j = 0; j < thisDoc.textFrames.length; j++) {
thisText = thisDoc.textFrames[j];
if (SEARCH_RX.test(thisText.contents)) {
thisText.contents = thisText.contents.replace(SEARCH_RX, REPLACE_TEXT);
textReplacementLog.push(thisText.uuid);
currentFileWasReplaced = true;
}
}
if (currentFileWasReplaced) {
fileReplacementLog.push(decodeURI(thisFile.name) + " : " + textReplacementLog.join(", "));
thisDoc.save(); // If this doesn't work to save, do a saveAs to a specified location.
thisDoc.close(); // Should be already saved, but if not, use SaveOptions.SAVECHANGES
} else {
thisDoc.close(SaveOptions.DONOTSAVECHANGES);
}
}
if (confirm("Done with " + foundAiFiles.length + " files. Write the process summary?")) {
var summaryLocation = Folder("~/Desktop/TextReplacement");
if (!summaryLocation.exists) {
summaryLocation.create();
}
var summaryFile = File(decodeURI(summaryLocation) + "/summary-" + new Date().getTime() + ".txt");
summaryFile.open("w");
summaryFile.write(fileReplacementLog.join("\n"));
summaryFile.close();
summaryFile.execute();
}
}
test();
Copy link to clipboard
Copied
Hi Silly V,
Thank you so much for the script, would you also help to figure out the script for find and replace the 2021 to 2022 in the active document.
Copy link to clipboard
Copied
This should help to do just the active one.
var SEARCH_TEXT = "2021";
var REPLACE_TEXT = "2022";
var SEARCH_RX = new RegExp(SEARCH_TEXT, "g");
var thisDoc = app.activeDocument, thisText;
var currentFileWasReplaced = false;
for (var j = 0; j < thisDoc.textFrames.length; j++) {
thisText = thisDoc.textFrames[j];
if (SEARCH_RX.test(thisText.contents)) {
thisText.contents = thisText.contents.replace(SEARCH_RX, REPLACE_TEXT);
currentFileWasReplaced = true;
}
}
if (currentFileWasReplaced) {
thisDoc.save(); // If this doesn't work to save, do a saveAs to a specified location.
thisDoc.close(); // Should be already saved, but if not, use SaveOptions.SAVECHANGES
} else {
alert("No replacement made.");
}