Skip to main content
Known Participant
June 23, 2017
Answered

A script which imitates doc.TextSelection property

  • June 23, 2017
  • 2 replies
  • 622 views

What exactly does doc.TextSelection contain? How can I print its contents? and can anyone provide an example script using which one can essentially perform the equivalent of "selecting" the entire contents of a paragraph to perform some operations on it?

This topic has been closed for replies.
Correct answer Russ Ward

Hi varuns44789478,

A TextSelection property uses a TextRange structure, which is an array of two text locations (TextLoc). A TextLoc itself is an array of a paragraph object and an offset. It is a little tricky to understand, but it starts to make sense if you think about it for a while. The TextLoc is like an insertion point in the document, so it can be identified by the parent paragraph and the character offset from the beginning of the paragraph. A selection in a document is effectively a range between two text locations, hence the TextRange structure.

Here is basic code that would select an entire paragraph, assuming you already had the paragraph object:

var tr = new TextRange();

tr.beg.obj = tr.end.obj = pgf;

tr.beg.offset = 0;

tr.end.offset = Constants.FV_OBJ_END_OFFSET;

doc.TextSelection = tr;

As far as "printing" a text range, there is no direct way. You have to be creative. Below is a script that I use to print various things related to the current selection in a document or book. Just select something and run it. I hope this helps some.

Russ

function cm_ReportIds(doMsgBox, doConsole)

{

    var msg = "";

    var doingBook = false;

    var file = app.ActiveDoc;

   

    if(!file.ObjectValid())

    {

        file = app.ActiveBook;

        doingBook = true;

    }

   

    if(!file.ObjectValid())

    {

        alert ("No active document or book.");

        return;

    }

    msg += "----------------------------------------\n";

    if(doingBook)   

        msg += "ACTIVE BOOK: ";

    else  

        msg += "ACTIVE DOCUMENT: \n\n"

    msg += file.Name + "\n(ID: " + file.id + ")\n";

    msg += "----------------------------------------\n\n";

  

    msg += "----------------------------------------\n";

    msg += "CURRENT ELEMENT SELECTION (er = file.ElementSelection):\n\n";

   

    var er = file.ElementSelection;

   

    if(!er.beg.parent.ObjectValid() &&

        !er.beg.child.ObjectValid() &&

        !er.end.parent.ObjectValid() &&

        !er.end.child.ObjectValid())

    {

        msg += "(No valid element selection)\n";

    }

   

    else

    {

        msg += "er.beg.parent (element):  ";

        if(er.beg.parent.ObjectValid())

            msg += "<" + er.beg.parent.ElementDef.Name + ">      (ID: " + er.beg.parent.id + ")\n";

        else msg += "-NONE-\n";

       

        msg += "er.beg.child (element):    ";

        if(er.beg.child.ObjectValid())

            msg += "<" + er.beg.child.ElementDef.Name + ">      (ID: " + er.beg.child.id + ")\n";

        else msg += "-NONE-\n";

       

        msg += "er.beg.offset: " + er.beg.offset + "\n\n";

       

        msg += "er.end.parent (element):  " ;

        if(er.end.parent.ObjectValid())

            msg += "<" + er.end.parent.ElementDef.Name + ">      (ID: " + er.end.parent.id + ")\n";

        else msg += "-NONE-\n";

       

        msg += "er.end.child (element):    ";

        if(er.end.child.ObjectValid())

            msg += "<" + er.end.child.ElementDef.Name + ">      (ID: " + er.end.child.id + ")\n";

        else msg += "-NONE-\n";

       

        msg += "er.end.offset: " + er.end.offset + "\n";

    }

    msg += "----------------------------------------\n\n";

    if(!doingBook)

    {

        msg += "----------------------------------------\n";

        msg += "CURRENT TEXT SELECTION (tr = file.TextSelection):\n\n";

       

        var tr = file.TextSelection;

       

        if(tr.beg.obj.ObjectValid())

        {

            msg += "tr.beg.obj (pgf ID):  " + tr.beg.obj.id + "\n";

            msg += "tr.beg.obj (pgf format):  " + tr.beg.obj.Name + "\n";

            msg += "tr.beg.offset:  " + tr.beg.offset + "\n\n";

           

            msg += "tr.end.obj (pgf ID):  " + tr.end.obj.id + "\n";

            msg += "tr.end.obj (pgf format):  " + tr.end.obj.Name + "\n";

            msg += "tr.end.offset:  " + tr.end.offset + "\n";

        }

        else msg += "(No valid text selection)\n";

        msg += "----------------------------------------\n\n";

        msg += "----------------------------------------\n";

        msg += "CURRENT GRAPHIC SELECTION:\n\n";

      

       var graphic = file.FirstSelectedGraphicInDoc;

       var path = "";

       

        if(graphic.ObjectValid())

        {

            msg += "Selected graphic object: ";

            if(graphic.constructor.name == "AFrame")

            {

                msg += "Anchored frame       (ID: " + graphic.id + ")\n";

                var elem = graphic.Element;

                if(elem.ObjectValid())

                {

                    msg += "Anchored frame element:  <" +

                        elem.ElementDef.Name + ">    (ID: " + elem.id + ")\n";

                }

                msg += "First graphic object in the frame: ";

               

                graphic = graphic.FirstGraphicInFrame;

                if(graphic.ObjectValid())

                {

                    msg += graphic.constructor.name + "       (ID: " + graphic.id + ")\n";

                    if(graphic.constructor.name == "Inset")

                        path = graphic.InsetFile;

                }

                else msg += "(none detected)";

            }

       

            else

            {

                msg += graphic.constructor.name + "       (ID: " + graphic.id + ")\n";

                if(graphic.constructor.name == "Inset")

                    path = graphic.InsetFile;

               

                msg += "Parent anchored frame: ";

               

                graphic = graphic.FrameParent;

                if(graphic.ObjectValid() && graphic.constructor.name == "AFrame")

                {

                    msg += "  ID: " + graphic.id + "\n";

                    var elem = graphic.Element;

                    if(elem.ObjectValid())

                    {

                        msg += "Anchored frame element:  <" +

                            elem.ElementDef.Name + ">    (ID: " + elem.id + ")\n";

                    }

                }

                else msg += "(none detected)";

            }

       

            if(path != "")

            {

                msg += "\nReferenced graphic filepath:\n" + path + "\n";

            }

        }           

       

        else msg += "(No valid graphic selection)\n";

        msg += "----------------------------------------\n\n";

    }

   

    if(doMsgBox)

        alert(msg);

    if(doConsole)

    {

        while(msg.length > 0)

        {

            Err(msg.substring(0, 255));

            msg = msg.substring(255, msg.length);

        }

    }

}

