Skip to main content
Participant
October 1, 2017
Answered

Moving markers inside a paragraph

  • October 1, 2017
  • 1 reply
  • 932 views

Hi;

I just started working with Framemaker scripting this afternoon and using sample scripts here I've got the main skeleton of my program complete. I know that a similar script was posted here years ago to move markers to the start of words, but the script appears to have vanished and the one I'm working on needs to move the markers to the beginning of the paragraph that they're currently residing in (it help translation immensely).

So far my program opens a book and, one chapter at at time, finds all of the Index markers. I need to move those markers, but I don't have enough of a handle on the language yet to get it working.

The key function is here:

function moveMarker(doc, mrkType, pgf, offset)

{

    if (!doc.ObjectValid() )

    {

        return;

     }

    mrker = doc.FirstMarkerInDoc;

    while (mrker.ObjectValid() )

    {

        if (mrker.MarkerTypeId.Name == mrkType)

        {

             var str1 = mrker.MarkerText;

            

             /*  A quick test to confirm it's finding the Index markers */

            

               $.writeln(str1);

                 cnt ++;

                

             /* 

                 A counter to check how many Index markers it's finding

                 in the whole book -- will need a counter for number in each paragraph

                 to use as the offset from beginning of each paragraph

        */ 

               $.writeln(cnt);

         }

        mrker = mrker.NextMarkerInDoc;

      }

  }

My thoughts are to start each paragraph with a paragraph-counter (p-counter) set to "0"; once the script finds an Index marker, it should cut the marker, then past it to the start of the paragraph using the p-counter as the offset, then increment the p-counter (that way it should always paste the marker after the last one -- so it won't re-find the markers it's already found).

The main script currently starts with the first Index marker in a document and, when it hits the end of a document, moves to the next document in a book -- I know I'll need to have it start with the first paragraph in a chapter and track paragraphs as it goes.

Any suggestions on how best to do the cut--and-paste, as well last track the paragraph the marker is in?

Any help would be greatly appreciated.

Thanks,

Walt Sterdan

This topic has been closed for replies.
Correct answer frameexpert

This is how I would do it for the paragraph containing the insertion point. Of course, you will need a loop for all of the paragraphs in the document and then a way to process all of the files in the book, but one step at a time.

#target framemaker

var doc = app.ActiveDoc;

// Get the paragraph containing the insertion point.

var pgf = doc.TextSelection.beg.obj;

var marker;

// Get a list of markers from the paragraph.

var textList = pgf.GetText (Constants.FTI_MarkerAnchor);

// Process the list backwards so that the markers stay in the same order.

for (var i = textList.length - 1; i >= 0; i -= 1) {

    marker = textList.obj;

    if (marker.MarkerTypeId.Name === "Index") {

        moveMarker (marker, pgf, doc);

    }

}

function moveMarker (marker, pgf, doc) {

   

    var textRange;

   

    // Select the marker.

    textRange = new TextRange (new TextLoc (pgf, marker.TextLoc.offset),

        new TextLoc (pgf, marker.TextLoc.offset + 1));

    doc.TextSelection = textRange;

    // Cut the marker.

    PushClipboard ();

    doc.Cut ();

   

    // Put the cursor at the beginning of the paragraph.

    textRange = new TextRange (new TextLoc (pgf, 0), new TextLoc (pgf, 0));

    doc.TextSelection = textRange;

   

    // Paste the marker.

    doc.Paste ();

    PopClipboard ();

}

1 reply

Participant
October 1, 2017

Hi;

It dawns on me that finding the "next" Index marker isn't going to find the next Index marker in the book by position, but by the order of creation.

This simplifies things a bit; it means that a counter isn't needed for the offset -- I only need to cut the Index Marker and paste it at the beginning of the paragraph with offset = "0".

At this point, I only need hints as to how to cut the Index Marker that's been found, and paste it at the beginning of the "current' paragraph.

-- Walt Sterdan

frameexpert
Community Expert
frameexpertCommunity ExpertCorrect answer
Community Expert
October 2, 2017

This is how I would do it for the paragraph containing the insertion point. Of course, you will need a loop for all of the paragraphs in the document and then a way to process all of the files in the book, but one step at a time.

#target framemaker

var doc = app.ActiveDoc;

// Get the paragraph containing the insertion point.

var pgf = doc.TextSelection.beg.obj;

var marker;

// Get a list of markers from the paragraph.

var textList = pgf.GetText (Constants.FTI_MarkerAnchor);

// Process the list backwards so that the markers stay in the same order.

for (var i = textList.length - 1; i >= 0; i -= 1) {

    marker = textList.obj;

    if (marker.MarkerTypeId.Name === "Index") {

        moveMarker (marker, pgf, doc);

    }

}

function moveMarker (marker, pgf, doc) {

   

    var textRange;

   

    // Select the marker.

    textRange = new TextRange (new TextLoc (pgf, marker.TextLoc.offset),

        new TextLoc (pgf, marker.TextLoc.offset + 1));

    doc.TextSelection = textRange;

    // Cut the marker.

    PushClipboard ();

    doc.Cut ();

   

    // Put the cursor at the beginning of the paragraph.

    textRange = new TextRange (new TextLoc (pgf, 0), new TextLoc (pgf, 0));

    doc.TextSelection = textRange;

   

    // Paste the marker.

    doc.Paste ();

    PopClipboard ();

}

www.frameexpert.com
Participant
October 2, 2017

Hi;

Thanks very much for the quick response, it looks like it should do the job.

I'll let you know how well it works once I've had a chance to test it, but at the very least the code involving the clipboard cut/paste looks like exactly what was stumping me.

Thanks again,

Walt