Script no longer working
I have a script that was working perfectly, but no longer does. I'm wondering if a recent update from FrameMaker might have changed/broken something. Did any property names change? I have 2019, version 15.0.4.751.
The full script is below. Basically what it does is read through the body pages looking for paragraphs with a specific paragraph tag. The paragraphs may or may not be part of the main flow. If found, it adds a newlink hypertext marker. However, now it doesn't only look at the body page; it picks up items from the reference page, too. Also, it's missing some of the paragraphs it's supposed to work on.
Has anyone else experienced this? The script is a huge timesaver for us, but the last two documents I've run it on, it's skipped all but one paragraph that it should have added the newlink marker to.
--------
#target framemaker
main ();
// Check for an open document
function main () {
var doc;
doc = app.ActiveDoc;
if (doc.ObjectValid () === 1) {
processDoc (doc);
}
else {
alert ("There is no active document.");
}
}
function processDoc (doc) {
var pgfRegex, page, frame, textFrame, pgf;
page = doc.FirstBodyPageInDoc;
frame = page.PageFrame;
textFrame = frame.FirstGraphicInFrame
pgf = textFrame.FirstPgf;
// Turn off the displaying property to speed the script and prevent flicker.
if (app.Displaying === 1) {
app.Displaying = 0;
}
// Match CaptionFigure and CaptionTable paragraph formats.
pgfRegex = /^(CaptionFigure|CaptionTable)$/;
while (pgf.ObjectValid () === 1) {
if (pgfRegex.test (pgf.Name) === true) {
processNewlinkPgf (pgf, doc);
}
pgf = pgf.NextPgfInDoc;
}
// Restore the document's display and refresh the screen.
if (app.Displaying === 0) {
app.Displaying = 1;
doc.Redisplay ();
alert("Markers are added");
}
}
// FUNCTION FOR REGULAR CAPTIONS
function processNewlinkPgf (pgf, doc) {
var num, marker;
num = getFigOrTableNum (pgf);
if (num) {
// See if a newlink marker already exists in the paragraph.
marker = getMarker (pgf, "Hypertext", "newlink");
if (marker) {
// Update the existing marker's text.
marker.MarkerText = "newlink " + num;
}
else { // No existing marker; create a new one.
createMarker (doc, pgf, 0, "Hypertext", "newlink " + num);
}
}
else {
Console ("Couldn't find number: " + pgf.Name);
}
}
// END FUNCTION
//See if a marker exists in a paragraph
function getMarker (pgf, markerType, text) {
var markers = [], marker, textList, i;
textList = pgf.GetText (Constants.FTI_MarkerAnchor);
for (i = 0; i < textList.len; i += 1) {
marker = textList[i].obj;
if (marker.MarkerTypeId.Name === markerType) {
if (marker.MarkerText.indexOf (text) > -1) {
return marker;
}
}
}
}
// Create a new marker
function createMarker(doc, pgf, offset, type, text) {
var tLoc = new TextLoc(pgf, offset);
var marker = doc.NewAnchoredObject(Constants.FO_Marker, tLoc);
var markerType = doc.GetNamedObject(Constants.FO_MarkerType, type);
marker.MarkerTypeId = markerType;
marker.MarkerText = text;
return 1;
}
// FUNCTION FOR REGULAR CAPTIONS
function getFigOrTableNum (pgf) {
var doc;
doc = app.ActiveDoc;
var regex, pgfText, match;
regex = /(?:Figure|Table) ((AF|AT|[FT])(\d+))/; // Match figure or table and number in text
pgfText = getText (pgf, doc);
if (regex.test (pgfText) === true) {
match = pgfText.match (regex);
return (match[1].toLowerCase ());
}
}
//END FUNCTION
function getText (textObj, doc) {
// Gets the text from the text object.
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
}
(@frameexpert is the one who helped me with this script originally.)
