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

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

Explorer ,
Dec 26, 2022 Dec 26, 2022

Copy link to clipboard

Copied

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 ]

TOPICS
Bug , Scripting

Views

503

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
Adobe
Guide ,
Dec 26, 2022 Dec 26, 2022

Copy link to clipboard

Copied

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

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
Community Expert ,
Dec 26, 2022 Dec 26, 2022

Copy link to clipboard

Copied

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

 

 

 

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
Engaged ,
Dec 26, 2022 Dec 26, 2022

Copy link to clipboard

Copied

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

words.gif

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
Engaged ,
Dec 27, 2022 Dec 27, 2022

Copy link to clipboard

Copied

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

 

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 ,
Dec 28, 2022 Dec 28, 2022

Copy link to clipboard

Copied

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.

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 ,
Dec 28, 2022 Dec 28, 2022

Copy link to clipboard

Copied

new2021.gif

 I also made a video. The code is still missing.

Same error in your video.

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
Engaged ,
Dec 28, 2022 Dec 28, 2022

Copy link to clipboard

Copied

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.

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
Engaged ,
Dec 28, 2022 Dec 28, 2022

Copy link to clipboard

Copied

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

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
Community Expert ,
Dec 28, 2022 Dec 28, 2022

Copy link to clipboard

Copied

Or he tries the code I already linked above. The execution should be a little faster because of the other search variant. But please don't forget - this is also only a script snippet and currently only works with one text frame.

 

quote
…  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) …

 

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
Engaged ,
Dec 28, 2022 Dec 28, 2022

Copy link to clipboard

Copied

Ah, I completely missed that link when reading your post. I had a few free minutes the other day so this looked like a fun challenge. Yesterday I was working on something very similar with `exec` since RegEx is so much faster but ran out of time... Glad you already figured it out. I just assumed the OP had searched the forums before asking.

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
Community Expert ,
Dec 28, 2022 Dec 28, 2022

Copy link to clipboard

Copied

LATEST
quote

… so this looked like a fun challenge …


By @jduncan

 

@jduncan 

I can assure you that this form of text editing via script in Illustrator is exactly the opposite.
😉

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
Engaged ,
Dec 28, 2022 Dec 28, 2022

Copy link to clipboard

Copied

If you want it to work for words, just remove the first if statement. I only had that statement in the script to attempt to speed things up a bit. I have pasted a revised script below that will work for words and numbers.

 

Screen Shot 2022-12-28 at 14.17.22.gif

 

PLEASE NOTE, this only works for finding singular words (eg. INCORRECT) not strings of multiple words. Like in the example above, you can replace the found 'word' with a string of multiple words though (eg. CORRECT WORD).

 

var doc = app.activeDocument;
replaceWord("INCORRECT", "CORRECT WORD");

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;
    for (var j = 0; j < words.length; j++) {
      word = words[j];
      if (word.contents == strFind) {
        word.contents = strReplace;
      }
    }
  }
}

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 ,
Dec 28, 2022 Dec 28, 2022

Copy link to clipboard

Copied

AI FindAndReplace+WordReplaceComparison.gif

 AI FindAndReplace+WordReplaceComparison. 

I may be pushing too hard, but this caught my attention. Cannot find concatenated words.

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
Community Expert ,
Dec 28, 2022 Dec 28, 2022

Copy link to clipboard

Copied

@enisio1 

In the meantime, have you tried my code snippet?

 

A small request:
Please always highlight your text frames and also show the hidden characters and the layer panel in your examples. This is the only way to see if you are dealing with different single point texts or different single area texts or a single point text or area text. This must be taken into account with some script variants. (A sample file for testing is also usually very useful - then you and we start from the same basics).

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
Engaged ,
Dec 28, 2022 Dec 28, 2022

Copy link to clipboard

Copied

@pixxxelschubser submitted the correct code in his original comment at the top of this post. I was trying a weird round-a-bout way that only addressed the original 2021 to 2022 conversion. His RegEx approach is the better option and should cover most of your edge cases.

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