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

[scripting] Delete Hidden Layers in preselected Group only

Advocate ,
Jul 10, 2020 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!

TOPICS
Actions and scripting

Views

405

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 2 Correct answers

Community Expert , Jul 10, 2020 Jul 10, 2020

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

Votes

Translate

Translate
Community Expert , Jul 10, 2020 Jul 10, 2020

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

...

Votes

Translate

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

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
Advocate ,
Jul 10, 2020 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..

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
Advocate ,
Jul 10, 2020 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

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

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
Advocate ,
Jul 10, 2020 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.

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

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
Advocate ,
Jul 11, 2020 Jul 11, 2020

Copy link to clipboard

Copied

LATEST

That works fine! Thank you again, Manan! 🙂

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