Copy link to clipboard
Copied
This code (below) is supposed to search text for a specific string (defined by regex), and if found, make the string bold. It finds and returns the text it's looking for, but when it applies the character format, it changes the entire paragraph, not just the string. So, I think it's the text range/text selection that I'm having trouble with. I've spent too much time trying to figure this out on my own, but I just can't get it. Can someone please help me?
I'm using FrameMaker 2019. Thank you!
#target framemaker
var doc = app.ActiveDoc;
var pgf = doc.TextSelection.beg.obj;
processLinks (pgf, doc);
function processLinks (pgf, doc) {
var end = Constants.FV_OBJ_END_OFFSET - 1, begin = 0, textRange, charTag;
var textList = pgf.GetText (Constants.FTI_CharPropsChange);
for (var i = textList.length - 1; i >= 0; i -= 1) {
begin = textList[i].offset;
if (begin !== end) {
textRange = new TextRange (new TextLoc (pgf, begin), new TextLoc (pgf, end));
num = getFigOrTable (pgf);
if (num) {
alert (num); //this is the string that matches the regex.
applyCharFmt (textRange, "Bold", doc); // makes whole para bold, nut just num
}
end = begin;
}
}
if (end > 0) {
textRange = new TextRange (new TextLoc (pgf,0), new TextLoc (pgf,end));
num = getFigOrTable (pgf);
if (num) {
alert (num); //this is the string that matches the regex.
applyCharFmt (textRange, "Bold", doc);
}
}
}
function getFigOrTable (pgf) {
var doc;
doc = app.ActiveDoc;
var regex, pgfText, match;
regex = /(?:Figure|Table|Plate) ((AF|AT|[FTP])(\d+))/; // Match figure or table and number in text.
pgfText = getText (pgf, doc);
if (regex.test (pgfText) === true) {
match = pgfText.match (regex);
return (match); // this returns Figure F1, F1, F, 1
}
}
function getText (textObj, doc) {
// Gets the text from the text object or text range.
var text = "", textItems, i;
// Get a list of the strings in the text object or text range.
if (textObj.constructor.name !== "TextRange") {
textItems = textObj.GetText(Constants.FTI_String);
} else {
textItems = doc.GetTextForRange(textObj, Constants.FTI_String);
}
// Concatenate the strings.
for (i = 0; i < textItems.len; i += 1) {
text += (textItems[i].sdata);
}
return text; // Return the text
}
function applyCharFmt (textRange, name, doc) {
var charFmt = 0;
charFmt = doc.GetNamedCharFmt (name);
if (charFmt.ObjectValid()) {
doc.SetTextProps (textRange, charFmt.GetProps());
}
}
Hi,
sorry for my late response, but I had a "buffer overflow" 😉
You have to do something like that:
var textList = pgf.GetText (Constants.FTI_String);
for (var i = textList.length - 1; i >= 0; i -= 1)
{
num = getFigOrTable (pgf);
begin = num.index;
end = begin + num[0].length;
textRange = new TextRange (new TextLoc (pgf, begin), new TextLoc (pgf, end));
applyCharFmt (textRange, "Bold", doc);
}
Because what you call "num" is the regex-match and you have to use start a
...Copy link to clipboard
Copied
Hi,
sorry for my late response, but I had a "buffer overflow" 😉
You have to do something like that:
var textList = pgf.GetText (Constants.FTI_String);
for (var i = textList.length - 1; i >= 0; i -= 1)
{
num = getFigOrTable (pgf);
begin = num.index;
end = begin + num[0].length;
textRange = new TextRange (new TextLoc (pgf, begin), new TextLoc (pgf, end));
applyCharFmt (textRange, "Bold", doc);
}
Because what you call "num" is the regex-match and you have to use start and end of the word (e.g. Figure 1") for the textrange in document.
But make sure, that in regex you can have more than one hit ("/g"). Then you need a for-loop.
Copy link to clipboard
Copied
Klaus,
That did it ... thank you so much!
Julie