2014-10-09 114 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