2012-03-07 39 views
2
  1. 第一次发布
  2. 第一次用JavaScript编写,虽然我在其他语言的经验。

我在Adobe公司的InDesign CS5.5工作。我在ID簿中有多个文件,每个文件包含不同数量的“章节”。本书包含一个索引文件,其中的主题标题以简写形式引用章节(例如,“第125章”变为“ch 125 no 3” - 注意“no x”部分不相关)。我的脚本的目标是创建文档间链接,当ID簿导出为PDF时,这些链接将增加显着的功能。用户将能够从索引跳转到章节,反之亦然。我认为脚本和我正在处理的问题会对其他人有用,但还没有找到任何帖子来解决我的问题。排版的JavaScript图书创建文本和内部文档超链接 - 速度异常慢

所有裁判(如“CH 125 1号”)在索引中特定的章节(“第125章”)获得的超链接到该章节的头部的位置。这部分脚本运行良好,运行速度很快。

另一半将在每个章节文字的末尾插入相应的主题标题,让这些段落在索引链接到相应的主题头部。 (换句话说,他们是交叉引用,但不是在ID方面真正的X-裁判,因为我想更好地控制他们和我的话题读书让我避开真正的X-裁判的)。这是脚本的一部分那让我把我的头撞在墙上。它几个小时运行几个小时,没有完成200章的书。请注意,出于测试目的,我只是在每章下的所需位置插入一段文本,而不是所有主题题头和链接。我知道从更小的文本集和从我的调试打印到控制台,脚本正在工作,而不是陷入无限循环。尽管如此,它的运行时间太长,如果我打断它,InDesign没有反应,我必须杀死它,所以甚至不能检查部分结果。

基于搜索/阅读论坛:我有禁用预检;禁用自动更新书页号码;将实时预览设置更改为延迟。我仍然怀疑缓慢可能与InDesign开销有关,但我不知道还有什么可以尝试的。

我很尴尬这个JS代码的风格有多糟糕,但目前我只需要它的工作,然后我可以改进它。

var myBookFilePath = File.openDialog("Choose an InDesign Book File", "Indb files: *.indb"); 
var myOpenBook = app.open(myBookFilePath); 
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract; 

// Open up every file in the currently active Book 
app.open(app.activeBook.bookContents.everyItem().fullName) 

// TODO: add error handling/user interaction here -- to pick which is Index file 
var strIndexFilename = "Index.indd"; 
var objChapHeadsWeb = {}; 
var myDoc = app.documents.item(strIndexFilename); 

$.writeln("\n\n~~~ " + myDoc.name + " ~~~"); 

// REMOVED CODE - check for existing hyperlinks, hyperlink sources/destinations 
// loop to delete any pre-existing hyperlinks & associated objects 
// works w/o any problems 

// Ugly GREP to find the Main heading text (all caps entry and nothing beyond) in the index file 
app.findGrepPreferences = NothingEnum.nothing; 
app.changeGrepPreferences = NothingEnum.nothing; 

/// GREP: ^[\u\d \:\;\?\-\'\"\$\%\&\!\@\*\#\,\.\(\)]+[\u\d](?=\.|,) 
app.findGrepPreferences.findWhat = "^[\\u\\d \\:\\;\\?\\-\\'\\\"\\$\\%\\&\\!\\@\\*\\#\\,\\.\\(\\)]+[\\u\\d](?=\\.|,)"; 
app.findGrepPreferences.appliedParagraphStyle = "Main"; 

var myFound = []; 
myFound = myDoc.findGrep(); 
$.writeln("Found " + myFound.length + " Main headings."); 

for (var i = 0; i < myFound.length; i++) { 
    myDoc.hyperlinkTextDestinations.add(myFound[i], { name: myFound[i].contents }); 
} 

$.writeln("There are now " + myDoc.hyperlinkTextDestinations.count() + " destinations."); 


myFound.length = 0; 

