javascript - 获取 InDesign 多页文档的当前页码

标签 javascript adobe-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 窗口。除非您的代码主动切换布局窗口以依次显示每个页面,否则它将始终返回相同的数字(如果当前显示的页面是第一页,那么您将得到您所描述的内容)。

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

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

我删除了 split 命令,因为正则表达式已经删除了所有非数字字符,因此没有空格或下划线可供拆分。/p>

关于javascript - 获取 InDesign 多页文档的当前页码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30631898/

相关文章:

javascript - 是否可以在 Extendscript 中抑制 CheckOut/CheckIn 对话框?

javascript - 如何在 indesign 中要求或包含脚本?

javascript - 如何创建和递增 2 位或 3 位十六进制数?

javascript - 多个文件选择的 Safari 中的文件输入大小问题

JavaScript:搜索字符串中的表达式,如果未找到则返回错误

javascript - Jquery幻灯片效果,点击后需要延迟

javascript - 无法使用 jquery 更改 css 类

javascript - f[i].insertionPoints[0].rectangles.add({geometricBounds :

javascript - TB_GETBUTTON 的结构是否正确? (WinXP) (同时检测进程是64位还是32位)

java - 如何在 adobe indesign 中创建我们自己的插件