2 replies

frameexpert
Community Expert
Community Expert
June 26, 2017

I am not sure exactly what you mean by printing the TextSelection, but here is a function that will retrieve the text so you can do something with it:

var doc = app.ActiveDoc;

// Display the selected text in an alert dialog box.

alert (getText (doc.TextSelection, doc));

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

    }

    return text; // Return the text

}

www.frameexpert.com
Known Participant
June 26, 2017

By printing the contents I mean being able to print the values of the TextSelection object attributes

Russ WardCorrect answer
Legend
June 26, 2017

Hi varuns44789478,

A TextSelection property uses a TextRange structure, which is an array of two text locations (TextLoc). A TextLoc itself is an array of a paragraph object and an offset. It is a little tricky to understand, but it starts to make sense if you think about it for a while. The TextLoc is like an insertion point in the document, so it can be identified by the parent paragraph and the character offset from the beginning of the paragraph. A selection in a document is effectively a range between two text locations, hence the TextRange structure.

Here is basic code that would select an entire paragraph, assuming you already had the paragraph object:

var tr = new TextRange();

tr.beg.obj = tr.end.obj = pgf;

tr.beg.offset = 0;

tr.end.offset = Constants.FV_OBJ_END_OFFSET;

doc.TextSelection = tr;

As far as "printing" a text range, there is no direct way. You have to be creative. Below is a script that I use to print various things related to the current selection in a document or book. Just select something and run it. I hope this helps some.

Russ

function cm_ReportIds(doMsgBox, doConsole)

