I am trying to apply a gotolink marker in my text to a word that has a specific character tag. A couple of months ago, frameexpert helped me with some code that applies the newlink marker to text in a specific paragraph format, and I thought I could just modify it (see below). But I'm obviously missing something because it just won't work. The marker needs to be added to the specific words, so that they are a link, rather than to the beginning of the paragraph, so I know I need to do something about the offset location. But first, I just need it to work. Any help would be much appreciated! Thanks in advance.
#target framemaker
main ();
function main () {
var doc = app.ActiveDoc;
if (doc.ObjectValid () === 1) {
processDoc (doc);
}
else {
alert ("There is no active document.");
}
}
function processDoc (doc) {
var findLink = doc.GetNamedCharFmt ("Link"); // Match Link character format.
var pgf = doc.FirstPgfInDoc;
while (pgf.ObjectValid () === 1 ) {
if (findLink.ObjectValid (doc) === true) {
processGoToLink (pgf, doc);
}
pgf = pgf.NextPgfInDoc;
}
}
function processGoToLink (pgf, doc) {
var num = getFigOrTableNum (pgf); // Calls the function to get the figure or table numbers
if (num) {
var marker = getMarker (doc, "Hypertext", "gotolink"); // See if a gotolink marker already exists in the paragraph.
if (marker) {
// Update the existing marker's text.
marker.MarkerText = "gotolink " + num;
}
else { // No existing marker; create a new one.
createMarker (doc, 0, "Hypertext", "gotolink " + num); // I don't want the marker at the beginning of the paragraph. The combination of letter plus number (F1) should be the link.
}
}
else {
Console ("Couldn't find number: " + pgf.Name);
}
}
function getMarker (pgf, markerType, text) { // See if a marker exists in a paragraph
var markers = [], marker, textList, i;
textList = pgf.GetText (Constants.FTI_MarkerAnchor);
for (i = 0; i < textList.len; i += 1) {
marker = textList[i].obj;
if (marker.MarkerTypeId.Name === markerType) {
if (marker.MarkerText.indexOf (text) > -1) {
return marker;
}
}
}
}
// Create a new marker
function createMarker(doc, pgf, offset, type, text) {
var tLoc = new TextLoc(pgf, offset);
var marker = doc.NewAnchoredObject(Constants.FO_Marker, tLoc);
var markerType = doc.GetNamedObject(Constants.FO_MarkerType, type);
marker.MarkerTypeId = markerType;
marker.MarkerText = text;
return 1;
}
function getFigOrTableNum (pgf) {
var doc = app.ActiveDoc;
var regex = /((AF|AT|[FT])(\d+))/; // Match F or T or AF or AT plus number in text
var pgfText = getText (pgf, doc); // Calls next function to get the text
if (regex.test (pgfText) === true) {
var match = pgfText.match (regex);
return (match[1].toLowerCase ());
}
}
function getText (textObj, doc) { // Gets the text from the text object.
var text = "", textItems, i;
if (textObj.constructor.name !== "TextRange") { // Get a list of the strings in the text object or text range.
textItems = textObj.GetText(Constants.FTI_String);
} else {
textItems = doc.GetTextForRange(textObj, Constants.FTI_String);
}
// Concatenate the strings.
for (i = 0; i < textItems.len; i += 1) {
text += (textItems[i].sdata);
}
return text; // Return the text
}