如何修改Photoshop脚本:仅对选中图层清除选区
修改Photoshop脚本:仅清除选中图层的选区
原脚本可清除所有图层的当前选区,现需修改为仅对选中图层执行清除选区操作,以下是修改完成的完整脚本:
#target photoshop // 2023, use at your own risk; if (app.documents.length > 0) { /* Start Process selected layers - from jazz-y */ var s2t = stringIDToTypeID; (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs')); r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum')); var lrs = executeActionGet(r).getList(p), sel = new ActionReference(); for (var i = 0; i < lrs.count; i++) { // 选中当前遍历到的图层 (r = new ActionReference()).putIdentifier(s2t('layer'), p = lrs.getReference(i).getIdentifier(s2t('layerID'))); (d = new ActionDescriptor()).putReference(s2t("target"), r); executeAction(s2t('select'), d, DialogModes.NO); // 清除该图层的选区 try {activeDocument.selection.clear()} catch (e) {} } /* End Process selected layers - from jazz-y */ }; ////// based on code by mike hale, via paul riggott ////// function selectLayerByID(id,add){ add = undefined ? add = false:add var ref = new ActionReference(); ref.putIdentifier(charIDToTypeID("Lyr "), id); var desc = new ActionDescriptor(); desc.putReference(charIDToTypeID("null"), ref ); if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); desc.putBoolean( charIDToTypeID( "MkVs" ), false ); try{ executeAction(charIDToTypeID("slct"), desc, DialogModes.NO ); }catch(e){ } };
修改说明
- 移除了原脚本中遍历所有图层的
collectLayersNamesIndexAndIdentifier函数及相关逻辑 - 替换为获取选中图层ID列表的代码,仅循环处理用户选中的图层
- 循环内先定位选中目标图层,再执行清除选区操作
- 保留原有的异常捕获逻辑,避免无选区的图层导致脚本中断
内容的提问来源于stack exchange,提问作者Pubg Mobile
相关产品推荐
相关产品推荐

