如何在Adobe InDesign中使用app.selection [0]作为脚本 [英] How to use app.selection[0] for scripts in Adobe InDesign

查看:177
本文介绍了如何在Adobe InDesign中使用app.selection [0]作为脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过仅测试当前选择(而不是整个文档)来运行代码,并且我很难理解数组app.selection及其方法是如何工作的。首先,我使用for循环来循环选择每个项目:

I would like to run code by testing only the current selection (not the whole document) and I'm having difficulty understanding exactly how the array "app.selection" and its methods work. To start, I use a "for" loop to cycle through each item selected by using:

for(loop = 0; loop < app.selection.length; loop++){
    var sel = loop;
}

这样可行,但是当我想确定每个项目是什么时,它有点奇怪。例如,

This works okay, but when I want to get into determining what each item IS, it gets a little weird. For example,

for(txt = 0; txt < app.selection[sel].textFrames.length; txt++){
    // do something to each text frame in the selection here.
}

不能按预期工作,但

for(img = 0; img < app.selection[sel].allGraphics.length; img++){
    // do something to each graphic in the selection here
}

似乎工作正常,无论选择是否包括更多单独的图形,或者它是在组内还是组外。

seems to work fine, regardless if the selection includes more than just graphics alone, or whether it is inside or outside a group.

有时,看起来app.selection [0]是唯一可以自行访问项目的方法。换句话说,如果选择了一个文本框,app.selection [0]可能与app.document.textFrames [0]相同,在这种情况下,说

At times, it seems like app.selection[0] is the only way to access the item by itself. In other words, if a text frame is selected, app.selection[0] might be the same as app.document.textFrames[0], in which case it would be redundant (and incorrect) to say

app.document.textFrames[0].textFrames[0]

然而,不同页面项目上的相同概念就像魅力一样。跟随它是非常令人费解的。此外,似乎无法确定该项目是什么类型的对象。我想说的是:

And yet the same concept on different page items works like a charm. It is quite puzzling to follow. Furthermore, it seems impossible to determine what kind of object the item is. I want to say something along the lines of:

if (app.selection[0] == [object TextFrame])

但这似乎对我不起作用。有没有办法清楚地测试当前项目是组,图形还是文本框架,并根据结果做不同的事情?

but that does not seem to work for me. Is there a way to clearly test if the current item is a group, a graphic or a text frame and do different things depending on the result?

推荐答案

app.selection返回一个Objects数组,因此数组中的每个项都可以是不同的类型,并且它可用的属性和方法会有所不同。使用Extendscript Javascript控制台时,只需输入

app.selection returns an array of Objects, so each item in the array can be of a different type, and the properties and methods available to it will differ. When using the Extendscript Javascript Console you can see what a particular item in the array is on the fly by just typing

app.selection[0]

(或任何数字)。结果将类似于[object TextFrame]。

(or whatever number). The result will be something like [object TextFrame].

在循环选择数组时,您可以使用app.selection [0] .constructor.name来确定类型每个。或者,如果您只对某些类型感兴趣,

While looping through the selection array, you could use app.selection[0].constructor.name to determine the type of each. Or, if you're only interested in certain types,

if (app.selection[i] instanceof TextFrame){}

此时,您将了解更多可以访问的属性,具体取决于类型。

At that point you'll know more about which properties you can access, depending on the type.

要回答问题的第二部分,没有allTextFrames属性,但有一个allPageItems属性。这将返回一个pageItems数组(textFrames,groups等),您可以像app.selection一样使用它。所以,如果我在文档的第一页上分组了三个文本框(没有其他内容),我可以看到以下内容都是正确的:

To answer the second part of the question, there isn't an allTextFrames property, but there is an allPageItems property. This returns an array of pageItems (textFrames, groups, etc.), and you can work with it similarly to app.selection. So, if I have three text frames grouped on the first page of my document (and nothing else), I can see that the following are all true:

app.activeDocument.pages[0].textFrames.length == 0;
app.activeDocument.pages[0].allPageItems.length == 4;
app.activeDocument.pages[0].allPageItems[0] instanceof Group;
app.activeDocument.pages[0].allPageItems[1].constructor.name == "TextFrame";

因此,如果它比textFrames集合对你更有用,你可以循环遍历该数组。请记住,您无权访问TextFrames的特殊集合属性(如everyItem())。

So you could probably cycle through that array if it's more useful to you than the textFrames collection. Just keep in mind that you don't have access to the special collection properties of TextFrames (like everyItem()).

这篇关于如何在Adobe InDesign中使用app.selection [0]作为脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