-
-
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); |
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.
Would you mind pasting here a couple of such notes? I could review their shape and figure out a pattern, if possible.