gpt4 book ai didi

javascript - 在 Adob​​e Illustrator 中检查锁定/隐藏图层

转载 作者:行者123 更新时间:2023-12-02 13:58:43 25 4
gpt4 key购买 nike

我正在 Adob​​e Illustrator (AI) 中编写脚本,但遇到了一个我无法理解的问题。这可能是由于人工智能工作的基本方式造成的,但我想知道是否有办法解决这个问题。好的,所以我的问题是这样的:

如何检查 AI 中的子图层是否被锁定/隐藏而不直接锁定(最顶层被锁定/隐藏,而子图层“继承状态”)。

我目前有一个脚本,它会执行一个过程(在图层上循环),并且只要图层没有锁定/隐藏就会执行此操作。问题是当顶层(父层)设置为锁定或隐藏时,其所有子层/对象都会继承该特征(锁定或隐藏)。我的脚本遇到的问题是这些锁定/隐藏的子图层被分类为可见和未锁定(或可见未定义)。还有其他方法可以确定吗?

脚本:

// JavaScript Document
if (app.documents.length > 0) {
var docRef = app.activeDocument;
var docLayers = docRef.layers;
var searchText = "";
var replaceText = "";
var found = false;

function recurseLayers(currLayers) {
var length = currLayers.length;
var currentLayer = null;
var searchtext = searchText;
var replacetext = replaceText;

try {
for (var i = length; i--;) {
currentLayer = currLayers[i];

replaceName(currentLayer, searchText, replaceText);

if (currentLayer.layers) {
recurseLayers(currentLayer.layers);
}
}
} catch (e) {
logger (e);
}
}

function replaceName(objArray, searchText, replaceText) {
try {
var visible = objArray.visible;
var locked = objArray.locked;
var typeName = objArray.typename;
if (visible && !locked) {
//var searchtext = "/\s*" + searchText + "\s*\d*/";
objArray.name = objArray.name.replace(searchText, replaceText);
}
} catch (e) {
logger(e);
}
}


startGUI();

function startGUI() {

var win = new Window("dialog", "Replace Layer name", undefined);

win.orientation = "column";
win.alignChildren = ["fill", "fill"];

// Search
var searchGrp = win.add("panel", undefined, "Search and Replace");
searchGrp.orientation = "column";
searchGrp.alignChildren = ["fill", "fill"];

var titleMsgS = searchGrp.add("statictext", undefined, "Layer name to search:");
var txt_searchText = searchGrp.add("edittext { characters: 1, justify: 'center', active: true }");

txt_searchText.helpTip = "Input layer name to replace";

var titleMsgR = searchGrp.add("statictext", undefined, "Layer name to replace with:");
var txt_replaceText = searchGrp.add("edittext { characters: 1, justify: 'center', active: true }");
txt_replaceText.helpTip = "Input layer name to replace with";

// Set first text box to active
txt_searchText.active = true;

win.addEventListener ("keydown", function(kd) {enter(kd) });

// Replace button
var replaceBtn = searchGrp.add("button", undefined, "Replace");
replaceBtn.helpTip = "Replace layer name";
replaceBtn.onClick = function() {
searchText = txt_searchText.text;
replaceText = txt_replaceText.text;

recurseLayers(docLayers);
app.redraw();
}

function enter(k) {
if (k.keyName == "Enter") {
recurseLayers(searchText.text, replaceText.text);
app.redraw();
}
}

// Close button
var quitBtn = win.add("button", undefined, "Close");
quitBtn.helpTip = "Press Esc to Close";

// Event listener for the quit button
quitBtn.onClick = function() {
win.close();
}

// Centering & Show Window
win.center();
win.show();
}


// Prints stack trace
function logger(e) {
var errorMsg = "";

errorMsg = errorMsg.concat("An error has occured:\n", e.line, "\n", e.message, "\n", e.stack);
//$.writeln(errorMsg);
}

} else {
alert("You do not have any document opened!");
}

示例:顶层(蓝色方 block )设置为锁定(灯锁符号)。当子图层继承锁定状态时(灰色的锁定符号)。在 AI 中,这些子层被认为是锁定的,但是,对于我的脚本,它们被标记为解锁(使用 layer.locked)。

enter image description here更新:工作代码,感谢@ermax。

function recurseLayers(currLayers) {
var length = currLayers.length;
var currentLayer = null;
var searchtext = searchText;
var replacetext = replaceText;

try {
for (var i = length; i--;) {
currentLayer = currLayers[i];
replaceName(currentLayer, searchText, replaceText);

if (currentLayer.layers) {
recurseLayers(currentLayer.layers);
}
}
} catch (e) {
logger (e);
}
}

function replaceName(currLayer, searchText, replaceText) {
try {
var visible = currLayer.visible;
var locked = currLayer.locked;
var typeName = currLayer.typename;
if (checkLayerVisibility(currLayer) && !checkLayerLocked(currLayer)) {
currLayer.name = currLayer.name.replace(searchText, replaceText);
}
} catch (e) {
logger(e);
}
}

function checkLayerLocked(layer) {
if(!layer.locked)
for(var parent = layer.parent; parent.typename=='Layer'; parent = parent.parent) {
if(parent.locked)
return true;
}
return layer.locked;
}

function checkLayerVisibility(layer) {
if(layer.visible)
for(var parent = layer.parent; parent.typename=='Layer'; parent = parent.parent) {
if(!parent.visible)
return false;
}
return layer.visible;
}

最佳答案

您可以通过检查父图层中的状态来检查子图层的可见状态,如果其中一个图层被隐藏,则该子图层也被隐藏:

function checkLayerVisibility(layer)
{
if(layer.visible)
for(var parent = layer.parent; parent.typename=='Layer'; parent = parent.parent)
{
if(!parent.visible)
return false;
}
return layer.visible;
}

对于锁定/解锁状态,同样的方式:

function checkLayerLockedStatus(layer)
{
if(!layer.locked)
for(var parent = layer.parent; parent.typename=='Layer'; parent = parent.parent)
{
if(parent.locked)
return true;
}
return layer.locked;
}

关于javascript - 在 Adob​​e Illustrator 中检查锁定/隐藏图层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40533623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com