繁体   English   中英

检查选择是否包含图形

[英]Check if the selection contains graphics

我正在尝试编写一个脚本,其中的一部分是找出所选对象中是否有图像,或者对象是否为空形状。

我需要检查选择的文件是否都是图像、所有形状或混合的。

我将这段代码与警报放在一起(最终代码不会带有警报,这是为了理解目的):

    if (app.selection.length == 0) {
        alert ("Nothing selected");
        }
    else {
        for (var i = 0; i < app.selection.length; i++) {
            if ((app.selection[i].graphics.length == 0) && (app.selection[i].graphics.length == 1)) {
                alert ("The files are mixed");
                }
            else if (app.selection[i].graphics.length == 1) {
                alert ("The files are images");
                }
            else if (app.selection[i].graphics.length == 0) {
                alert ("The files are shapes");
                }
            }
        }

显然代码没有按预期工作(它适用于图像和形状,而我已经知道混合部分它没有正确编写),因为我的搜索没有定论。

我正在寻找的是:如果没有选择任何东西(做某事),否则如果文件是形状和图像(只对形状做某事),否则如果只有图像(做某事),否则如果只有形状(做某事)。

有人可以告诉我该怎么做吗?

基本上算法可以是这样的:

var sel = app.selection;

if (sel.length == 0) alert('Nothing selected');

else {

    var images = sel.pop().graphics.length > 0;
    var mixed = false;

    while (sel.length) {
        if ((sel.pop().graphics.length > 0) != images) {
            mixed = true;
            break;
        }
    }

    if (mixed) alert('Mixed');
    else {
        if (images) alert('Images');
        else alert('Shapes');
    }
}

但它不处理组。

更新

如果您只想从选择中获取形状(并省略图像),可以通过以下方式完成:

var sel = app.selection;
if (sel.length == 0) { alert('Nothing selected'); exit() }

var shapes = [];
for (var i=0; i<sel.length; i++)
    if (sel[i].graphics.length == 0) shapes.push(sel[i]);

if (shapes.length > 0) do_something(shapes);

// ----------------------------------------------------------------------
function do_something(shapes) {
    alert('Here we go...');
    app.selection = shapes;

    // do stuff...
}

或者一个简短的变体(只是为了取消选择图像):

var sel = app.selection;
for (var i=0; i<sel.length; i++)
    if (sel[i].graphics.length > 0) sel[i].select(SelectionOptions.REMOVE_FROM);

更新 2

这是执行以下操作的代码:

  • 如果没有选择 > 创建一个白色矩形,
  • 如果仅选择图像 > 创建一个白色矩形,
  • 如果只选择形状 > colors 形状为白色,
  • 如果选择是混合的 > 仅选择形状和 colors 白色。
var sel = app.selection;

if (sel.length == 0) fn_nothing();
else {
    var images = sel.pop().graphics.length > 0;
    var mixed = false;
    while (sel.length) {
        if ((sel.pop().graphics.length > 0) != images) {
            mixed = true;
            break;
        }
    }
    if (mixed) fn_mixed();
    else {
        if (images) fn_images();
        else fn_shapes();
    }
}

function fn_nothing() {
    alert('Nothing');
    create_white_rectangle();
}

function fn_images() {
    alert('Images');
    create_white_rectangle();
}

function fn_shapes() {
    alert('Shapes');
    color_shapes_white();
}

function fn_mixed() {
    alert('Mixed');
    select_shapes();
    color_shapes_white();
}

function select_shapes() {
    var sel = app.selection;
    while (sel.length) {
        var item = sel.pop();
        if (item.graphics.length > 0)
        item.select(SelectionOptions.REMOVE_FROM)
    }
    alert('Select shapes');
}

function color_shapes_white() {
    alert('Color shapes white')
}

function create_white_rectangle() {
    alert('Create a white rectangle');
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM