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
Adobe 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
Adobe 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;
        }
      }
    }
  }
}

 

enisio1
enisio1Author
Known Participant
December 28, 2022

The script is working properly. No problem. But I will change any word to another word, maybe not a number. Then that doesn't work either. I'm not that expert in java script but if I had talent like you I would do it in general. So I would do both numbers and words. I wouldn't separate the number and the word.

pixxxelschubser
Adobe 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
Brainiac
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.