InDesign脚本:脚注失去了风格

InDesign script: footnotes loosing their style

本文关键字:风格 失去 脚注 脚本 InDesign      更新时间:2023-09-26

好的,我知道这里已经讨论过了,但没有提供明确的答案。我经常需要将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);

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

一个基本的解决方案是使用

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