Skip to main content
Known Participant
April 22, 2020
Answered

Script no longer working

  • April 22, 2020
  • 2 replies
  • 2970 views

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.)

This topic has been closed for replies.
Correct answer Klaus Göbel

Here's an improved version:

 

Replace

while (pgf.ObjectValid () === 1) {
     if (pgf.InTextFrame.FrameParent.PageFramePage.constructor.name == "BodyPage")
            {
            if (pgfRegex.test (pgf.Name) === true) {
            processNewlinkPgf (pgf, doc);
            }
        }
    pgf = pgf.NextPgfInDoc;
    }

 

with:

  
  var oPage;  
    
while (pgf.ObjectValid () === 1) {
    
     var oUAFramePage = pgf.InTextFrame.FrameParent.FrameParent.PageFramePage;
        
    oPage = pgf.InTextFrame.FrameParent.PageFramePage;
    
    if (!oPage.ObjectValid())
        {
        oPage = oUAFramePage;
        }
   
    if (oPage.constructor.name == "BodyPage")
            {
            if (pgfRegex.test (pgf.Name) === true) {
            processNewlinkPgf (pgf, doc);
            }
        }
    pgf = pgf.NextPgfInDoc;
    }

 

2 replies

Klaus Göbel
Klaus GöbelCorrect answer
Legend
May 7, 2020

Here's an improved version:

 

Replace

while (pgf.ObjectValid () === 1) {
     if (pgf.InTextFrame.FrameParent.PageFramePage.constructor.name == "BodyPage")
            {
            if (pgfRegex.test (pgf.Name) === true) {
            processNewlinkPgf (pgf, doc);
            }
        }
    pgf = pgf.NextPgfInDoc;
    }

 

with:

  
  var oPage;  
    
while (pgf.ObjectValid () === 1) {
    
     var oUAFramePage = pgf.InTextFrame.FrameParent.FrameParent.PageFramePage;
        
    oPage = pgf.InTextFrame.FrameParent.PageFramePage;
    
    if (!oPage.ObjectValid())
        {
        oPage = oUAFramePage;
        }
   
    if (oPage.constructor.name == "BodyPage")
            {
            if (pgfRegex.test (pgf.Name) === true) {
            processNewlinkPgf (pgf, doc);
            }
        }
    pgf = pgf.NextPgfInDoc;
    }

 

jmyers2Author
Known Participant
May 14, 2020

That did it. Thanks!

Klaus Göbel
Legend
May 1, 2020

Hi jmyers2

If you work with "FirstBodyPageInDoc" and "NextPgfInDoc", the next paragraph is not necessarily on the current body page.It can be anywhere in the document (RefPage - masterpage or another bodypage. Also you don't have "PageNext" anywhere and you don't check if the paragraph is still on the corresponding "BodyPage" or "TextFrame". So you would also have to start with "NextGraphicInFrame" work.
I suggest you work with a different approach that makes things easier and clearer:

 

 

function processDoc (doc) {
var pgfRegex, page, frame, textFrame, pgf;
pgf = doc.FirstPgfInDoc;

// 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 (pgf.InTextFrame.FrameParent.PageFramePage.constructor.name == "BodyPage")
            {
            if (pgfRegex.test (pgf.Name) === true) {
            processNewlinkPgf (pgf, doc);
            }
        }
    pgf = pgf.NextPgfInDoc;
    }
}

 

jmyers2Author
Known Participant
May 6, 2020

Thank you, I'll give that a try.