一些背景
我们在 Adobe Illustrator CC 2022 中进行产品标签设计。此脚本的目的是根据用户选择将公制值转换为英制值:ML 到 FL OZ 或 G 到 OZ。数学/转换部分工作正常。
我需要什么指导
我需要判断用户是否有:
包含的代码片段确实适用于上面的数字 1 和 2,但列表中的数字 3 我似乎无法正确。它似乎完全忽略了嵌套的“Catch”。我对 Try/Catch 块不太熟悉,并且尝试过 if/then 语句,但没有成功。
有什么方法可以让脚本不忽略嵌套的捕获,也许还有其他解决方法吗?
有问题的代码片段(为了便于参考,我添加了注释):
if (app.documents.length > 0)
{
var $docRef = app.activeDocument;
var $rslt = "I am the replacement value";
try
{
//check if the selected object is a "Text Frame"
if($docRef.selection[0].typename == "TextFrame")
{
$docRef.selection[0].contents = $rslt;
}
}
catch(e1)
{
alert("e1 :"+e1.message); // "e1: undefined is not an object"
//DO THE FOLLOWING: Ignore the error and try the nested check
try
{
//check if the parent object of selected is a "Story"... thus editable text within a Text Frame
if($docRef.selection.parent.typename == "Story")
{
$docRef.selection.contents = $rslt;
}
}
catch(e2)
{
alert("e2 :"+e2.message); // "e2: undefined is not an object"
//DO THE FOLLOWING: Assume that no ediatble text is selected, create a Text Frame and populate it with $rslt value
alert("Creating new Text Frame");
//MY PROBLEM: It ignores this Catch step completely...
}
}
}
可能是这样的:
var sel = app.selection;
if (sel.typename == undefined) sel = sel[0];
try { sel.contents = "I am the replacement value" } catch(e) {}