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

Remove unused layers include masterpage items

Guide ,
Aug 28, 2017 Aug 28, 2017

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

1.3K

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
Enthusiast ,
Aug 28, 2017 Aug 28, 2017

Copy link to clipboard

Copied

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.

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
Guide ,
Aug 28, 2017 Aug 28, 2017

Copy link to clipboard

Copied

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

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
Guide ,
Aug 28, 2017 Aug 28, 2017

Copy link to clipboard

Copied

Hi S Hopkins,

It is working fine. But I wonder how to use this script for all version in Indesign. It is directing to cc 2017. I have no idea how the applescript working.

Also I have to include this applescript into my core javascript. Because this a part of the script.

Thanks,

K

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 ,
Aug 31, 2017 Aug 31, 2017

Copy link to clipboard

Copied

LATEST

Try this:

    tell application id "com.adobe.indesign"

When I switched from CS6 to CC 2017 I didn't have to change this at all.

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