我试着制作了一个脚本,它可以根据所选艺术的 colored颜色 数量来执行几次动作(循环). 如果有一种方法可以将 colored颜色 名称存储在数组中,那也是很好的.

我自己想不出来,我只知道 playbook 的基本内容:

var doc = app.activeDocument;
var sel = doc.selection[0];
var selcolors = 0;

//====================
//Here I need to get how many colors the selected art have and set it to selcolors
//====================

for(i = 0; i < selcolors; i++)
{
   //Set of actions
}

任何帮助我们都将不胜感激!

Example of my graphics: enter image description here

推荐答案

这取决于你到底想要用什么 colored颜色 ,以及你使用的是哪种 colored颜色 .

例如,对于专色,可以是这样的:

var doc = app.activeDocument;
var sel = doc.selection;

// get all path items from the selection
var pathItems = [];
for (var i=0; i<sel.length; i++) get_all_pathItems(sel[i], pathItems);

// put all the colors into the object
var obj = {};
for (var i=0; i<pathItems.length; i++) {
    var item = pathItems[i];
    if (item.filled) {
        var color = item.fillColor;
        if (color instanceof SpotColor) obj[color.spot.name] = color;
    }
    if (item.stroked) {
        var color = item.strokeColor;
        if (color instanceof SpotColor) obj[color.spot.name] = color;
    }
}

// get a number of the colors from the object 
var n = 0;
for (key in obj) n++;

// updated lines below:

// run some actions, perhaps you don't even need the number
for (key in obj) {
    if (key == 'White') continue; // skip White color
    // action 1
    // action 2
    // etc...
}

// ----------------------------------------------------------

// recursive function to fill the given array with all pathItems of given item
function get_all_pathItems(item, arr) {
    try { if (item instanceof PathItem) arr.push(item) } catch(e) {}
    try {
        if (item instanceof GroupItem) {
            var paths = item.pathItems;
            for (var i=0; i<paths.length; i++) arr.push(paths[i]);
            var groups = item.groupItems;
            for (var i=0; i<groups.length; i++) get_all_pathItems(groups[i], arr);
        }
    } catch(e) {}
}

而且还有一个问题.在Illustrator中,一组项目可以有自己的 colored颜色 (填充和描边).据我所知,你不能用 playbook 来了解团队的 colored颜色 .脚本只能获取路径项的 colored颜色 ,而不能获取组.

Javascript相关问答推荐

将数据从strapi提取到next.js,但响应延迟API URL

RxJS setTimeout操作符等效

react 路由加载程序行为

如何在表格上拥有水平滚动条,在正文页面上拥有垂直滚动条,同时还对html表格的标题使用位置粘性?

Klaro与Angular的集成

vscode扩展-webView Panel按钮不起任何作用

在页面上滚动 timeshift 动垂直滚动条

Javascript,部分重排序数组

InDesign—创建一个独立的窗口,在文档中进行更正时保持打开状态

将2D数组转换为图形

Chromium会将URL与JS一起传递到V8吗?

我在我的Java代码中遇到了问题,代码的一部分看不到先前定义的对象

并不是所有的iframe都被更换

优化Google Sheet脚本以将下拉菜单和公式添加到多行

IF语句的计算结果与实际情况相反

如何修复使用axios运行TSC index.ts时出现的错误?

更改agGRID/Reaction中的单元格格式

未捕获的不变违规:即使在使用DndProvider之后也应使用拖放上下文

对不同目录中的Angular material 表列进行排序

如果我将高度设置为其内容的100%,则在Java脚本中拖动以调整面板大小时会冻结