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

Multiline Text Format Changing

Engaged ,
Aug 23, 2016 Aug 23, 2016

My document contains 2 places that I want to change a particular number.

Capture.JPG

Here is the code that I currently have to change the number I want.

#target illustrator

var doc = app.activeDocument;

var allText = doc.textFrames;

for (var i = 0; i < allText.length; i++) {

  if (allText.contents.substring(0,11) == "UENR3640-05"){

  var mediaNumber = allText.contents.replace("UENR3640-05", "UENR3640-06");

  allText.contents = mediaNumber;

  }

  }

The yellow box is a multi-line that contains 3 different font size. So when I run my above script it changes the number in the gray box fine. However the yellow box changes to this....

Capture2.JPG

Any help that would ignore the formatting of the text and just change the text I have setup? I would love to also have the option for this to run on ALL open documents.

This is on a Windows 7 64-bit PC with Adobe Illustrator CS4

Thanks in advance!

TOPICS
Scripting
801
Translate
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 1 Correct answer

Valorous Hero , Aug 23, 2016 Aug 23, 2016

Make your code above into a function which accepts a document object as an argument and make that inside of a loop which goes through all the documents.

#target illustrator

function test(){

  function processDoc(doc){

    var s = /UENR3640-05/gi;  

    var replacer = "UENR3640-06", result;  

    var allText = doc.textFrames;

    for (var i = 0; i < allText.length; i++) {

    while (result = s.exec(allText.contents)) {  

          try {  

                aCon = allText.characters[result.index];  

   

...
Translate
Adobe
Valorous Hero ,
Aug 23, 2016 Aug 23, 2016

Hello!

We just did this a few days ago, you will probably be able to use the answer here for your exact goals!

RegExp search and replace, keep original text formatting

Translate
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 ,
Aug 23, 2016 Aug 23, 2016

I saw that yesterday. The problem I have is that if I run the code....

#target illustrator

var s = /UENR3640-05/gi; 

var replacer = "UENR3640-06", result; 

var atf = activeDocument.textFrames[0]; 

 

while (result = s.exec(atf.contents)) { 

    try { 

        aCon = atf.characters[result.index]; 

        aCon.length = result[0].length; 

        aCon.contents = replacer; 

        } catch (e) {}; 

    }

It will change the text in the gray box....but not in the yellow box. So it isn't changing my formatting....but it isn't changing my text either

Translate
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 ,
Aug 23, 2016 Aug 23, 2016

Ok so update....I realized that I wasn't looping through all of the text in the documents. So how can I make this updated code loop through and find ALL of the matches....on all open documents??

#target illustrator

var doc = app.activeDocument;

var s = /UENR3640-05/gi; 

var replacer = "UENR3640-06", result; 

var allText = doc.textFrames;

for (var i = 0; i < allText.length; i++) {

while (result = s.exec(allText.contents)) { 

    try { 

        aCon = allText.characters[result.index]; 

        aCon.length = result[0].length; 

        aCon.contents = replacer; 

        } catch (e) {}; 

    }

}

Translate
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 ,
Aug 23, 2016 Aug 23, 2016

Make your code above into a function which accepts a document object as an argument and make that inside of a loop which goes through all the documents.

#target illustrator

function test(){

  function processDoc(doc){

    var s = /UENR3640-05/gi;  

    var replacer = "UENR3640-06", result;  

    var allText = doc.textFrames;

    for (var i = 0; i < allText.length; i++) {

    while (result = s.exec(allText.contents)) {  

          try {  

                aCon = allText.characters[result.index];  

                aCon.length = result[0].length;  

                aCon.contents = replacer;  

            } catch (e) {};  

      }

    }

  };

  for(var i=0; i<app.documents.length; i++){

      app.documents.activate();

      processDoc(app.activeDocument);

  };

};

test();

Translate
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 ,
Aug 24, 2016 Aug 24, 2016

That works great! I still have to have it run the function multiple times to catch all the instances...but it runs fast! Thanks again Silly-V

Translate
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 ,
Aug 24, 2016 Aug 24, 2016
LATEST

I am sure you can change it so that you don't have to run it multiple times, by adding another argument, an array of search strings, and another loop to go through them all. Instead of making your regexp with slashes, you can use the new RegExp() constructor and put your string & flags in there.

Translate
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