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 ]
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))
...
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");
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");
}
Copy link to clipboard
Copied
Thank you so much for this, it is exactly what I was after. Greatly appreciated.