好吧,我知道这已经被讨论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);
你让我的一天!非常感谢!我必须添加的唯一一件事是删除catch(_){}行。它仍然因此抛出错误。如果这个工作正确完成,不知道。 – 2014-10-09 13:11:58
不客气!没有看到实际的文件,我不能评论为什么有时它会抛出一个错误;但添加'try..catch'不会造成伤害,并且您可以始终“手动”搜索并插入可能错过的笔记。如果这[回答你的问题](http://stackoverflow.com/help/someone-answers),请考虑通过点击左上角的复选标记正式“接受”它。 – usr2564301 2014-10-09 14:57:50
谢谢,刚刚弄清楚如何使用它。我非常喜欢Ruby,这启发了我,也许我可以学习更多关于JavaScript的知识:) – 2014-10-10 05:44:43