for (var j = app.documents.count()-1; j >= 0; j--) { 
    app.findGrepPreferences = NothingEnum.nothing; 
    app.changeGrepPreferences = NothingEnum.nothing; 

    // set the variable to the document we are working with 
    myDoc = null; 
    myDoc = app.documents[j]; 
    myFound.length = 0; 

    if (myDoc.name === strIndexFilename) { 
     continue;  // we don't want to look for chapter heads in the Index file, so skip it 
    } 

    $.writeln("\n\n~~~ " + myDoc.name + " ~~~"); 

// REMOVED CODE - check for existing hyperlinks, hyperlink sources/destinations 
// loop to delete any pre-existing hyperlinks & associated objects 
// works w/o any problems 

    // Clear GREP prefs 
    app.findGrepPreferences = NothingEnum.nothing; 
    app.changeGrepPreferences = NothingEnum.nothing; 

    app.findGrepPreferences.findWhat = "^CHAPTER \\d+"; 
    app.findGrepPreferences.appliedParagraphStyle = "chapter"; 

    myFound = myDoc.findGrep(); 
    var strTemp = ""; 
    $.writeln("Found " + myFound.length + " chapter headings."); 

    for (var m = 0; m < myFound.length; m++) { 
     strTemp = myFound[m].contents; 

     objChapHeadsWeb[strTemp] = {}; 
     objChapHeadsWeb[strTemp].withinDocName = myDoc.name; 
     objChapHeadsWeb[strTemp].hltdChHead = 
      myDoc.hyperlinkTextDestinations.add(myFound[m], {name:strTemp}); 
     objChapHeadsWeb[strTemp].a_strIxMains = []; 
     objChapHeadsWeb[strTemp].a_hltdIxMains = []; 
     objChapHeadsWeb[strTemp].nextKeyName = ""; 

     objChapHeadsWeb[strTemp].nextKeyName = 
      ((m < myFound.length-1) ? myFound[m+1].contents : String("")); 
    } 

    $.writeln("There are now " + myDoc.hyperlinkTextDestinations.count() + " destinations."); 
} 


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
// 
// Find the "ch" (chapter) references in the index file, link them 
//  back to the corresponding text anchors for the chapter heads 
//  in the text. 
// 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
myDoc = app.documents.item(strIndexFilename); // work with the Index file 

app.findGrepPreferences = NothingEnum.nothing; 
app.changeGrepPreferences = NothingEnum.nothing; 

// GREP to find the "ch" (chapter) references in the index file 
// like ch 151 no 1 OR ch 12 no 3 
app.findGrepPreferences.findWhat = "(ch\\s+\\d+\\s+no\\s+\\d+)"; 

var strExpandedChap = ""; 
var strWorkingMainHd = ""; 
var arrFoundChapRefs = []; 
var myHyperlinkSource; 
var myHyperlinkDest; 

for (var x = 0; x < myDoc.hyperlinkTextDestinations.count(); x++) { 
    strWorkingMainHd = ""; 
    arrFoundChapRefs.length = 0; 

    // the special case, where we are working with the ultimate hyperlinkTextDestination obj 
    if (x === myDoc.hyperlinkTextDestinations.count()-1) { 
     // This is selecting text from the start of one MAIN heading... 
     myDoc.hyperlinkTextDestinations[x].destinationText.select(); 
     // This next line will extend the selection to the end of the story, 
     //  which should also be the end of the document 
     myDoc.selection[0].parentStory.insertionPoints[-1].select(SelectionOptions.ADD_TO); 
    } 
    // the regular case... 
    else { 
     // This is selecting text from the start of one MAIN heading... 
     myDoc.hyperlinkTextDestinations[x].destinationText.select(); 
     // ... to the start of the next MAIN heading 
     myDoc.hyperlinkTextDestinations[x+1].destinationText.select(SelectionOptions.ADD_TO); 
    } 

    strWorkingMainHd = myDoc.hyperlinkTextDestinations[x].name; 
    //arrFoundChapRefs = myDoc.selection[0].match(/(ch\s+)(\d+)(\s+no\s+\d+)/g); //NOTE: global flag 

    arrFoundChapRefs = myDoc.selection[0].findGrep(); 

    for(y = 0; y < arrFoundChapRefs.length; y++) { 
     myHyperlinkSource = null; 
     myHyperlinkDest = null; 
     strExpandedChap = ""; 

     strExpandedChap = arrFoundChapRefs[y].contents.replace(/ch\s+/, "CHAPTER "); 
     strExpandedChap = strExpandedChap.replace(/\s+no\s+\d+/, ""); 

     // if we found the chapter head corresponding to our chapter ref in the index 
     //  then it is time to create a link 
     if (strExpandedChap in objChapHeadsWeb) { 
      objChapHeadsWeb[strExpandedChap].a_strIxMains.push(strWorkingMainHd); 
      objChapHeadsWeb[strExpandedChap].a_hltdIxMains.push(myDoc.hyperlinkTextDestinations[x]); 

      myHyperlinkSource = myDoc.hyperlinkTextSources.add(arrFoundChapRefs[y]); 
      myHyperlinkDest = objChapHeadsWeb[strExpandedChap].hltdChHead; 

      myDoc.hyperlinks.add(myHyperlinkSource, myHyperlinkDest); 
     } else { 
      $.writeln("Couldn't find chapter head " + strExpandedChap); 
     } 
    } 
} 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
// NOW TIME FOR THE HARD PART... 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
myDoc = null; 
var strWorkingMainHd = ""; 
var nextKey = ""; 
var myParentStory = null; 
var myCharIndex = 0; 
var myCompareChar = null; 
var myLeftmostBound = 0; 
var myCurrentPara = null; 

