获取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
                        } 
                } 
            } 
在这一点上,我需要做的是创建一个循环,正如你所建议的那样,从第一个到最后一个应用标签运行所有页面。

属性app.activeWindow.activePage.name只返回在当前(活动)InDesign窗口中可见的一个页面的"name"(这确实是当前页码)。除非您的代码主动切换布局窗口以依次显示每个页面,否则它将始终返回相同的数字(如果当前显示的页面是第一页,那么您将得到您所描述的内容)。

要将myLabel分配给整个文档中的每个页码,您需要遍历文档的每个:

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

我删除了split命令,因为regex已经删除了所有非数字的字符,因此在上没有空格或下划线来分割

最新更新