Skip to main content
roym76266846
Known Participant
January 16, 2025
Answered

FrameMaker - ExtendScript Writing text to console

  • January 16, 2025
  • 2 replies
  • 384 views

Hello 

I am trying to create an ExtendScript that takes contents from a FrameMaker document and write it to the Console in the first instance:

This is my attempt:

var doc = app.ActiveDoc;
var firstPgf = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;floop(firstPgf);

function floop(Pgf){
while(Pgf){
var pgfText = Pgf.GetText(Constants.FTI_String);
Pgf=doc.MainFlowInDoc.FirstTextFrameInFlow.NextPgf;
for(var i=0; i<pgfText.length;i++){
$.writeln(pgfText[i]);
}
$.writeln(pgfText);
}
}

------

The output I get is:

[object TextItem]
Result: undefined
[object TextItem]
[object TextItem]
[object TextItem]
[object TextItem]
[object TextItem]
[object TextItem],[object TextItem],[object TextItem],[object TextItem],[object TextItem]
Result: undefined

The object instances returned seem to be empty or not read at all.

Can you advise?

Thanks

 

    Correct answer frameexpert

    There are some other problems with your code. Here is what you can use for a shell; add the getText function to the bottom of this script:

    #target framemaker
    
    main ();
    
    function main () {
        
        var doc;
        
        doc = app.ActiveDoc;
        if (doc.ObjectValid () === 1) {
            processDoc (doc);
        }
    }
    
    function processDoc (doc) {
        
        var pgf;
        
        pgf = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
        while (pgf.ObjectValid () === 1) {
            Console (getText (pgf, doc));
            pgf = pgf.NextPgfInFlow;
        }
    }
    

    2 replies

    frameexpert
    Community Expert
    frameexpertCommunity ExpertCorrect answer
    Community Expert
    January 16, 2025

    There are some other problems with your code. Here is what you can use for a shell; add the getText function to the bottom of this script:

    #target framemaker
    
    main ();
    
    function main () {
        
        var doc;
        
        doc = app.ActiveDoc;
        if (doc.ObjectValid () === 1) {
            processDoc (doc);
        }
    }
    
    function processDoc (doc) {
        
        var pgf;
        
        pgf = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
        while (pgf.ObjectValid () === 1) {
            Console (getText (pgf, doc));
            pgf = pgf.NextPgfInFlow;
        }
    }
    
    roym76266846
    Known Participant
    January 17, 2025

    Thanks very much. It works.

    frameexpert
    Community Expert
    Community Expert
    January 16, 2025

    TextItems can be a little complicated to figure out because you have to know what properties to extract. Here is a function that you call for each paragraph that will give you the paragraph's text:

    function getText (textObj, doc) {
        
        // Gets the text from the text object or text range.
    
        var textItems, text, 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.
        text = "";
        for (i = 0; i < textItems.len; i += 1) {
            text += (textItems[i].sdata);
        }
        
        return text; // Return the text
    }