Skip to main content
sam95317750
Known Participant
June 10, 2020
Question

Function not working

  • June 10, 2020
  • 1 reply
  • 386 views

I am trying to do the following using the codes already available on this forum:

 

1. find marker type Index and marker text goto 

if 1 is true then it should exit

if 1 is not true then it should move forward and create new markers

Following is the function:

 

var regex, marker1;
regex = "goto";
function setMarker(doc, pgf)
{
var bMarkerExists = false;
while (marker1.ObjectValid () === 1) {
	bMarkerExists = false;
    // Make sure it is an Hypertext marker.
    if (marker1.MarkerTypeId.Name === "Index") {
        // Check the text.
        if (regex.test (marker1.MarkerText) === true) {

        alert ("markers already present");
		bMarkerExists = true;
		break;

		}

    }
 marker1 = marker1.NextMarkerInDoc;
			
if (!bMarkerExists)
          {

		if (pgf.Name == target1.Name)   {

			createMarker (doc, pgf, 4, "Index", "goto" + getText(pgf));

		}

		else if (pgf.Name == target2.Name)  {

			createMarker (doc, pgf, 4, "Index", "goto" + getText(pgf));

		}
}
pgf = pgf.NextPgfInFlow;

}

}

 

    This topic has been closed for replies.

    1 reply

    frameexpert
    Community Expert
    Community Expert
    June 10, 2020

    You don't say exactly what is not working. Here are a couple of things to consider:

     

    1) Your definition of your regular expression variable (regex) is wrong. It should be

    regex = /goto/;

     

    2) The flow of your code is not clear. I don't see where you define the marker1 variable or where you call the setMarker function. Where are you defining the pgf variable? According to your code, you have a createMarker function, although you didn't post it.

     

    The best approach is to break this down into separate tasks:

     

    1) Define a function that just does the marker test. Return true if it finds a marker.

    2) Define a separate function that loops through the paragraphs in the document and inserts appropriate Index markers.

     

    Once you create and troubleshoot each of these functions, then you can chain them together in a larger script.

     

    www.frameexpert.com
    sam95317750
    Known Participant
    June 10, 2020

    Hi frameexpert,

     

    following si he complete code. I want to add a check (which I am unable to do) if there is marker type Hypertext and marker text newlink Filename present, it does not adds markers.

     

    #target framemaker 
    #strict on
        
        var doc = app.ActiveDoc;
        var flow = doc.MainFlowInDoc;
        var tframe = flow.FirstTextFrameInFlow;
        var goPgf = tframe.FirstPgf;
        
        var markerTypeName = "Hypertext";
        
        var target1 = "h1 head 1";
        var target2 = "h1t head 1.top";
        
        var markerType = doc.GetNamedObject(Constants.FO_MarkerType, markerTypeName);
           
        /*if (!markerType.ObjectValid())
            {
             markerType = doc.NewNamedMarkerType (markerTypeName);    
            }*/
            
        setMarker(doc,goPgf);
        
    function setMarker(foDoc,foPgf)
    {
        var sText;
        var bMarkerExists = false;
        
        
        while (foPgf.ObjectValid())
            {
            bMarkerExists = false;
            
            var oMarkers = foPgf.GetText(Constants.FTI_MarkerAnchor);
            
             for (var i = 0; i < oMarkers.length; i++)
                {
                 if (oMarkers[i].obj.MarkerTypeId.Name == markerTypeName)   
                    {
                     bMarkerExists = true;   
                     
    				 break;
                    }
                }   
            
             if (!bMarkerExists)
                {
                if (foPgf.Name == target1) 
                    {
                    
    				sText = GetTextFromPgf(foPgf);
                    createMarker(foDoc, foPgf,markerType,"newlink Filename:" + sText);
    		alert (sText);        
            }
                
                if (foPgf.Name == target2) 
                    {
                    sText = GetTextFromPgf(foPgf);
                    createMarker(foDoc, foPgf,markerType,sText);
                    }
                }
           
            foPgf = foPgf.NextPgfInFlow;
     //alert ("marker already added to " + GetTextFromPgf(foPgf));       
    	   }
    }
    
    function GetTextFromPgf(foPgf)
    {
        var oTextItems = foPgf.GetText (Constants.FTI_String);
        
        var sText = "";
        
        for (var i = 0; i <= oTextItems.length -1; i++)
            {
            sText = sText + oTextItems[i].sdata;
            }
    
        sText = sText.replace(/ /g,"_");     // you've asked for in PM
        return sText;
    }
    
    
    function createMarker(foDoc, foPgf,foMarkerType,fsText) 
    {
        var tLoc, oMarker;
        tLoc = new TextLoc(foPgf, 0);
        
        oMarker = foDoc.NewAnchoredObject(Constants.FO_Marker, tLoc);
        oMarker.MarkerTypeId = foMarkerType;
        oMarker.MarkerText = fsText;
    }