Highlighted

[scripting] Delete Hidden Layers in preselected Group only

Explorer ,
Jul 10, 2020

Copy link to clipboard

Copied

I know there are many threads about deleting hidden layers but my insight seems not deep enough to cobble it together for layers inside a selected group *only* ...

 

Suppose I've already selected only one Group manually or by action step.

This group will have no nested groups.

 

I'd now like a script that deletes any hidden layer — only within the selected group.

 

Thank you for any help!

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

Try a new version

var a= activeDocument.activeLayer
if(a.typename == "LayerSet")
	for(var i = a.layers.length - 1; i >= 0; i--)
		a.layers[i].visible || a.layers[i].remove()

 

-Manan

Topics

Actions and scripting, How to

Views

82

Likes

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

[scripting] Delete Hidden Layers in preselected Group only

Explorer ,
Jul 10, 2020

Copy link to clipboard

Copied

I know there are many threads about deleting hidden layers but my insight seems not deep enough to cobble it together for layers inside a selected group *only* ...

 

Suppose I've already selected only one Group manually or by action step.

This group will have no nested groups.

 

I'd now like a script that deletes any hidden layer — only within the selected group.

 

Thank you for any help!

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

Try a new version

var a= activeDocument.activeLayer
if(a.typename == "LayerSet")
	for(var i = a.layers.length - 1; i >= 0; i--)
		a.layers[i].visible || a.layers[i].remove()

 

-Manan

Topics

Actions and scripting, How to

Views

83

Likes

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
Adobe Community Professional ,
Jul 10, 2020

Copy link to clipboard

Copied

Hi there,

Try the following code

var a= activeDocument.activeLayer
if(a.typename == "LayerSet")
	for(var i = 0; i < a.layers.length; i++)
		a.layers[i].visible || a.layers[i].remove()

 

-Manan

Likes

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
Reply
Loading...
Explorer ,
Jul 10, 2020

Copy link to clipboard

Copied

Thanks!  Very compact but it does not fully work...

It seems to always work when there is just one invisible layer.

When there are more, it depends where they are.

An example where it fails is if I use 4 layers inside the group and the two middle ones are invisible. It will then delete only the first invisible one.

 

P.S. I'm only using like 5 simple pixel and adjustment layers trying it out..

Likes

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
Reply
Loading...
Explorer ,
Jul 10, 2020

Copy link to clipboard

Copied

It seems the problem occurs only when two or more consecutive layers are invisible.

 

Doesn't work for ...

invisible

invisible

visible

visible

 

Works ...

invisible

visible

invisible

visible

Likes

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
Reply
Loading...
Adobe Community Professional ,
Jul 10, 2020

Copy link to clipboard

Copied

Try a new version

var a= activeDocument.activeLayer
if(a.typename == "LayerSet")
	for(var i = a.layers.length - 1; i >= 0; i--)
		a.layers[i].visible || a.layers[i].remove()

 

-Manan

Likes

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
Reply
Loading...
Explorer ,
Jul 10, 2020

Copy link to clipboard

Copied

That seems perfect!

Thank you very, very much, Manan!  Have a great weekend 🙂

 

P.S.

When you Undo step by step, one of the invisible layers will now be visible. No problem to me — just a warning for other users.

Likes

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
Reply
Loading...
Adobe Community Professional ,
Jul 10, 2020

Copy link to clipboard

Copied

For a single step undo try the following

activeDocument.suspendHistory("removeHiddenLayer", "removeHiddenLayer()")
function removeHiddenLayer()
{
	var a= activeDocument.activeLayer
	if(a.typename == "LayerSet")
		for(var i = a.layers.length - 1; i >= 0; i--)
			a.layers[i].visible || a.layers[i].remove()
}

P.S. :- I have never manipulated history in PS and am new to PS scripting as well, based on my exp with InDesign scripting(that is what i do mostly) manipulating history could sometimes cause issue. So please do test to be certain all works fine with this code snippet

 

-Manan

Likes

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
Reply
Loading...
Signfeld LATEST
Explorer ,
Jul 11, 2020

Copy link to clipboard

Copied

That works fine! Thank you again, Manan! 🙂

Likes

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
Reply
Loading...