Skip to main content
K.Daube
Community Expert
Community Expert
February 21, 2019
Answered

Make selection visible for table/frame

  • February 21, 2019
  • 3 replies
  • 1864 views

Dear all,

In my script FMGraph I collect tables and anchored frames in arrays of objects (aoObjects). An arry element contains

aoObjects[iObject].Name     // User string from the object

aoObjects[iObject].Obj      // the object (Tbl, AFrame)

aoObjects[iObject].TL       // text loction of object (anchor paragraph)

When I then navigate to a particular object i want to see that it is selected (there may be multiple tables/frames on a page).

  oTL = aoObjects[iObject].TL;

  oTR = new TextRange(oTL, oTL);

  oDoc.TextSelection = oTR;

  oDoc.ScrollToText(oTR);

selects the anchor point - not the object itself. How to select the object to make the selection visible?

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

Hi Klaus,

here's the way to select a table:

    var oDoc = app.ActiveDoc;

    var aTabs = [];

    var flow = oDoc.MainFlowInDoc;

    GetTblInFlow(oDoc,aTabs);

    var SelTbl = aTabs[0];

    oDoc.SelectedTbl = SelTbl.obj;

    var oPgf = SelTbl.obj.TextLoc.obj

    var oTblAnchors = oPgf.GetText(Constants.FTI_TblAnchor);

      

    var oAnchorOffset = oTblAnchors[0].offset;

  

    var oTlocStart = new TextLoc (oPgf, oAnchorOffset);

    var oTlocEnd = new TextLoc (oPgf, oAnchorOffset +1);

    oDoc.TextSelection = new TextRange(oTlocStart,oTlocEnd);

function GetTblInFlow(foDoc,faTabs)

{

    var textItems = flow.GetText(Constants.FTI_TblAnchor);

    for (var i = 0; i < textItems.length; i++)

        {

        faTabs.push(textItems)

        }

}

3 replies

frameexpert
Community Expert
Community Expert
February 21, 2019

To test the code below, put your cursor in any table in the active document.

#target framemaker

var doc, tbl, textLoc, textRange;

doc = app.ActiveDoc;

tbl = doc.SelectedTbl;

// Text location of the table

textLoc = tbl.TextLoc;

// Make a text range so that it includes the table anchor.

textRange = new TextRange (textLoc, new TextLoc (textLoc.obj, textLoc.offset + 1));

// Select the table.

doc.TextSelection = textRange;

Of course, best practice is to generalize this into a function so you can use it on any table:

selectTable (tbl, doc);

function selectTable (tbl, doc) {

   

    var textLoc, textRange;

   

    textLoc = tbl.TextLoc;

    textRange = new TextRange (textLoc,

        new TextLoc (textLoc.obj, textLoc.offset + 1));

    doc.TextSelection = textRange;

}

You are dealing with tables and anchored frames so you can generalize it further (it would also work with markers):

selectAnchoredObject (tbl, doc);

function selectAnchoredObject (anchoredObj, doc) {

   

    var textLoc, textRange;

   

    textLoc = anchoredObj.TextLoc;

    textRange = new TextRange (textLoc,

        new TextLoc (textLoc.obj, textLoc.offset + 1));

    doc.TextSelection = textRange;

}

www.frameexpert.com
K.Daube
Community Expert
K.DaubeCommunity ExpertAuthor
Community Expert
February 21, 2019

Wow, such a host of good ideas!

I will need to check out which one would fit best...

Klaus: thanks for this method to collect the tables/frames. I will check whether the order is as they appear (OK) or as they are created (less OK).

Jang: Thanks for the tip - it seems that Rick already deals with that +1 in 2nd and 3rd snippet.

Rick: Great input

So, let me work with Your contributions!

Klaus

K.Daube
Community Expert
K.DaubeCommunity ExpertAuthor
Community Expert
February 21, 2019

Combining Klaus' and Rick's input I got this working prototype:

/* CollectTables.jsx ====== UTF-8 =================================================================

             Collect tables in main flow and select them one be the other.

             Display the User String of the object

Reference    Klaus Göbel, Rick Quatro; https://forums.adobe.com/thread/2597350

History      2019-02-21

*/ ; // ===========================================================================================

#target framemaker

main ();

function main () {

var oDoc, aoTables = [], oFlow, nTables, j, sName;

  oDoc = app.ActiveDoc;

  oFlow = oDoc.MainFlowInDoc;

  GetTblInFlow (oFlow,aoTables);

  nTables = aoTables.length;

  for (j = 0; j < nTables; j++) {

    SelectAnchoredObject (aoTables, oDoc)

    sName = aoTables.obj.UserString;

    alert (sName, "Usert String", false);        // stop to see the selection

  }

  alert (nTables + " tables displayed", "Collect Tables in Flow", false);

} //--- end main

function GetTblInFlow(oFlow,aoTables)  {

var j, aTextItems = [];

  aTextItems = oFlow.GetText(Constants.FTI_TblAnchor);

  for (var j = 0; j < aTextItems.length; j++)  {

    aoTables.push(aTextItems)

  }

} //--- end GetTblInFlow

function SelectAnchoredObject (anchoredObj, oDoc) {

var oTL, oTR, oTL1;

  oTL = anchoredObj.obj.TextLoc;

  oTL1 =  new TextLoc (oTL.obj, oTL.offset + 1);

  oTR = new TextRange (oTL, oTL1);

  oDoc.TextSelection = oTR;

  oDoc.ScrollToText(oTR);                        // Selection is viewed

} //--- end SelectAnchoredObject

Thank You all - to whom I now should give the credit?

... and of course it was easy to create the variant of this for anchored frames!

4everJang
Legend
February 21, 2019

What Rick is indicating in a general way comes down to one small change in your existing script. Instead of creating a text range from two identical TLoc elements, you need to create a second TLoc element with an offset + 1. What your code is doing is pointing to a text location. Making the range extend one character position to the right will make the entire object fall within the selection.

You could try to add this line after you create your TR:

TR.end.offset++;

Klaus Göbel
Klaus GöbelCorrect answer
Legend
February 21, 2019

Hi Klaus,

here's the way to select a table:

    var oDoc = app.ActiveDoc;

    var aTabs = [];

    var flow = oDoc.MainFlowInDoc;

    GetTblInFlow(oDoc,aTabs);

    var SelTbl = aTabs[0];

    oDoc.SelectedTbl = SelTbl.obj;

    var oPgf = SelTbl.obj.TextLoc.obj

    var oTblAnchors = oPgf.GetText(Constants.FTI_TblAnchor);

      

    var oAnchorOffset = oTblAnchors[0].offset;

  

    var oTlocStart = new TextLoc (oPgf, oAnchorOffset);

    var oTlocEnd = new TextLoc (oPgf, oAnchorOffset +1);

    oDoc.TextSelection = new TextRange(oTlocStart,oTlocEnd);

function GetTblInFlow(foDoc,faTabs)

{

    var textItems = flow.GetText(Constants.FTI_TblAnchor);

    for (var i = 0; i < textItems.length; i++)

        {

        faTabs.push(textItems)

        }

}