Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

Fix Index Markers with regex and Find and Replace script

Community Beginner ,
Jan 24, 2022 Jan 24, 2022

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.");
431
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 24, 2022 Jan 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);

Translate
Community Expert ,
Jan 24, 2022 Jan 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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 24, 2022 Jan 24, 2022

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 24, 2022 Jan 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);
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 24, 2022 Jan 24, 2022
LATEST

This is wonderful! Thank you very much for this, I've saved it to my HD for future use. I'm awesome working with VBA scripts but very new to these FM scripts. I'm still learning the syntax and how to debug and I really appreciate the help 🙂

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