Highlighted

Function not working

Explorer ,
Jun 10, 2020

Copy link to clipboard

Copied

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;

}

}

 

Views

166

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Function not working

Explorer ,
Jun 10, 2020

Copy link to clipboard

Copied

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;

}

}

 

Views

167

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 10, 2020 0
Adobe Community Professional ,
Jun 10, 2020

Copy link to clipboard

Copied

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.

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 10, 2020 1
sam.m1 LATEST
Explorer ,
Jun 10, 2020

Copy link to clipboard

Copied

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;
}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 10, 2020 0