for (var key in objChapHeadsWeb) { 
    myDoc = app.documents.item(objChapHeadsWeb[key].withinDocName); 
    myCompareChar = null; //recent addition 
    $.writeln("Working on " + key + "."); //debugging 

    nextKey = objChapHeadsWeb[key].nextKeyName; 

    objChapHeadsWeb[key].hltdChHead.destinationText.select(); 
    myLeftmostBound = myDoc.selection[0].index; 
    myParentStory = myDoc.selection[0].parentStory; 

    if((nextKey === "") || (myDoc.name !== objChapHeadsWeb[nextKey].withinDocName)) 
    { 
     //// Need to find end of story instead of beginning of next chapter 
     //myDoc.selection[0].parentStory.insertionPoints[-1].select(SelectionOptions.ADD_TO); 
     myParentStory.insertionPoints[-1].select(); 
     //myCharIndex = myDoc.selection[0].index;   /recently commented out 

     myCharIndex = myDoc.selection[0].index - 1;  //testing new version 
     myCompareChar = myParentStory.characters.item(myCharIndex);  //recenttly added/relocated from below 
    } else { 
     ///// 
     //objChapHeadsWeb[nextKey].hltdChHead.destinationText.select(SelectionOptions.ADD_TO); 
     objChapHeadsWeb[nextKey].hltdChHead.destinationText.select(); 

     //myParentStory.characters.item(myDoc.selection[0].index -1).select(); 

     myParentStory.characters.item(myDoc.selection[0].index -2).select(); //temp test ***** 

     myCharIndex = myDoc.selection[0].index; 
     myCompareChar = myParentStory.characters.item(myCharIndex); 

     if (myCompareChar.contents === "\uFEFF") { 
      $.writeln("Message from inside the \\uFEFF check.");  //debugging 

      myParentStory.characters.item(myDoc.selection[0].index -1).select(); 

      myCharIndex = myDoc.selection[0].index; 
      myCompareChar = myParentStory.characters.item(myCharIndex); 
     } 

     if((myCompareChar.contents !== SpecialCharacters.PAGE_BREAK) && 
      (myCompareChar.contents !== SpecialCharacters.ODD_PAGE_BREAK) && 
      (myCompareChar.contents !== SpecialCharacters.EVEN_PAGE_BREAK) && 
      (myCompareChar.contents !== SpecialCharacters.COLUMN_BREAK) && 
      (myCompareChar.contents !== SpecialCharacters.FRAME_BREAK)) 
     { 
      $.writeln("Possible error finding correct insertion point for " + objChapHeadsWeb[key].hltdChHead.name + "."); 
     } 
    } 

    if(myCharIndex <= myLeftmostBound) { // this shouldn't ever happen 
     alert("Critical error finding IX Marker insertion point for " + objChapHeadsWeb[key].hltdChHead.name + "."); 
    } 

    if(myCompareChar.contents !== "\r") { 
     myDoc.selection[0].insertionPoints[-1].contents = "\r"; 
    } 

    myDoc.selection[0].insertionPoints[-1].contents = "TESTING text insertion for: " + objChapHeadsWeb[key].hltdChHead.name + "\r"; 
    myDoc.selection[0].insertionPoints.previousItem(myDoc.selection[0].insertionPoints[-1]).select(); 

//myDoc.selection[0].insertionPoints[-1].contents = "<Now I'm here!>"; 

    myCurrentPara = myDoc.selection[0].paragraphs[0]; 

    myCurrentPara.appliedParagraphStyle = myDoc.paragraphStyles.item("IX Marker"); 

    // TODO: 
    //  need error handling for when style doesn't already exist in the document 
} // end big for loop 


