2015-06-03 31 views
1

我正在嘗試將InDesign頁面的當前頁碼置於標籤中。 這是我在用的:獲取InDesign多頁文檔的當前頁碼

myLabel = myDocument.properties.name.replace(/\D+/,'').split(' ')[0].split('_')[0] + '_' 
    + app.activeWindow.activePage.name +'_'+myCounter; 

我遇到的問題是,我從一個多頁的文件運行這一點,而不是使用當前的頁碼,其使用的第一頁號文檔在每個頁面的所有標籤中。

下面是代碼,我認爲我有問題:

function myAddLabel(myDocument, myGraphic, myCounter, myLabelType, myLabelHeight, myLabelOffset, myStyle, mySwatch, myStoriesArray){ 
    var myLabel; 
    var myLink = myGraphic.itemLink; 
    var myPasteFromClipboard = false; 
    //Create the label layer if it does not already exist. 
    var myLabelLayer = myDocument.layers.item("Coded Layout"); 
    try{ 
     myLabelLayer.name; 
    } 
    catch (myError){ 
     myLabelLayer = myDocument.layers.add({name:"Coded Layout"}); 
    } 
    //Label type defines the text that goes in the label. 
    switch(myLabelType){ 
    //File name 
     case 0: 
      myLabel = myDocument.properties.name.replace(/\D+/,'').split(' ')[0].split('_')[0]+'_' + app.activeWindow.activePage.name+'_'+padNumber(myCounter); 
      break; 

現在,我仍然認爲,實際問題是在腳本的這一部分,如果我選擇頁面上的所有項目和按照我希望它工作的方式運行腳本。

function myAddLabels(myLabelType, myLabelHeight, myLabelOffset, myStyle, mySwatch){ 
var myDocument = app.documents.item(0); 
myStoriesArray = new Array(); 

if (app.selection.length == 0) // If nothing is selected apply caption to all graphics in the document 
    { 
     var myConfirmation = confirm("Add captions to all images in the document?", false, "LabelGraphics.jsx"); 
     if (myConfirmation == true) 
      { 
       var myGraphics = myDocument.allGraphics; 
       } 
     } 
    else  
     { // If graphics are selected, just add captions to the selected items, as long as they are rectangles(image frames) 
     var myConfirmation = true; 
     var mySelections = app.selection; 
     myGraphics = new Array(); 

     for(i = 0; i < mySelections.length; i++){ 
       if(mySelections[i] == "[object Rectangle]"){ //Check to make sure selection only includes rectangles 
         myGraphics.push(mySelections[i].allGraphics[0]); 
         } 
        else{ 
         //alert("Objects other than graphics were selected!"); 
         //Nothing happens if you don't select at least one graphic 
         } 
       } 
      } 

什麼,我需要在這一點上做的是創建一個循環,你建議運行雖然從第一所有頁面到最後應用的標籤。

+0

您可以添加文檔名稱的示例嗎?這樣所有這些分裂的目的可能會變得更加清晰。請注意,*第一次*操作(將所有不是數字的數字全部替換掉​​)似乎使這些分割變得不必要。當你運行這個腳本時,它也不清楚。事實上,它不應該設置多於一個標籤。 – usr2564301

+0

嗨Jongware我滾動通過您的網站整個早晨,但無法找到答案,謝謝你的答覆。文件名的一個例子是ABCD1934 P2_1.indd。我試圖以一個只包含文件名中的數字,頁碼和數字的標籤結束。例如1934_2_001謝謝你讓我知道你是否需要別的什麼 –

+0

我只是想讓某些東西去工作,我已經編輯了一個現有的腳本。我認爲腳本可能會在文檔中的所有圖形上循環,然後爲它添加一個標籤,標籤是我之前發送的,我是否正確認爲我應該試着讓腳本一次只做一頁? –

回答

1

屬性app.activeWindow.activePage.name只返回一個頁面,在你的當前(活動)的InDesign窗口可見「名稱」(這的確是當前頁碼)。除非您的代碼主動切換佈局窗口以依次顯示每個頁面,否則它將始終返回相同的編號(並且如果當前顯示的頁面是第一頁,那麼您將得到完全描述的內容)。

要指定myLabel到整個文檔中的每個頁面數,你需要遍歷每個文檔的的:

for (p=0; p<myDocument.pages.length; p++) 
{ 
    myLabel = myDocument.name.replace(/\D+/,'') + '_' 
     + app.activeDocument.pages[p].name +'_'+myCounter; 
    /* .. use myLabel here .. */ 
} 

我刪除了split命令,因爲正則表達式已經刪除所有字符這些數字不是數字,因此沒有空格或下劃線左側可以在上拆分

+0

嗨謝謝你回到我身邊,我非常感謝。這是我第一次嘗試這樣的事情,所以它真的超越了我,但我已經變得癡迷,我實際上不知道該把它放在哪裏。我把它放在我認爲你建議的地方,但是現在它將頁碼添加爲所有頁面文檔的最後頁碼。我可以寄給你我所擁有的東西嗎?你向我展示你的建議?實際上,我從另一個腳本中抽取了一些代碼來分解我在一起的東西。 –

+0

你可以編輯你的文章並添加**相關**部分的腳本。 [儘量減少你的代碼](http://stackoverflow.com/help/mcve)雖然不錯,但是你有點過分了,所以現在我不能說出它有什麼問題。 – usr2564301

+0

我試圖編輯它,但我不認爲它的工作...我想它更多的與一個功能... if(app.documents.length!= 0){if(app.documents。 item(0).allGraphics.length!= 0){...所以我認爲我可能會離開商標,但我正在考慮寫一個新函數來獲取文檔中所有頁面的數量,然後更改它這樣它就可以從第一頁到最後一頁的順序遍歷每個頁面上的所有圖形......這是一個更好的方法來做到這一點..我如何與您分享腳本 –