0

I'm working with footnotes in InDesign 5 . I managed to convert my text to footnotes, but my problem is that they lose the style in the process.

This is the script I'm using :

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;

var fnPatterns = ["@FOOTNOTES_BEGIN@([\\s\\S]*?)@FOOTNOTES_END@", "@footnotes_begin@([\\s\\S]*?)@footnotes_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, fnText, rg = new RegExp(fnPattern), ip, fnParent, fn, count;

    while( fnFind=fnFinds.pop() ){
        fnText = fnFind.contents.match(rg)[1];


        fnParent = fnFind.parent.getElements()[0];
        ip = fnFind.insertionPoints[0].index;
        try {
            fnFind.remove();
            fn = fnParent.footnotes.add(LocationOptions.BEFORE, fnParent.insertionPoints[ip]);
            fn.texts[0].insertionPoints[-1].contents = fnText;
            ++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);

The footnotes are added correctly, they just lose their style. Before using the script, text is shown perfect, but after the script, the footnote styles are gone.

This is an example of the xml input :

....text@FOOTNOTES_BEGIN@<italic>Text</italic> More text@FOOTNOTES_END@

I'm a newbie with InDesign scripting... I'd been looking for an answer, trying lots of stuffs, but somehow I just can't do it. :S Any help? Thanks :)

Nico Gallegos
  • 321
  • 8
  • 20
  • Is your middle name "Martin"? This question is *strikingly* similar to http://stackoverflow.com/questions/20235029/indesign-script-for-footnotes-losing-text-styles – Jongware Nov 28 '13 at 22:16

1 Answers1

3

Here is your problem:

fn.texts[0].insertionPoints[-1].contents = fnText;

contents gets you the plain text only: a String or SpecialCharacters enumerator, where "String" is to be taken literally as a Javascript string.

Use the move method instead (and you have to rewrite your match line as well, since it also works on, and returns, plain Javascript strings). move moves native InDesign text around, with all formatting included.

Jongware
  • 22,200
  • 8
  • 54
  • 100