• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Javascript for Find and Replace the text in the .ai file

Explorer ,
Jan 23, 2022 Jan 23, 2022

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.

TOPICS
Scripting

Views

849

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Valorous Hero , Jan 23, 2022 Jan 23, 2022

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 
...

Votes

Translate

Translate
Valorous Hero , Jan 24, 2022 Jan 24, 2022

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
...

Votes

Translate

Translate
Adobe
Valorous Hero ,
Jan 23, 2022 Jan 23, 2022

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();

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 23, 2022 Jan 23, 2022

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jan 24, 2022 Jan 24, 2022

Copy link to clipboard

Copied

LATEST

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.");
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines