• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Toggling layer lock

Mentor ,
Feb 16, 2012 Feb 16, 2012

Copy link to clipboard

Copied

hi all,

have one liner, simple as that:

app.documents.item(0).layers.itemByName("Graphics").locked = true;

I wonder, would it be possible to turn it to a toggler, without too much blood, sweat and tears?

I mean, script should lock named layer, if it founds that layer unlocked, and vice versa - UNlock it, if founded locked.

please don't kick me badly, I really tried, but... still nothing to write home about 

TOPICS
Scripting

Views

2.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Feb 16, 2012 Feb 16, 2012

app.documents.item(0).layers.itemByName("Layer 1").locked ^= 1;

I hope this helps to you.  and what you expected...

Votes

Translate

Translate
Engaged ,
Feb 16, 2012 Feb 16, 2012

Copy link to clipboard

Copied

app.documents.item(0).layers.itemByName("Layer 1").locked ^= 1;

I hope this helps to you.  and what you expected...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Feb 16, 2012 Feb 16, 2012

Copy link to clipboard

Copied

Yes, that's exactly what I was looking for... thank You!

Now I just need to find out, what the beast is that ^= 1. Never saw that expression before.

Thank You again!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 16, 2012 Feb 16, 2012

Copy link to clipboard

Copied

Technically it should be

..locked ^= true

because the property 'locked' is a Boolean, not a number. It does work because Javascript internally changes the numbers 0 and Not-0 to boolean false and true (which may or may not be interesting to know, anyway).

The expression a ^= b is shorthand for a = a ^ b. All of the simple math and logical functions can be written that way:

a = a - b -> a -= b

a *= b -> a = a*b

a &= b -> a = a & b

which may seem just to be a programmers' affection at first, but there are actual advantages to it. For example, not only would you have to write out this in full:

app.documents.item(0).layers.itemByName("Layer 1").locked = app.documents.item(0).layers.itemByName("Layer 1").locked ^ true;

but Javascript would have to parse it in its entirety as well. With the singular expression, the left hand part (the a above) is only evaluated once.

The ^ command is a simple Exclusive-Or (a.k.a. XOR), that is one or the other, not neither nor both. If ("Layer 1").locked = false, then

("Layer 1").locked (new) = ("Layer 1").locked=false XOR true

-- the left value (a) is false, the right value (b) is true, so in this case the result is "true", because it is 'true' that 'false XOR true' is "one OR the other". If the layer was locked, it would be

("Layer 1").locked(new) = ("Layer 1").locked=true XOR true

and the expression "true XOR true" equals false because then it is NOT "one OR the other".

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Feb 16, 2012 Feb 16, 2012

Copy link to clipboard

Copied

LATEST

Ugh...

you guys, are too smart for the real world tasks...

nevertheless, Imagine's one liner just does the trick I needed - seamlessly switch dedicated layer lock on and off.

With KB shortcut assigned it speeds up my workflow, it's simply faster than hiting F7 and clicking on icons...

Uwe, Jongware - your solutions are undoubtfully useful in the long run and in understanding how the whole stuff works.

Thank you all, I appreciate it and believe, other users will have benefit of it, too.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 16, 2012 Feb 16, 2012

Copy link to clipboard

Copied

@winterm – If I understand you right you want to un-lock every locked layer and lock every un-locked one?

Then see the following script:

var d= app.documents[0];

var allLayers = d.layers;

var lockedLayersIDsTrue = new Array();

var lockedLayersIDsFalse = new Array();

for(var n=0;n<allLayers.length;n++){

    if(allLayers.locked){

        lockedLayersIDsTrue.push(allLayers.id);

        }

    else{lockedLayersIDsFalse.push(allLayers.id)};

    };

for(var n=0;n<lockedLayersIDsTrue.length;n++){

    allLayers.itemByID(lockedLayersIDsTrue).locked = false;

    };

for(var n=0;n<lockedLayersIDsFalse.length;n++){

    allLayers.itemByID(lockedLayersIDsFalse).locked = true;

    };

First the locked state of every layer (in fact its ID) is recorded in two arrays, one for locked layers, one for unlocked ones.
Then the script goes through the locked layers array and unlocks all recoded layers, after that we visit the locked layers array and lock all recorded layers.


Uwe

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 16, 2012 Feb 16, 2012

Copy link to clipboard

Copied

^= expression performs a bitwise XOR and assigns the result to the first operand.

Here is a link for full description about XOR.

http://en.wikipedia.org/wiki/XOR_gate

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 16, 2012 Feb 16, 2012

Copy link to clipboard

Copied

@winterm – forget my version. Imagine's solution is much more smarter:

var d= app.documents[0];

//Toggle: for true set false and vice versa:

for(var n=0;n<d.layers.length;n++){

    d.layers.locked ^= 1;

    };

Uwe

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines