Indesign Script for footnotes losing text styles

纵然是瞬间 提交于 2019-12-11 16:14:22

问题


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 :)


回答1:


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.



来源:https://stackoverflow.com/questions/20235029/indesign-script-for-footnotes-losing-text-styles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!