Skip to main content
tpk1982
Legend
August 28, 2017
Question

Remove unused layers include masterpage items

  • August 28, 2017
  • 1 reply
  • 1597 views

Hi All,

Good day. I am trying to remove the empty layers. The script tags master page elements as unused and therefore deletes all of them resulting to a blank file.

My code:

function mylayer()

{

var flag=0;

wasteLayers=[];

wastename=[];

var myDoc = app.activeDocument;

var layers = app.activeDocument.layers;

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

    if (layers.locked){

       layers.locked=false;

     }

   }

if(layers.length>1){

for(var i=layers.length-1;i>=0;i--){

  if((layers.pageItems.length==0)){

      wasteLayers.push(layers);

      wastename.push(layers.name)

        layers.remove();

        flag=1;

  }

}

}

if(flag==1){

alert(wasteLayers.length + " unused layers deleted"+"\n");

}

else

{

   alert("No Unused layers Present in the document")

}

}

mylayer();

I found the interesting solution (Thanks to Jump_Over) from the below link. But it removes the item if it presents in pasteboard. How to ignore this?

IDCS6 MACOSX JS: "remove unused layer" script doesn't see masters based on masters

Thanks,

K

This topic has been closed for replies.

1 reply

Inspiring
August 28, 2017

Interesting! Never ran into this problem.

I tested this out in AppleScript as sometimes the language can make a difference. Not. Working from a list of layers and checking for no page items, ignores a layer created in a master page based on a master page. So here is what I came up with in AppleScript (gets list of item layer for all page items then compares with list of all layers).

set layerList to {}

tell application "Adobe InDesign CC 2017"

  tell document 1

    set allItems to all page items

    set allLayers to id of layers

    repeat with i from 1 to length of allItems

      set thisLayer to item layer of item i of allItems

      if id of thisLayer is not in layerList then

        set end of layerList to id of item layer of item i of allItems

      end if

    end repeat

    repeat with i from 1 to length of allLayers

      if item i of allLayers is not in layerList then

        delete layer id (item i of allLayers)

      end if

    end repeat

  end tell

end tell

{layerList, allLayers}

Note: It also deleted the layer even when it was locked.

tpk1982
tpk1982Author
Legend
August 28, 2017

Hi S Hopkins,

Thank you so much for your script. Unfortunately I dont have Mac right now to text this script. I will check and let you know.

Thanks again.

K