[ Branched ] Select Character and replace font?

Explorer ,
Nov 18, 2020

Copy link to clipboard

Copied

Hi, this is great, but I can only get it to work on the first character of the regex I input i.e. I need to change 'EN' to another font so have used 'var reg = /EN/g;' but it only seems to change the 'E' and not the 'N' to my desired font.

 

How would I get around this issue?

[ branched from Select Character and replace font? by moderator ]

 

TOPICS
How to, Scripting

Views

68

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

1 Correct Answer

Most Valuable Participant , Nov 18, 2020
pixxxelschubser Most Valuable Participant , Nov 18, 2020
Your own script snippet only changes the first occurrence of your search string in a text frame. The problem is: For the solution in the other thread, we didn't need all the properties of reg.exec () But we absolutely need the properties for your request.   Try the following: // regex_textRange_changeFormattingOfString.jsx // regards pixxxelschubser var aDoc = app.activeDocument; var aTF = aDoc.textFrames[0]; var reg = /EN/g; var res = null; var aCon = null; while (res = reg.exec(aTF.contents))...

Likes

Translate

Translate
Jump to answer Jump to answer
Explorer ,
Nov 18, 2020

Copy link to clipboard

Copied

I have temporarily got around the issue by doing the following, but thisis far from ideal. Is anyone able to help me on how to do this in the correct manner? Many thanks in advance:



var aDoc = app.activeDocument;
var aTF = aDoc.textFrames[0];
var reg = /EN/g;
var res = reg.exec(aTF.contents);
idx1 = res.index;
idx2 = res.index+1;
aTF.characters[idx1].textFont = app.textFonts.getByName("ArialMT");
aTF.characters[idx2].textFont = app.textFonts.getByName("ArialMT");

 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Most Valuable Participant ,
Nov 18, 2020

Copy link to clipboard

Copied

Your own script snippet only changes the first occurrence of your search string in a text frame.


The problem is: For the solution in the other thread, we didn't need all the properties of reg.exec ()

But we absolutely need the properties for your request.

 

Try the following:

// regex_textRange_changeFormattingOfString.jsx
// regards pixxxelschubser

var aDoc = app.activeDocument;
var aTF = aDoc.textFrames[0];
var reg = /EN/g;
var res = null;
var aCon = null;
while (res = reg.exec(aTF.contents)) {
    aCon = aTF.characters[res.index];
    aCon.length = res[0].length;
    aCon.textFont = app.textFonts.getByName("ArialMT");
    }

 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
glibshaft AUTHOR LATEST
Explorer ,
Nov 19, 2020

Copy link to clipboard

Copied

Thank you so much for this, it is exactly what I was after. Greatly appreciated.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more