确定是否存在选择

问题描述 投票:0回答:1

有没有(更好的)方法来确定Photoshop中的图层中是否存在选择而没有在selection.bounds上使用try / catch?

try
{
    var sel = app.activeDocument.selection.bounds;
}
catch(e)
{
    var sel = undefined;
    alert("No selection");
}

if (sel) alert(sel);

如果没有选择而不是返回(预期的)未定义的边界,我只得到错误1302:没有这样的元素。因此需要尝试/捕获。

javascript undefined selection photoshop photoshop-script
1个回答
0
投票

我也遇到了这个问题,虽然我没有找到使用try..catch的方法,但我只是在我可以测试的Selection原型中添加了一个简单的active()函数。

Selection.prototype.active = function()
{
    try      { return (selection.bounds) ? true : false; }
    catch(e) { return false; }
}

这样,您可以调用app.activeDocument.selection.active()来查看是否选择了某些内容。

try部分中的三元运算符是为了防止将来报告未定义的Selection.bounds。

© www.soinside.com 2019 - 2024. All rights reserved.