//TODO: add error handling support to carry on if user cancels 
//close each open file; user should be prompted to save changed files by default 

app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll; 
app.documents.everyItem().close(); 

// Cleanup 
app.findGrepPreferences = NothingEnum.nothing; 
app.changeGrepPreferences = NothingEnum.nothing; 

回答

0

尝试打开交叉引用的所有文件都链接到它们。

0

我可以提出几个改进,也许可以加快事情有点。 首先,您在这里拥有大量的全局变量,您可以将精力集中在使用函数的范围更小的范围内。具有许多全局变量在性能方面成本很高。

一旦这样说,我不会立即打开本书的每一个文档,而是一个接一个地处理它们。请注意,grep调用非常昂贵,所以您可以尝试查看自己的模式。

另一个是$ .writeln命令的广泛使用。特别是在循环中避免它。倾向于设置一个易于设置的报告库。

最后我尝试以“更好”的方式重写你的代码,但很难用清晰的理解你的需求来构造整个脚本,并且不需要处理任何文件。但是我希望下面的代码片段能够帮助你开始重写代码并显着改善时间。

var debug = true; 
 

 
var log = function(msg) { 
 
\t 
 
\t var l = File (Folder.desktop+"/log.txt"); 
 
\t 
 
\t if (!debug) return; 
 
\t 
 
\t l.open('a'); 
 
\t l.write(msg); 
 
\t l.close(); 
 
}; 
 

 
var main = function() { 
 
\t 
 
\t var bookFile, uil = app.scriptPreferences.userIntercationLevel; 
 
\t 
 
\t log("The party has started"); 
 
\t 
 
\t bookFile = File.openDialog("Choose an InDesign Book File", "Indb files: *.indb"); 
 
\t 
 
\t if (!bookFile) return; 
 
\t app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT; 
 
\t 
 
\t try { 
 
\t \t processBookFile (bookFile); 
 
\t } 
 
\t catch(err) { 
 
\t \t alert(err.line+"///"+err.message); 
 
\t } 
 

 
\t app.scriptPreferences.userInteractionLevel = uil; 
 
}; 
 

 
function processBookFile (bookFile) { 
 
\t var book = app.open (bookFile), 
 
\t bks = book.bookContents, 
 
\t n = bks.length; 
 
\t 
 
\t while (n--) { 
 
\t \t File(bks[n].name)!="Index.indd" && processBookContent (bks[n]); 
 
\t } 
 
} 
 

 
function processBookContent (bookContent) { 
 
\t var bcf = bookContent.fullName, 
 
\t doc = app.open (bcf, debug); 
 
\t 
 
\t //DEAL WITH HEADINGS 
 
\t processHeadings (doc); 
 
\t 
 
\t //DEAL WITH CHAPTERS 
 
\t processHeadings (doc); 
 
\t 
 
\t //add hyperlinks 
 
\t addHyperlinks(doc); 
 
} 
 

 

 

 
function processHeadings (doc){ 
 
\t var props = { 
 
\t \t findWhat : "^[\\u\\d \\:\\;\\?\\-\\'\\\"\\$\\%\\&\\!\\@\\*\\#\\,\\.\\(\\)]+[\\u\\d](?=\\.|,)", 
 
\t \t appliedParagraphStyle : "Main" 
 
\t }, 
 
\t found = findGrep(doc, props), 
 
\t n = found.length; 
 
\t 
 
\t while (n--) { 
 
\t \t doc.hyperlinkTextDestinations.add(doc, { name: found[i].contents }); 
 
\t } 
 
}; 
 

 
function processChapters (doc) { 
 
\t var props = { 
 
\t \t findWhat : "^CHAPTER \\d+", 
 
\t \t appliedParagraphStyle : "chapter" 
 
\t }, 
 
\t found = findGrep(doc, props), 
 
\t n = found.length; 
 
\t 
 
\t while (n--) { 
 
\t \t doc.hyperlinkTextDestinations.add(found[n], found[n].contents); 
 
\t } 
 
} 
 

 
function findGrep(doc, props){ 
 
\t app.findGrepPreferences = app.changeGrepPreferences = null; \t 
 
\t app.findGrepPreferences.properties = props; 
 
\t return doc.findGrep(); 
 
} 
 

 
function addHyperlinks (doc){ 
 
\t //a logic of yours 
 
}; 
 

 

 
main();