Skip to main content
Known Participant
September 27, 2024
Answered

Move selected text to footnotes via script

  • September 27, 2024
  • 2 replies
  • 427 views

Hello,

I have a document, in which footnote text is typed after the refrence text in curly braces like so:


Lorem ipsum dolor sit amet {consectetur adipiscing elit}. Etiam condimentum purus nec lobortis hendrerit. Praesent vitae velit dictum, congue sapien et, vehicula velit. Aliquam a sapien {turpis}. Ut placerat nulla velit, sit amet dictum velit lobortis sed. Suspendisse efficitur finibus massa ut sagittis. Morbi ac mauris nunc. Vestibulum blandit eros ut fermentum dignissim. Aliquam viverra mattis orci. Aenean vehicula sollicitudin neque, quis feugiat odio sagittis in. Etiam tincidunt {sem nec orci ultricies semper}. Etiam consequat nulla non metus euismod, vel elementum turpis elementum.

 

I want to extract the text inside curly braces and move the text to footnotes.

 

Please suggest a solution.

Thanks and regards.

This topic has been closed for replies.
Correct answer Manan Joshi

Try the following

 

var doc = app.activeDocument;
app.findGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "\\{([^\\}]+)\\}";
var foundTexts = doc.findGrep(true);
for (var i = 0; i < foundTexts.length; i++) {
    var foundText = foundTexts[i];
    var textInsideBraces = foundText.contents;
    var fn = foundText.insertionPoints[0].footnotes.add()
    fn.insertionPoints[-1].contents = textInsideBraces.substring(1,textInsideBraces.length - 1)
    foundText.contents = "";
}
app.findGrepPreferences = NothingEnum.nothing;

 

-Manan

2 replies

Manan JoshiCommunity ExpertCorrect answer
Community Expert
September 27, 2024

Try the following

 

var doc = app.activeDocument;
app.findGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "\\{([^\\}]+)\\}";
var foundTexts = doc.findGrep(true);
for (var i = 0; i < foundTexts.length; i++) {
    var foundText = foundTexts[i];
    var textInsideBraces = foundText.contents;
    var fn = foundText.insertionPoints[0].footnotes.add()
    fn.insertionPoints[-1].contents = textInsideBraces.substring(1,textInsideBraces.length - 1)
    foundText.contents = "";
}
app.findGrepPreferences = NothingEnum.nothing;

 

-Manan

-Manan
Moiz5FB2Author
Known Participant
September 27, 2024

Many many thanks for the quick solution.

works like a charm.

 

Thanks.

m1b
Community Expert
Community Expert
September 27, 2024

Hi @Moiz5FB2 try this script. It's pretty basic, but might be good enough.

- Mark

/**
 * Make Footnotes From Text.js
 * 
 * Usage:
 *   1. Select some text.
 *   2. Run script.
 * 
 * @author m1b
 * @version 2024-09-27
 * @discussion https://community.adobe.com/t5/indesign-discussions/move-selected-text-to-footnotes-via-script/m-p/14883910
 */
function main() {

    var doc = app.activeDocument,
        text = doc.selection[0];

    // clear find/change preferences
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;

    // find the braces text
    app.findGrepPreferences.findWhat = '\\s*\\{([^\\}]+)\\}';
    var found = text.findGrep();

    doc.footnoteOptions.properties = {
        showPrefixSuffix: FootnotePrefixSuffix.PREFIX_SUFFIX_REFERENCE,
        prefix: '',
        suffix: '',
        separatorText: '\t',
        markerPositioning: FootnoteMarkerPositioning.SUPERSCRIPT_MARKER,
    };

    var removeBraces = /(^\s*\{|\s*\}$)/g;

    for (var i = found.length - 1, text, contents, index, footnote; i >= 0; i--) {

        text = found[i];
        contents = text.contents.replace(removeBraces, '');
        index = text.insertionPoints[0].index;

        // remove the braces text
        text.remove();

        // make a new footnote
        footnote = text.footnotes.add(
            LocationOptions.BEFORE,
            text.parentStory.insertionPoints[index]
        );

        // add the text we got from the braces
        footnote.texts[0].insertionPoints[-1].contents = contents;
    }

}
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Make Footnotes');