2012-03-07 2 views
2
  1. 初めて、私は他の言語での経験を持っていますが、JavaScriptで書く
  2. まず時間を掲示します。

私はAdobe InDesign CS5.5で作業しています。私はIDブックに複数のファイルを持っており、それぞれにはさまざまな数の「チャプター」が含まれています。この書籍には、章を略式で参照するトピック見出しのインデックスファイルが含まれています(たとえば、「CHAPTER 125」は「ch 125 no 3」になります - 「no x」部分は無関係です)。私のスクリプトの目的は、IDブックをたとえばPDFにエクスポートする際に重要な機能を追加する文書間リンクを作成することです。ユーザーはインデックスから章にジャンプすることができ、その逆も可能です。私が扱っているスクリプトと問題は、他の人には役に立つと思うが、私の問題にまだ対処するための投稿は見つけていない。IndesignのJavaScriptを - 非常に遅い

特定の章( "CHAPTER 125")のインデックスにあるすべてのref( "ch 125 no 1"のような)は、その章の先頭の場所へのハイパーリンクを取得します。スクリプトのこの部分はすばらしい働きをしており、すぐに実行されます。

残りの半分は、各チャプタテキストの最後に対応するトピック見出しを挿入し、それらの段落をインデックス内の対応するトピックヘッドにリンクさせます。 (言い換えれば、それらは相互参照ですが、真のx-refではないので、私はそれらをよりコントロールしたいと思っていました。そして、トピックに関する私の読書は真のx-refsを明らかにするように指示しました。)これはスクリプトの一部ですそれは私が壁に私の頭を叩いている。それは、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コールは非常にコストがかかるので、パターンを見てみてください。

もう1つは、$ .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();