Skip to content

Instantly share code, notes, and snippets.

@welblaud
Last active September 28, 2023 10:05
Show Gist options
  • Save welblaud/c21a96f2f23db58b4011726cf21addb8 to your computer and use it in GitHub Desktop.
Save welblaud/c21a96f2f23db58b4011726cf21addb8 to your computer and use it in GitHub Desktop.
A script for turning in-text notes into footnotes in InDesign
Application.prototype.main = function(){
if (this.documents.length <= 0) return;
var tg = this.selection[0] || this.activeDocument;
if ('appliedFont' in tg) tg = tg.parent;
if (tg.constructor == TextFrame) { tg = tg.parentStory; }
if (!('findGrep' in tg)) return;
/* Here it is neecessary to choose a pattern for wrapping the note, we are not parsing
here, hence the rough technique! */
var fnPatterns = ["@foot_beg@([\\s\\S]*?)@foot_end@", "@footnote_begin@([\\s\\S]*?)@footnote_end@"];
var count = 0;
for(patterCounter = 0; patterCounter < fnPatterns.length; patterCounter++){
fnPattern = fnPatterns[patterCounter];
var fnFinds = (function(){
this.findGrepPreferences = this.changeGrepPreferences = null;
this.findGrepPreferences.findWhat = fnPattern;
var ret = tg.findGrep();
this.findGrepPreferences = this.changeGrepPreferences = null;
return ret;
}).call(this);
var fnFind, fnPrefix,fnSuffix, rg = new RegExp(fnPattern), ip, fnParent, fn, count;
while (fnFind = fnFinds.pop()){
fnPrefix = fnFind.contents.match(/^@[^@]+@/)[0];
fnSuffix = fnFind.contents.match(/@[^@]+@/)[0];
try {
// add footnote
fn = fnFind.footnotes.add(LocationOptions.AFTER, fnFind.insertionPoints[-1]);
// duplicate the text
fnFind.texts[0].characters.itemByRange(fnPrefix.length,fnFind.texts[0].characters.length-fnSuffix.length-1).duplicate(LocationOptions.AT_END, fn.texts[0]);
// remove the original
fnFind.characters.itemByRange(0,fnFind.characters.length-2).remove();
++count;
}
catch(_){}
}
}
alert((count)? (count+" footnote(s) successfully added."): "No footnote added. Make sure you use the relevant pattern.");
}
app.doScript('app.main();', ScriptLanguage.javascript,
undefined, UndoModes.entireScript, app.activeScript.displayName);
@welblaud
Copy link
Author

I'd say the problem might be here: fnFind.characters.itemByRange(0,fnFind.characters.length-2).remove();
I am sorry, I have no chance to test it (I don't have InDesign at the time). This script is rather old and its behavior might be associated with the overall behavior of footnote-related logic in the application. I'd been using it often a couple of years ago without a hassle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment