Skip to main content
enisio1
Known Participant
December 26, 2022
Question

[Branched] Problem with the formatting - with Javascripts for Find and Replace the text

  • December 26, 2022
  • 3 replies
  • 2372 views

All the codes I found for FIND REPLACE I always encountered the same problem as in the video. Just like the codes here. Watch the video and you'll know what I mean. Character styles, sizes, colors spoil everything. This script and all similar ones are very dangerous.

 

[ branched from Javascript for Find and Replace the text in the .ai file to Illustrator forum by moderator ]

[ title added by moderator ]

This topic has been closed for replies.

3 replies

jduncan
Community Expert
Community Expert
December 26, 2022

This solution is slow (could be optimized), tailored for this exact 2021 -> 2022 swap, and is not very elegant... But it shows what you can do with 5 minutes and the Illustrator API reference docs...

var doc = app.activeDocument;
var textFrames = doc.textFrames;
var frame, words, word, characters;
for (var i = 0; i < textFrames.length; i++) {
  frame = doc.textFrames[i].textRange;
  words = frame.words;
  for (var j = 0; j < words.length; j++) {
    word = words[j];
    characters = word.characters;
    if (word.contents == "2021") {
      word.characters[2].contents = 2;
      word.characters[3].contents = 2;
    }
  }
}

jduncan
Community Expert
Community Expert
December 27, 2022

I cleaned this up a bit... It is pretty fast on smaller blocks of text but can be slow on larger blocks of text since it iterates over every word.

 

Things to note:

  • only works on whole-word matches
  • can now replace the matched word with a word of a different length or even a string of words
  • won't work as expected on matched words where characters have different styles

 

var doc = app.activeDocument;
replaceWord("2021", "Twenty twenty-two");

function replaceWord(strFind, strReplace) {
  var frame, words, word;
  for (var i = 0; i < doc.textFrames.length; i++) {
    frame = doc.textFrames[i].textRange;
    words = frame.words;
    if (strFind in words) {
      for (var j = 0; j < words.length; j++) {
        word = words[j];
        if (word.contents == strFind) {
          word.contents = strReplace;
        }
      }
    }
  }
}

 

jduncan
Community Expert
Community Expert
December 28, 2022

Sorry, I missed the video when I first responded. I think the reason it's missing that last 2021 is because it is at the end of a paragraph. If you put a single character after that 2021, the script works just fine. I'm not sure of the cause but I'll look into it and let you know what I find out.


Ah, I see what the problem is. You have a blank space after that last 2021 that doesn't get changed. When you first highlight the text in your video, I can see the extra space getting highlighted along with the word. If you click on the "Type" menu and select "Show Hidden Characters" you'll see a little dot after "2021" indicating there is a space there before the paragraph break. For what ever reason, Ai determines that space to be part of the word...

 

Two ways to fix it are...

1. Remove the space from the text and the script will work as is

2. Edit the script to strip spaces from the words like below

 

var doc = app.activeDocument;
replaceWord("2021", "2022");

function replaceWord(strFind, strReplace) {
  var frame, words, word;
  var ct = 0;
  for (var i = 0; i < doc.textFrames.length; i++) {
    frame = doc.textFrames[i].textRange;
    words = frame.words;
    for (var j = 0; j < words.length; j++) {
      word = words[j];
      if (word.contents.replace(" ", "") == strFind) {
        ct += 1;
        word.contents = strReplace;
      }
    }
  }
  alert("Replacements Made\n" + ct);
}
pixxxelschubser
Community Expert
Community Expert
December 26, 2022

Sorry @enisio1 

You are right in your observation. Actually, also with your warning.

On the other hand, it is as superfluous as the warning not to use an indoor paint outdoors. True, a paint is a paint. But as you correctly pointed out, not every paint (script) can be used for everything.

 

@femkeblanco  is absolutely right. Scripting is complex and time-consuming, and the scripts that are freely available and posted on the forum for free are usually (only) tailored to the requirements of the particular theme. Therefore, it is always very important to test scripts that are not written specifically for you to see if they meet your requirements. Very often this can work - unfortunately not in this case.

 

Therefore, I would rather look for scripts or script snippets that not only search and replace, but also take formatting into account, e.g. here: Adobe Illustrator Forum  RegExp search and replace, keep original text formatting 

 

Maybe it works - but maybe this script doesn't work for you either (Adjustments in the regex will certainly be necessary).

 

Only you can know that. Because only you know the requirements you need. And if it doesn't work, then you need to keep looking, either for a suitable script, or for someone to modify an existing (free) script for you, or for someone to write a paid script for you to your own requirements.

 

However, you can also learn to script yourself and write your own scripts. You can always ask questions about scripting here in the forum - if possible, they will be happy to answer you.

 

Many greetings

 

 

 

femkeblanco
Legend
December 26, 2022

Not every eventuality will be anticipated in the casual script.  There is nothing in the OP's post about font, style or size.