Skip to main content
Participating Frequently
January 24, 2022
Answered

Fix Index Markers with regex and Find and Replace script

  • January 24, 2022
  • 1 reply
  • 504 views

Hi Everyone, 

This is my first time creating a script for FM and I'm having some trouble getting this to work. I have some index markers that have had a semicolon replaced with a colon, which is creating havoc in my Index file. I wanted to use an regex expression with a Find and Replace script to correct them. There are a great many of these to find and fix and doing it manually isn't too appealing (though if I have to, I will). My script, which has been stolen/cobbled from other posts here, seems to run (I get the message box showing X number of replacements made) but it doesn't actually perform the replace part (script below). I'm not sure it's performing the Find part either, come to think of it. Any help would be most appreciated!

Thank you in advance,

Megan

 

#target framemaker

var doc, marker, nextMarker, regex, regexR
var markersAdjusted = 0;

// Set a variable for the active document.
doc = app.ActiveDoc;
// Make a regular expression for the text.
regex = /:([A-Z0-9. ])/;
regexR = /;($1)/;

// Process all of the markers in the document.
marker = doc.FirstMarkerInDoc;
while (marker.ObjectValid () === 1) {
    nextMarker = marker.NextMarkerInDoc;
    // Make sure it is an Index marker.
    if (marker.MarkerTypeId.Name === "Index") {
        // Check the text.
        if (regex.test (marker.MarkerText) === true) {
            marker.MarkerText === regexR;
            markersAdjusted++;
           
        }
    }
    marker = nextMarker;
}
alert("Operation complete. " + markersAdjusted + " markers adjusted.");
    This topic has been closed for replies.
    Correct answer frameexpert

    A couple of things: your regexR should be a string, not a regular expressions object:

    regexR = ";$1";

     

    Then inside the regex.test, you need this:

    marker.MarkerText = marker.MarkerText.replace (regex, regexR);

    1 reply

    frameexpert
    Community Expert
    frameexpertCommunity ExpertCorrect answer
    Community Expert
    January 24, 2022

    A couple of things: your regexR should be a string, not a regular expressions object:

    regexR = ";$1";

     

    Then inside the regex.test, you need this:

    marker.MarkerText = marker.MarkerText.replace (regex, regexR);

    Megan5EAEAuthor
    Participating Frequently
    January 24, 2022

    Thank you very much! You've literally saved me days of insanely boring manual work!

    frameexpert
    Community Expert
    Community Expert
    January 24, 2022

    One of the things I do when developing scripts is to work with a single selected item in the document. For example, you can use this code to get the currently selected marker in the active document. The benefit is that you can make sure that your basic code works on the selected marker before expanding it to work on all markers in the document (or book).

     

    var doc, textRange, textItems, marker;
    
    // Set a variable for the active document.
    doc = app.ActiveDoc;
    
    // Set a variable for the selected text range in the document.
    textRange = doc.TextSelection;
    
    // Get a list of markers in the selected text.
    textItems = doc.GetTextForRange(textRange,Constants.FTI_MarkerAnchor);
    // Make sure there is a marker in the selection.
    if (textItems.len) {
        // Get the first marker and display its marker text and marker type.
        marker = textItems[0].obj;
        alert (marker.MarkerText);
        alert (marker.MarkerTypeId.Name);
    }