InDesign脚本:脚注失去了他们的风格

时间:2014-10-09 08:07:36

标签: javascript adobe-indesign extendscript

好的,我知道这已经讨论过了here,但没有提供明确的答案。我经常需要将XML文件导入InDesign,其中包括许多脚注。当然,在这种情况下,InD无法自动使用标签。除了所有脚注松散其样式外,此脚本运行良好。我知道这可能是因为第27和35行的contents。需要使用move代替。不幸的是,我不擅长JavaScript,也无法弄清楚如何正确实现它。

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 = ["@foot@([\\s\\S]*?)@foot@", "@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);

1 个答案:

答案 0 :(得分:1)

问题与您链接到的问题完全相同:您正在Javascript中操作纯字符串转换,而不是本机InDesign文本对象本身。改为在找到的列表的move属性中使用duplicatetext方法。

基本解决方案是使用

    fn = fnFind.footnotes.add(LocationOptions.AFTER, fnFind.insertionPoints[-1]);
    fnFind.texts[0].move (LocationOptions.AT_END, fn.texts[0]);

但这也会复制开始和结束标记。删除它们需要更多一点;我在您的GREP模式的原始脚本中进行了以下调整,但构建prefix / suffix对的明确列表可能更安全,因为您也可以使用它们来构建GREP搜索。

接下来的问题是,如果您复制duplicate,在InDesign的DOM中)找到的文本,原始的“找到”文本现在会附加脚注!这是因为之前的一行,你将脚注添加到“找到的”文本。因此,您无法使用简单的remove来删除它;再次,您需要操纵text对象,但这次是通过其各自的字符。我的最后一个调整行“选择”fnFind文本,减去最后一个字符(这是新添加的脚注),并删除它。

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];

    // 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;
}
相关问题