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

Delete hidden layers script

New Here ,
Sep 09, 2008 Sep 09, 2008
I wish, I wish for a javascript that would check my open document for hidden layers and delete them, thats all.

Thank you,
Richard
TOPICS
Scripting
6.6K
Translate
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 ,
Sep 09, 2008 Sep 09, 2008
for (i=0; i<app.activeDocument.layers.length; i++) {<br />if (!app.activeDocument.layers.visible) app.activeDocument.layers.remove();<br />}<br /><br />Enjoy!
Translate
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
New Here ,
Sep 10, 2008 Sep 10, 2008
Hello try67 and thank you very much for the script.

It works well but not at 100%.

As an example:
If I have a InDesign file with 20 hidden layers. The script will delete 10, then when double-clicked again 5 more will be gone, do it again and 2 more will be deleted, so the run command needs to be repeated until all hidden layers are gone.

I cannot find any particular pattern as to the way layers are deleted and what prompt the script to stop, but I am sure there is one.

If you find out why please let me know, in the mean time this sure beats: selecting layers, right click, select delete layers, click yes, wait...

Thanks again,
Richard
Translate
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
Explorer ,
Sep 10, 2008 Sep 10, 2008
Try

for (i=app.activeDocument.layers.length; i>0; i--) { if (!app.activeDocument.layers[i-1].visible) app.activeDocument.layers[i-1].remove(); }
Translate
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
Explorer ,
Jul 17, 2017 Jul 17, 2017

thank you

Translate
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
Explorer ,
Jul 26, 2018 Jul 26, 2018

Works perfectly, thanks!

Translate
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
Explorer ,
Dec 11, 2018 Dec 11, 2018

how would you make a prompt alerting you that hidden layers have been found

and if you press ok they will be deleted if you press cancel it will close out the script with no change?

Translate
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
Contributor ,
Dec 11, 2018 Dec 11, 2018

This script searches for invisible layers and asks the user if they should be removed.

doc = app.activeDocument;

invisibleLayers = new Array;

layers = doc.layers;

//collect invisible layers

for (i = 0; i < layers.length; i++)

    {

        if (layers.visible == false)

            {

                invisibleLayers.push(layers);

            }

    }

//when no invisible layers are found prompt alert and exit

if (invisibleLayers.length == 0) {alert("No invisible layers have been found."); exit();}

//when invisible layers are found ask user if they should be removed

if (invisibleLayers.length > 0)

    {

        x = confirm(invisibleLayers.length + " invisible Layers have been found.\nDo you want to delete them?");

    }

//exit script when users clicks "no"

if (x == false) {exit();}

//remove layers when user clicks "yes"

for (i = invisibleLayers.length-1; i >= 0; i--)

    {

        invisibleLayers.remove();

    }

Translate
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
Explorer ,
Dec 14, 2018 Dec 14, 2018

thank you....this was working great until we encountered mission creep

they changed the mission to now have this incorporated into a larger script so instead of closing out the script when the user presses no, it just passes thru and continues on with the next step of the script without doing anything.

i tried this...but get an error  (sometimes?) at if (x == true)

doc = app.activeDocument; 

invisibleLayers = new Array; 

layers = doc.layers; 

 

//collect invisible layers 

for (i = 0; i < layers.length; i++) 

    { 

        if (layers.visible == false) 

            { 

                invisibleLayers.push(layers); 

            } 

    } 

 

//when no invisible layers are found prompt alert and exit 

//*****if (invisibleLayers.length == 0) {alert("No invisible layers have been found."); exit();} 

 

//when invisible layers are found ask user if they should be removed 

if (invisibleLayers.length > 0) 

    { 

        x = confirm(invisibleLayers.length + " invisible Layers have been found.\nDo you want to delete them?"); 

    } 

 

//exit script when users clicks "no" 

if (x == true)

for (i = invisibleLayers.length-1; i >= 0; i--) 

    { 

        invisibleLayers.remove(); 

    }

Translate
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
Contributor ,
Dec 14, 2018 Dec 14, 2018

Hi Js1212,

I think the problem is that the loop needs to be put in curly brackets

//if user clicks "yes": delete invisible Layers

if (x == true)

     {

         for (i = invisibleLayers.length -1; i >= 0; i--)

                {

                    invisibleLayers.remove();

                }

     }

Greetings

Translate
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 14, 2018 Dec 14, 2018

Put the following statement before the if where you are showing the prompt

var x = false

The issue is that in case there is no layer which is invisible, x remains undefined and the condition x == true results in error

-Manan

Translate
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 14, 2018 Dec 14, 2018

Hi,

The curly brackets are not needed as by default one statement is covered

-Manan

Translate
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
Explorer ,
Dec 17, 2018 Dec 17, 2018
LATEST

much appreciated....you made me look like a rockstar!!!!

Translate
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
New Here ,
Sep 10, 2008 Sep 10, 2008
Perfect!

Thank you both.
Richard
Translate
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 ,
Sep 10, 2008 Sep 10, 2008
Hi Richard,<br /><br />I know the reason why it didn't work. It's because the length property of the layers array changes each time you remove a layer, so you will be missing the last couple of layers after each removal.<br />You can either use Gerald's script, or add a variable to hold the length property in mine. So you change it to:<br /><br />myLength = app.activeDocument.layers.length;<br />for (i=0; i<myLength; i++) { ... }<br /><br />I suspected this might happen, but wasn't sure. Anyway, adding the variable solves the problem.
Translate
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
New Here ,
Sep 10, 2008 Sep 10, 2008
Thank you all
Translate
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