Need help applying markers through document
I am working on a script to search through a document and add gotolink markers to specific text.
For example, in this text, "Lorem ipsum dolor sit amet, consectetur adipiscing F1, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim F2 minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip T1 ea commodo consequat," F1, F2 and T1 have the character tag "Link" and need the gotolink marker applied. The word "quis" is also a link, but should be skipped as it is not what we are looking for.
Thanks to script I've found and help I've received here, I've got a good start, but now I'm stuck. The script loops through and finds all the text with the character tag "link." However, it only read the text and applies the gotolink marker to the first instance. In the example above, If there are three instances of text with the "link" character tag, it will apply "gotolink f1" three times to the first instance.
This is what I want the script to do.
1. Loop through text and find all tagged "Link"
2. Go to the first instance. If there's already a gotolink marker, skip and go to the next. I'm not sure where to do this.
3. If there is not a gotolink marker, select the text range.
4. Read the text.
5. If the text is the right kind (regex), create a marker.
6. Go to the next one. I can't get it to read/apply marker to subsequent links.
Code is below. I am using Framemaker 19.
Thank you for your help!
Julie
-------------
var doc = app.ActiveDoc;
var fmtName = "Link";
var pgf = doc.FirstPgfInDoc;
var markerList = [];
var tloc = new TextLoc (pgf, 0);
var locTextRange = new TextRange (tloc, tloc);
doc.TextSelection = locTextRange;
if (!doc.ObjectValid ()) {
alert ("There is no active document.");
}
else { // gather all markers-locations(objects) and store them in an array (MarkerList)
var findParams = getFindParams ();
var foundTextRange = doc.Find(tloc, findParams);
while (foundTextRange.beg.obj.ObjectValid())
{
markerList.push(foundTextRange);
tloc = foundTextRange.end;
foundTextRange = doc.Find(tloc, findParams);
}
var foundMarker = [];
for (var i = 0; i < markerList.length; i++)
{
var markerTI = doc.GetTextForRange (markerList, Constants.FP_CharTag);
doc.TextSelection = markerList;
alert("found link") //All the links are being found.
getFigOrTableNum (pgf); //Somewhere in here I need this function to apply to each instance of the text selection found. Right now, it's only reading and applying to the first found "link"
doc.ScrollToText (markerList);
for (var x = 0; x < markerTI.length; x++)
{
var textItem = markerTI
foundMarker.push(textItem.obj);//store the marker objects
}
}
}
function getFindParams () { //finding text that has the character tag "Link"
var findParams = new PropVals();
var propVal = new PropVal();
propVal.propIdent.num = Constants.FS_FindCharTag;
propVal.propVal.valType = Constants.FT_String;
propVal.propVal.sval = fmtName;
findParams.push(propVal);
propVal = new PropVal();
propVal.propIdent.num = Constants.FS_FindWrap;
propVal.propVal.valType = Constants.FT_Integer;
propVal.propVal.ival = false;
findParams.push(propVal);
return findParams;
}
function getTextRange () {
var textRange = new TextRange();
textRange.beg.obj = textRange.end.obj = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
textRange.beg.offset = textRange.end.offset = 0;
doc.TextSelection = textRange;
textRange = doc.Find(textRange.beg, findParams);
return textRange;
}
function getMarker (pgf, markerType, text) { //See if a marker exists in a paragraph. I don't have this function called yet... couldn't get it to work, but I want to use this to see if a "link" already has a marker or not. If not, create the marker. If it does, go on to the next link.
var markers = [], marker, textList, i;
textList = pgf.GetText (Constants.FTI_MarkerAnchor);
for (i = 0; i < textList.len; i += 1) {
marker = textList.obj;
if (marker.MarkerTypeId.Name === markerType) {
if (marker.MarkerText.indexOf (text) > -1) {
return marker;
}
}
}
}
function getFigOrTableNum (pgf) {
var doc = app.ActiveDoc;
var findParams = getFindParams ();
var text = "", textItems, i;
var textRange = getTextRange ();
// Get a list of the strings in the text object or text range.
if (textRange.constructor.name !== "TextRange") {
textItems = textRange.GetText(Constants.FTI_String);
alert(textItems);
} else {
textItems = doc.GetTextForRange(textRange, Constants.FTI_String);
}
// Concatenate the strings.
for (i = 0; i < textItems.length; i += 1) {
text += (textItems.sdata);
}
alert ("This link text is "+ text); // it is getting and reading the text, but only from the first one
if (text.match('(AF|AT|[FT])([0-9]+)') != null ) {
createMarker (text);
alert("marker created");
}
else {
alert("this link is not a figure or table");
}
}
function createMarker (text) { // creates the marker
var doc = app.ActiveDoc;
var textRange = getTextRange ();
var marker = doc.NewAnchoredObject(Constants.FO_Marker, (textRange.beg, textRange.end));
var markerType = doc.GetNamedObject(Constants.FO_MarkerType, "Hypertext");
marker.MarkerTypeId = markerType;
marker.MarkerText = "gotolink " + text.toLowerCase ();
return 1;
}
