InDesign脚本:脚注失去了风格
InDesign script: footnotes loosing their style
好的,我知道这里已经讨论过了,但没有提供明确的答案。我经常需要将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
属性使用move
和duplicate
方法。
一个基本的解决方案是使用
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;
}
相关文章:
- 为什么Airbnb风格指南说不鼓励依赖函数名称推断
- CKEditor-我在editor.css中的风格是't
- Javascript游戏输入失去焦点
- jquery日期选择器失去了交互性
- onclick风格的整个李不想要
- JavaScript改变了双方的显示风格
- 如何移除或改进:在Bootstrap Carousel上聚焦风格
- 如何塑造李的风格;s正在使用单击的李的值
- 用于Dynamic Div'的Javascript数学函数;s风格
- 创建一个不断变化的谷歌涂鸦风格的标题
- 突然失去Visual Studio Typescript Intellisense
- jQuery将TD注入适当的列(俄罗斯方块风格)
- 最好的技术自上而下RPG风格的网格滚动
- 在Windows Metro风格的Javascript应用程序中读取文件内容
- jquery可以't隐藏输入名称为“”的表单;风格”;
- 如何使元素在按下按钮时不会失去焦点
- 谷歌地图气球失去风格
- chucknorris的twitter小部件在IE上正在失去风格
- InDesign脚本:脚注失去了风格
- 剑道对象失去了功能和风格