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

[ Branched ] Unlocking layers and sublayers and objects [JS]

Participant ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

More than 3 years later and I still couldn't find a Javascript solution for this yet. Thank you guys for all your work! Any chance you could adapt this to JS?



[ branched from Unlocking sublayers - Applescript to Illustrator forum by moderator ]

[ title changed by moderator ]

 

TOPICS
Scripting

Views

416

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 , Dec 03, 2020 Dec 03, 2020

Your script is buggy.

 

  layer = layers;

 

should read:

 

  layer = layers[n];

 

 Here is a more idiomatic implementation, which also unhides any hidden layers on the assumption OP wants to make everything editable:

 

function unlockLayers(layers) {
	for (var i = 0; i < layers.length; i++) {
		var layer = layers[i]
		layer.hidden = false
		layer.locked = false
		unlockLayers(layer.layers)
	}
}

var doc = app.activeDocument

unlockLayers(doc.layers)

 

If OP wishes the hidden layers to remain hidden, then de

...

Votes

Translate

Translate
Adobe
Community Expert ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

Hi @Eduard Buta 

Why not read the whole topic first?

 

See post #1 here in the same thread by

 

@Loic.Aigon  wrote:

"Here is a JS approach :                                                                                                                                                     

 

var doc = app.activeDocument;

var unlockLayers = function ( layers ) {
  var n = layers.length, layer;
  while ( n-- ) {
  layer = layers;
  layer.locked = false;
  layer.layers.length && unlockLayers ( layer.layers );
  }
}
unlockLayers ( doc.layers );

 

HTH

Loic"

 

 

Have fun

😉

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
Participant ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

Hi @pixxxelschubser 

 

I have read the whole post, and I have tried using that Script. I got some sort of error, and I just thought it might be deprecated since the final answer for the question was laid out in the section I've replied to earlier.

 

I might have to try that one out again, and see what exactly didn't work out. Being a designer and not a coder also doesn't help, so fingers crossed that I'll make it work. Thank you for your time nonetheless

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 ,
Dec 03, 2020 Dec 03, 2020

Copy link to clipboard

Copied

Your script is buggy.

 

  layer = layers;

 

should read:

 

  layer = layers[n];

 

 Here is a more idiomatic implementation, which also unhides any hidden layers on the assumption OP wants to make everything editable:

 

function unlockLayers(layers) {
	for (var i = 0; i < layers.length; i++) {
		var layer = layers[i]
		layer.hidden = false
		layer.locked = false
		unlockLayers(layer.layers)
	}
}

var doc = app.activeDocument

unlockLayers(doc.layers)

 

If OP wishes the hidden layers to remain hidden, then delete the `layer.hidden = false` line. (Layers that are hidden and locked will still unlock, mind.)

 

To unhide and unlock all page items after all layers have been unhidden and unlocked:

for (var i = 0; i < doc.pageItems.length; i++) {
	var item = doc.pageItems[i]
	item.hidden = false
	item.locked = false
}

It’s a little more complicated if some layers remain hidden as AI doesn’t like you modifying the contents of locked/hidden layers, so you would need to catch those errors and continue to the next item. But I’ll leave that as an exercise to the reader.

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
Participant ,
Dec 03, 2020 Dec 03, 2020

Copy link to clipboard

Copied

This worked great for me. Full code below for those that come after, with hidden layers remaining hidden:

function unlockLayers(layers) {
	for (var i = 0; i < layers.length; i++) {
		var layer = layers[i];
		layer.locked = false;
		unlockLayers(layer.layers);
	}
}

var doc = app.activeDocument;

unlockLayers(doc.layers);

for (var i = 0; i < doc.pageItems.length; i++) {
	var item = doc.pageItems[i];
	item.hidden = false;
	item.locked = false;
}

 Thank you very much!

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 ,
Dec 03, 2020 Dec 03, 2020

Copy link to clipboard

Copied

LATEST

 

Ok.

@hhas01  Good finding.

 

The script was posted in 2017. And the switch to the new forum software has broken many of the good old scripts (mostly a counter in square brackets is missing - like in this case, 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 ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

That's why I added the post again (cleanly) in my answer.

And I tested it too. It still works the way it was intended back then.

 

Please note: It only unlocks layers and deep nested sub-layers - but not the objects in the layers!

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
Participant ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

Got it. Thank you, I'll make it work for sure then.

 

It's been a number of years since you've written this, have you found any type of solution that also works with objects? Or is that simply a limitation of Illustrator that code can't bypass? I'm curious now, since you definitely know your stuff. 🙂 

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 ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

Ok.

At first: I didn't wrote this script. It was written by @Loic.Aigon 

Second: This script was written to (only) unlock layers and sublayers

 

Third: You have new requirements. Let me move our last answers into a new thread.

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 ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

Welcome in your own new thread.

😉

 

Try to add these two lines at the end of the script:

app.executeMenuCommand("unlockAll");
app.redraw();

 

If that works for you

have fun

😉

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
Participant ,
Dec 02, 2020 Dec 02, 2020

Copy link to clipboard

Copied

Apologies for the late reply. I've been away until this time.

It still doesn't seem to work for me. I get an Error 1302: No such element on Line 8: layer.layers.length && unlockLayers(layer.layers);

Do you have any idea what might cause this? Thank you in advance!

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 ,
Dec 02, 2020 Dec 02, 2020

Copy link to clipboard

Copied

No.

 

Without to see the code you've changed and screenshots of your file with opened Layers Panel, or better an example file - no, I can't. Sorry.

 

 

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
Participant ,
Dec 03, 2020 Dec 03, 2020

Copy link to clipboard

Copied

Thank you for your reply.

 

First thing's first, I've taken the entirety of the code above and dropped it in a notepad file which I then saved with a .jsx extension.

 

var doc = app.activeDocument;

var unlockLayers = function (layers) {
  var n = layers.length, layer;
  while (n--) {
  layer = layers;
  layer.locked = false;
  layer.layers.length && unlockLayers(layer.layers);
  }
}
unlockLayers (doc.layers);

 

I wasn't aware that layers need to be in a certain way for this to work. I am looking for a solution that I can run and unlock all the layers within a specific document, no matter how they're stacked and nested. Let me know if that's where I was missing some info. See a test I've tried below:

 

test.PNG

 

I am running Illustrator 24.3

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 ,
Dec 03, 2020 Dec 03, 2020

Copy link to clipboard

Copied

These are not layers and sublayers!

 

Those groups and deep nested groups and objects within you could unlock with the two lines of code that I have already posted.

@pixxxelschubser  wrote:

"… Try to add these two lines at the end of the script:

app.executeMenuCommand("unlockAll");
app.redraw();

 

If that works for you

have fun …"

 

 

Can you upload a sample document to a host of your choice and link here?

 

Regards

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