Javascript InDesign脚本:失去风格的脚注
好的,我知道这已经讨论过了,但是没有给出明确的答案。我经常需要将XML文件导入InDesign,包括许多脚注。当然,在这种情况下,InD不能自动使用标记。这个脚本很好用,除了所有的脚注都有自己的风格。我知道这可能是因为第27行和第35行的Javascript InDesign脚本:失去风格的脚注,javascript,adobe-indesign,extendscript,Javascript,Adobe Indesign,Extendscript,好的,我知道这已经讨论过了,但是没有给出明确的答案。我经常需要将XML文件导入InDesign,包括许多脚注。当然,在这种情况下,InD不能自动使用标记。这个脚本很好用,除了所有的脚注都有自己的风格。我知道这可能是因为第27行和第35行的内容。需要使用移动。不幸的是,我不擅长JavaScript,也不知道如何正确地实现它 Application.prototype.main = function(){ if ( this.documents.length <= 0 ) return; va
内容。需要使用移动
。不幸的是,我不擅长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);
Application.prototype.main=函数(){
如果(this.documents.length问题与您链接到的问题完全相同:您正在使用Javascript处理纯字符串翻译,而不是本机InDesign文本对象本身。请在找到的列表的text
属性上使用move
和duplicate
方法
一个基本的解决方案是使用
fn = fnFind.footnotes.add(LocationOptions.AFTER, fnFind.insertionPoints[-1]);
fnFind.texts[0].move (LocationOptions.AT_END, fn.texts[0]);
但是这也会复制开始标记和结束标记。删除它们需要更多的时间;我根据您的GREP模式在原始脚本中进行了以下调整,但是构建一个明确的前缀
/后缀
对列表可能更安全,因为您还可以使用它们来构建GREP搜索
接下来的问题是,如果您复制(replicate
,在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;
}
你让我高兴极了!非常感谢!我唯一要补充的一件事就是去掉捕获物({}行。它仍然是错误的,因为这是正确的。如果没有看到实际的文档,我不知道为什么有时会出错。但是添加一个<代码>尝试.. catch /代码>不会伤害,并且你总是可以“手动”搜索并插入它可能遗漏的注释。通过点击左上角的复选标记,冰冷地“接受”它。谢谢,我刚刚学会了如何使用它。我非常喜欢Ruby,这启发了我,也许我还可以学习更多关于JavaScript的知识:)