javascript - Checking for Locked/Hidden Layers in Adobe Illustrator -
i working on script in adobe illustrator (ai), , running issue unable understand. maybe due underlying way ai works, know if there way work around this. ok, question this:
how check if sublayer in ai locked/hidden while not directly locked (top layer locked/hidden while sublayers 'inherit status').
i have script process (looping on layers) , long layer not locked/hidden. problem when top layer (parent layer) set locked or hidden, of sublayer/objects inherit trait (locked or hidden). problem having script these locked/hidden sublayers being classified visible , unlocked (or visible undefined). there way determine this?
script:
// 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 { (var = 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 , replace"); searchgrp.orientation = "column"; searchgrp.alignchildren = ["fill", "fill"]; var titlemsgs = searchgrp.add("statictext", undefined, "layer name search:"); var txt_searchtext = searchgrp.add("edittext { characters: 1, justify: 'center', active: true }"); txt_searchtext.helptip = "input layer name replace"; var titlemsgr = searchgrp.add("statictext", undefined, "layer name replace with:"); var txt_replacetext = searchgrp.add("edittext { characters: 1, justify: 'center', active: true }"); txt_replacetext.helptip = "input layer name replace with"; // set first text box 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 close"; // event listener 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 not have document opened!"); }
example: top layer (blue square) set locked (light lock symbol). while sublayers inheriting locked status (grayed out locked symbol). in ai, these sublayers considered locked, however, script labeled @ unlocked (using layer.locked).
function recurselayers(currlayers) { var length = currlayers.length; var currentlayer = null; var searchtext = searchtext; var replacetext = replacetext; try { (var = 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; }
you can check sublayer visible status checking status in parent layers, if 1 of them hidden, hence sublayer hidden:
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; }
for locked/unlocked status same way:
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; }
Comments
Post a Comment