我正在尝试将 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/