{

    var msg = "";

    var doingBook = false;

    var file = app.ActiveDoc;

   

    if(!file.ObjectValid())

    {

        file = app.ActiveBook;

        doingBook = true;

    }

   

    if(!file.ObjectValid())

    {

        alert ("No active document or book.");

        return;

    }

    msg += "----------------------------------------\n";

    if(doingBook)   

        msg += "ACTIVE BOOK: ";

    else  

        msg += "ACTIVE DOCUMENT: \n\n"

    msg += file.Name + "\n(ID: " + file.id + ")\n";

    msg += "----------------------------------------\n\n";

  

    msg += "----------------------------------------\n";

    msg += "CURRENT ELEMENT SELECTION (er = file.ElementSelection):\n\n";

   

    var er = file.ElementSelection;

   

    if(!er.beg.parent.ObjectValid() &&

        !er.beg.child.ObjectValid() &&

        !er.end.parent.ObjectValid() &&

        !er.end.child.ObjectValid())

    {

        msg += "(No valid element selection)\n";

    }

   

    else

    {

        msg += "er.beg.parent (element):  ";

        if(er.beg.parent.ObjectValid())

            msg += "<" + er.beg.parent.ElementDef.Name + ">      (ID: " + er.beg.parent.id + ")\n";

        else msg += "-NONE-\n";

       

        msg += "er.beg.child (element):    ";

        if(er.beg.child.ObjectValid())

            msg += "<" + er.beg.child.ElementDef.Name + ">      (ID: " + er.beg.child.id + ")\n";

        else msg += "-NONE-\n";

       

        msg += "er.beg.offset: " + er.beg.offset + "\n\n";

       

        msg += "er.end.parent (element):  " ;

        if(er.end.parent.ObjectValid())

            msg += "<" + er.end.parent.ElementDef.Name + ">      (ID: " + er.end.parent.id + ")\n";

        else msg += "-NONE-\n";

       

        msg += "er.end.child (element):    ";

        if(er.end.child.ObjectValid())

            msg += "<" + er.end.child.ElementDef.Name + ">      (ID: " + er.end.child.id + ")\n";

        else msg += "-NONE-\n";

       

        msg += "er.end.offset: " + er.end.offset + "\n";

    }

    msg += "----------------------------------------\n\n";

    if(!doingBook)

    {

        msg += "----------------------------------------\n";

        msg += "CURRENT TEXT SELECTION (tr = file.TextSelection):\n\n";

       

        var tr = file.TextSelection;

       

        if(tr.beg.obj.ObjectValid())

        {

            msg += "tr.beg.obj (pgf ID):  " + tr.beg.obj.id + "\n";

            msg += "tr.beg.obj (pgf format):  " + tr.beg.obj.Name + "\n";

            msg += "tr.beg.offset:  " + tr.beg.offset + "\n\n";

           

            msg += "tr.end.obj (pgf ID):  " + tr.end.obj.id + "\n";

            msg += "tr.end.obj (pgf format):  " + tr.end.obj.Name + "\n";

            msg += "tr.end.offset:  " + tr.end.offset + "\n";

        }

        else msg += "(No valid text selection)\n";

        msg += "----------------------------------------\n\n";

        msg += "----------------------------------------\n";

        msg += "CURRENT GRAPHIC SELECTION:\n\n";

      

       var graphic = file.FirstSelectedGraphicInDoc;

       var path = "";

       

        if(graphic.ObjectValid())

        {

            msg += "Selected graphic object: ";

            if(graphic.constructor.name == "AFrame")

            {

                msg += "Anchored frame       (ID: " + graphic.id + ")\n";

                var elem = graphic.Element;

                if(elem.ObjectValid())

                {

                    msg += "Anchored frame element:  <" +

                        elem.ElementDef.Name + ">    (ID: " + elem.id + ")\n";

                }

                msg += "First graphic object in the frame: ";

               

                graphic = graphic.FirstGraphicInFrame;

                if(graphic.ObjectValid())

                {

                    msg += graphic.constructor.name + "       (ID: " + graphic.id + ")\n";

                    if(graphic.constructor.name == "Inset")

                        path = graphic.InsetFile;

                }

                else msg += "(none detected)";

            }

       

            else

            {

                msg += graphic.constructor.name + "       (ID: " + graphic.id + ")\n";

                if(graphic.constructor.name == "Inset")

                    path = graphic.InsetFile;

               

                msg += "Parent anchored frame: ";

               

                graphic = graphic.FrameParent;

                if(graphic.ObjectValid() && graphic.constructor.name == "AFrame")

                {

                    msg += "  ID: " + graphic.id + "\n";

                    var elem = graphic.Element;

                    if(elem.ObjectValid())

                    {

                        msg += "Anchored frame element:  <" +

                            elem.ElementDef.Name + ">    (ID: " + elem.id + ")\n";

                    }

                }

                else msg += "(none detected)";

            }

       

            if(path != "")

            {

                msg += "\nReferenced graphic filepath:\n" + path + "\n";

            }

        }           

       

        else msg += "(No valid graphic selection)\n";

        msg += "----------------------------------------\n\n";

    }

   

    if(doMsgBox)

        alert(msg);

    if(doConsole)

    {

        while(msg.length > 0)

        {

            Err(msg.substring(0, 255));

            msg = msg.substring(255, msg.length);

        }

    }

}

Known Participant
June 26, 2017

Hi Russ,

This helps a lot.

Thanks,

Varun