-
-
Save welblaud/c21a96f2f23db58b4011726cf21addb8 to your computer and use it in GitHub Desktop.
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); |
@stephanedebove I'd say the most direct attitude could be a bit preprocessing... I guess it would be possible to substitute the LaTeX native footnotes into the provided pattern this way: search: \\footnote\{(.*?)\}
/ replace: @footnote_beg@$1@footnote_end@
. After this operation, the script could work as expected. If not, I am sorry, it's been quite a long time since I worked with it for the last time! (Of course, the pattern for searching assumes there are no curly braces somewhere inside the notes.)
Thanks for the fast answer! It does work indeed and I don’t mind preprocessing! Although you are right, I sometimes have braces inside the notes, and I’m really bad at regex, any idea how to modify the search pattern so that the right brace is identified as the last one?
Would you mind pasting here a couple of such notes? I could review their shape and figure out a pattern, if possible.
In fact, don’t worry: I only have a couple of footnotes with nested braces so I will just deal with them manually.
I have another more important problem: it seems like your script is cutting the last two characters of each footnote. I was able to get one character back by removing the "-1" from fnPrefix.length,fnFind.texts[0].characters.length-fnSuffix.length-1
but I can’t find where the second character could be removed in your script? (also I don’t know if this is the expected behavior of your script, or if I’m missing something here).
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.
Hello,
thanks for the code.
Can you elaborate on how to modify fnPatterns to match my own notes?
I’m working on a LaTex script, so all my footnotes are included in \footnote{} tags inside the main text. How do I transform them into footnotes into indesign?