Illustrator v2024脚本频繁报1346458189('PARM')错误求助
解决Illustrator 2024脚本错误:1346458189 ('PARM')
问题背景
在Illustrator v2024中,运行各类简单脚本时频繁触发错误:
Error: an Illustrator error occurred: 1346458189 ('PARM')
尝试过调整坐标系等方案仍无法解决,涉及的脚本功能为:获取选中对象的厘米尺寸,生成文本框显示尺寸并将文本框居中到视图。原脚本代码如下:
//this script task: Adds the selected objects to the page in cm. // The error it gives every now and then is: // Error: an Illustrator error occurred: 1346458189 ('PARM') #target illustrator offsetT=0 var myDoc = app.activeDocument; myDoc.rulerOrigin = [0,0]; var sel = app.selection; //sel as selections array. var str = ""; function essenziki() { if(sel.length==0){ alert("choose something first"); return } for (var i = 0; i < sel.length; i++) { //loop and check each selected objects str += (pt2mm(sel[i].width) / 10+offsetT).toFixed(0) + "*" + (pt2mm(sel[i].height) / 10+offsetT).toFixed(0)+ "\n"; } str = str.replace(/\n$/, ''); // Replace last line break to empty symbol var tx = myDoc.textFrames.add(); tx.contents = str; tx.textRange.characterAttributes.size=150; tx.textRange.characterAttributes.textFont = app.textFonts.getByName('Arial-BoldMT'); app.executeMenuCommand("deselectall"); //deselect all abjects var cmykColor = new CMYKColor(); cmykColor.cyan = 34; cmykColor.magenta = 49; cmykColor.yellow = 19; cmykColor.black = 27; tx.textRange.characterAttributes.fillColor = cmykColor; app.executeMenuCommand("deselectall"); //deselect all abjects tx.selected = true; function pt2mm(n){ return n * 0.35277778; } }; essenziki(); if (app.selection.length > 0) { var selectedItem = app.selection[0]; // Get the first selected item // Get the center of the current view in document coordinates var viewCenter = app.activeDocument.activeView.centerPoint; // [x, y] of view center in document coordinates // Calculate the center of the selected item var itemBounds = selectedItem.geometricBounds; // [left, top, right, bottom] var itemCenterX = (itemBounds[0] + itemBounds[2]) / 2; var itemCenterY = (itemBounds[1] + itemBounds[3]) / 2; // Calculate the offset to move the item to the center of the view var offsetX = viewCenter[0] - itemCenterX; var offsetY = viewCenter[1] - itemCenterY; // Move the item to the center of the view selectedItem.translate(offsetX, offsetY); }
错误原因分析
PARM错误通常表示参数传递异常,结合脚本排查出几个核心问题:
- 函数嵌套作用域:
pt2mm函数定义在essenziki内部,可能导致Illustrator脚本引擎解析时出现参数绑定问题 - 选择状态操作冲突:多次调用
deselectall菜单命令后直接设置选中状态,UI线程与脚本线程不同步,导致参数异常 - 边界情况未处理:未检查
app.activeDocument是否存在,若文档未激活会触发参数错误 - 字体获取无容错:
app.textFonts.getByName('Arial-BoldMT')若字体不存在会直接抛出参数错误
修复后的脚本代码
#target illustrator // 单位转换函数移至全局作用域 function pt2mm(n) { return n * 0.35277778; } function essenziki() { // 检查是否有激活文档 if (!app.documents.length) { alert("请先打开一个文档"); return; } var myDoc = app.activeDocument; myDoc.rulerOrigin = [0, 0]; var sel = app.selection; var offsetT = 0; var str = ""; if (sel.length === 0) { alert("请先选择对象"); return; } // 遍历选中对象生成尺寸字符串(厘米) for (var i = 0; i < sel.length; i++) { var widthCm = (pt2mm(sel[i].width) / 10 + offsetT).toFixed(0); var heightCm = (pt2mm(sel[i].height) / 10 + offsetT).toFixed(0); str += widthCm + "*" + heightCm + "\n"; } str = str.replace(/\n$/, ''); // 创建文本框并设置属性 var tx = myDoc.textFrames.add(); tx.contents = str; // 字体设置添加容错 var targetFont = app.textFonts.getByName('Arial-BoldMT') || app.textFonts[0]; tx.textRange.characterAttributes.textFont = targetFont; tx.textRange.characterAttributes.size = 150; // 设置CMYK填充色 var cmykColor = new CMYKColor(); cmykColor.cyan = 34; cmykColor.magenta = 49; cmykColor.yellow = 19; cmykColor.black = 27; tx.textRange.characterAttributes.fillColor = cmykColor; // 直接清除选择并选中文本框,避免菜单命令的线程延迟 app.selection = [tx]; // 将文本框居中到视图 var viewCenter = myDoc.activeView.centerPoint; var itemBounds = tx.geometricBounds; var itemCenterX = (itemBounds[0] + itemBounds[2]) / 2; var itemCenterY = (itemBounds[1] + itemBounds[3]) / 2; var offsetX = viewCenter[0] - itemCenterX; var offsetY = viewCenter[1] - itemCenterY; tx.translate(offsetX, offsetY); } essenziki();
关键修复点说明
- 将
pt2mm函数移至全局作用域,避免嵌套导致的解析异常 - 添加文档存在检查,防止无激活文档时出错
- 字体获取添加容错,若目标字体不存在则使用第一个可用字体
- 替换
app.executeMenuCommand("deselectall")为app.selection = [tx],直接操作选择数组,避免UI线程同步问题 - 将文本框居中逻辑整合到函数内部,减少全局变量依赖,避免选择状态冲突
内容的提问来源于stack exchange,提问作者enisio
相关产品推荐
相关产品推荐

