Skip to main content
kylamrtn21
Participating Frequently
January 20, 2017
Answered

Trouble applying paragraph and character styles to text in Illustrator with a script

  • January 20, 2017
  • 1 reply
  • 1523 views

0down votefavorite

I am trying to figure out how to apply paragraph and character styles to text in Illustrator with a script. I have pieced together this and it works sort-of. But I run into the problem that if searchWord1 = "CAUTION:"; with the colon it doesn't work. The same goes for searchWord1 = "¡CUIDADO!:"; I'm assuming it is because of the punctuation? How do I fix this?

var doc = app.activeDocument; var myFrame = doc.textFrames.getByName("back-info"); var myStyle = doc.paragraphStyles.getByName("bullets"); myStyle.applyTo(myFrame.textRange);

if ( app.documents.length > 0 && app.activeDocument.textFrames.length > 0 ) {

searchWord1 = "CAUTION";

for ( i = 0; i < app.activeDocument.textFrames.length; i++ ) { textArt = activeDocument.textFrames; for ( j = 0; j < textArt.words.length; j++) { word = textArt.words; if ( word.contents == searchWord1) { var boldstyle = doc.characterStyles["bold"]; boldstyle.applyTo(word);
} } } }

This topic has been closed for replies.
Correct answer Silly-V

Here, from the link I posted in previous thread, an altered version of pixxxel's script:

#target illustrator

function test(){

// regex_changeContentsOfWordOrString_RemainFormatting.jsx 

// regards pixxxel schubser 

var doc = app.activeDocument;

var s = new RegExp("CAUTION:", "gi"); 

var myStyle = doc.characterStyles.getByName("MyStyle");

var atf = activeDocument.textFrames[0]; 

 

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

    try { 

        aCon = atf.characters[result.index]; 

        aCon.length = result[0].length; 

        myStyle.applyTo(aCon);

        } catch (e) {}; 

    } 

};

test();

1 reply

Silly-V
Silly-VCorrect answer
Legend
January 20, 2017

Here, from the link I posted in previous thread, an altered version of pixxxel's script:

#target illustrator

function test(){

// regex_changeContentsOfWordOrString_RemainFormatting.jsx 

// regards pixxxel schubser 

var doc = app.activeDocument;

var s = new RegExp("CAUTION:", "gi"); 

var myStyle = doc.characterStyles.getByName("MyStyle");

var atf = activeDocument.textFrames[0]; 

 

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

    try { 

        aCon = atf.characters[result.index]; 

        aCon.length = result[0].length; 

        myStyle.applyTo(aCon);

        } catch (e) {}; 

    } 

};

test();

pixxxelschubser
Community Expert
Community Expert
January 20, 2017

Well done Silly-V

Nothing to add …