2014-10-09 32 views
0

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

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

回答

0

问题是完全相同在您链接到的问题是:你是操纵在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(/^@[^@][email protected]/)[0]; 
    fnSuffix = fnFind.contents.match(/@[^@][email protected]/)[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; 
} 
+0

你让我的一天!非常感谢!我必须添加的唯一一件事是删除catch(_){}行。它仍然因此抛出错误。如果这个工作正确完成,不知道。 – 2014-10-09 13:11:58

+0

不客气!没有看到实际的文件,我不能评论为什么有时它会抛出一个错误;但添加'try..catch'不会造成伤害,并且您可以始终“手动”搜索并插入可能错过的笔记。如果这[回答你的问题](http://stackoverflow.com/help/someone-answers),请考虑通过点击左上角的复选标记正式“接受”它。 – usr2564301 2014-10-09 14:57:50

+0

谢谢,刚刚弄清楚如何使用它。我非常喜欢Ruby,这启发了我,也许我可以学习更多关于JavaScript的知识:) – 2014-10-10 05:44:43