Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

Script to Automate Deletion of Specific Layers

Return to Member Forum

  • Author
    Posts
    • #78722
      Alex Lee
      Member

      Hi all,

      I’ve spent the last 30 minutes searching online, but have come up empty; I’m hoping to find an example script with some code that deletes a specifically named layers in an InDesign template.

      I’ve found some code that automatically deletes hidden layers, which is good, but I’d like to delete layers with specific names.

      In a team of 10 designers, my intention is we use some print based templates that mostly share the same elements of the template, but there are multiple branches of the same basic template that deviate. In the past, we’ve created multiple templates, but that’s meant maintenance has been a pain, as has keeping consistency.

      Ideally, what I’d like to do is set up a single template, which includes layer groups for all our branches, but have a script that deletes unneeded layer groups for the other branches of use for the template.

      Is there an existing script out there I could use for reference and change the code for deleting specifcally named layers/groups of layers?

      Or is there another approach to this template issue I’m not aware about.

      – Alex

    • #78724

      Hi Alex,

      Have you tried something like this?

      tell application “Adobe InDesign CC 2014”
      set myDoc to front document
      tell myDoc
      delete layer “layer name” –change text in quotes to the name of the layer to delete
      end tell
      end tell

      Hope this helps!

      Trisha

    • #78734
      Loic Aigon
      Member

      Here you are:

      function removeLayers ( layersNamesArray ) {
      var d, ls, l, n, r, c = 0;
      if ( !app.documents.length
      || !layersNamesArray
      ||!(layersNamesArray instanceof Array) ) return;
      d=app.activeDocument;
      ls = d.layers;
      n = ls.length;

      r = new RegExp ( “^(“+layersNamesArray.join(“|”)+”)$”, “” );

      while ( n– ) {
      l = ls[n];
      if ( r.test ( l.name ) && n>0 ) {
      l.remove()
      c++;
      }
      }

      alert( c>0? “Removed “+c+” layers” : “Removed none layers” );
      }
      var arr = [“myName”,”yolo”];
      var u;
      app.doScript (“removeLayers(arr);”, u,u, UndoModes.ENTIRE_SCRIPT );

    • #78762
      Alex Lee
      Member

      Thanks Trisha and loicaigon!

      I’ll try out both (if you haven’t guessed, I’m completely new to scripting but hoping with an example to follow, it’ll just be down to specifying the layers I want to delete between different scripts).

      I’m hoping to complete my rather complex template in the next 3 weeks, and only then will I be ready to set up the scripts once all the layers are in place. I’ll keep everyone posted!

      – Alex

    • #79302
      Alex Lee
      Member

      It’s taken a little longer than expected, but my InDesign master tempate for this is now done.

      I’ve created a first script to hopefully get right, before rolling out to other formats. Needless to say, as I’m a scripting newbie, it doesn’t work :-)

      I’ve used the code sample supplied by ‘loicaigon’ earlier in this forum thread (although we’re on Macs, I felt using a platform agnostic script language would be best for future proofing), but when I run it (and check the .jsx’s syntax in The ExtendScript Toolkit) there’s an error on line 20. Here’s the complete code and I’ve separated line 20 out with paragraph breaks:

      //PopCultureTemplate.jsx
      //An InDesign JavaScript
      /*
      @@@BUILDINFO@@@ “PopCultureSlickTemplates.jsx” v1.0 November 2015
      */
      //This script will prepare any slick template format for use for Pop Culture/Anime specific use, including DVD, Blu-ray, Fatpack and 12 disc Fatpack formats.

      //For more information on InDesign scripting, go to https://www.adobe.com/products/indesign/scripting/index.html
      //Or visit the InDesign Scripting User to User forum at https://www.adobeforums.com.
      //
      main();
      function removeLayers ( layersNamesArray ) {
      var d, ls, l, n, r, c = 0;
      if ( !app.documents.length
      || !layersNamesArray
      ||!(layersNamesArray instanceof Array) ) return;
      d=app.activeDocument;
      ls = d.layers;
      n = ls.length;

      r = new RegExp (“^(“+layersNamesArray.join(“|”)+”)$”, “” );

      while ( n– ) {
      l = ls[n];
      if ( r.test ( l.name ) && n>0 ) {
      l.remove()
      c++;
      }
      }
      alert( c>0? “Removed “+c+” layers” : “Removed none layers” );
      }
      var arr = [“TV LIcensor Logos and Inner Slick Template Layout Elements”,”Disc Number Icon Back (Palace / No QR Code – Black)”,“Disc Number Icon Back (Palace / No QR Code – White)”,“Specs Box Cartoon Network/Adult Swim (White)”,“Specs Box Cartoon Network/Adult Swim (Black)”,“Specs Box (Palace / No QR Code – White)”,“Specs Box (Palace / No QR Code – White)”,“Specs Box (Palace / No QR Code – Black)”,“Specs Box Films/TV (Black)”,“Specs Box Films/TV (White)”,“Studio Ghibli Layout Elements”,“Adult Swim Layout Elements”,“Cartoon Network Layout Elements (Madman Logo Not present on front)”,“Directors Suite Layout Elements”,“Palace Layout Elements”,“SBS Template Elements”];
      var u;
      app.doScript (“removeLayers(arr);”, u,u, UndoModes.ENTIRE_SCRIPT );

      I thought it could be because it was using different quote marks than expected, but I tried some variations and it’s no good. I don’t know what else to try in terms of syntax that Javascript will understand.

      Help me Scripti-Wan-Kenobi(s), you’re my only hope!

      – Alex

      • #79464
        Loic Aigon
        Member

        Hi,

        Sorry for the laaaaate reply but was deep into work. The issue you got at line 20 was that it should have been n with two minus characters like n– You only set n-

        For the rest, it should work. I would have sticked to that code as it tends to avoid issues your final code won’t prevent.

    • #79345
      Alex Lee
      Member

      Hi all,

      I enlisted the help of our IT guru here at work, and he’s substantially changed the code to make it work.

      Where ‘var arr = [“xxx”,”xxx”];

      is where you specify the layers you want to delete.

      Now trying to find a definitive removed unused paragraph, character, object styles and swatches to add to this script…

      //Films_Slick Templates.jsx
      //An InDesign JavaScript
      /*
      @@@BUILDINFO@@@ “Films_Slick Templates.jsx” v1.0 November 2015
      */
      //This script will prepare any slick template format for use for Films specific use, including DVD, Blu-ray, Fatpack and 12 disc Fatpack.
      //Huge Thanks to Tom Byrne for making this code work, originally provided on indesignsecrets.com user loicaigon.

      //For more information on InDesign scripting, go to https://www.adobe.com/products/indesign/scripting/index.html
      //Or visit the InDesign Scripting User to User forum at https://www.adobeforums.com.
      //
      function removeLayers ( layersNamesArray ) {
      var d, ls, l, n, r, c = 0;
      if ( !app.documents.length || !layersNamesArray ||!(layersNamesArray instanceof Array) )
      return;

      d=app.activeDocument;
      ls = d.layers;

      for (var i = 0; i < ls.length; i++)
      {
      for (var j = 0; j < arr.length; j++)
      {
      l = ls[i];
      if (l.name == arr[j])
      {
      l.remove();
      c++;
      //Reset the loop and start again
      i = 0;
      break;
      }
      }
      }
      alert( c>0? “Removed “+c+” layers” : “Removed none layers” );
      }
      var arr = [“TV Licensor Logos and Inner Slick Template Layout Elements”,”Anime Licensor Logos (if not included, obtain logo from raw assets or previously done slick)”,”Disc Number Icon Back (Palace / No QR Code – Black)”,”Disc Number Icon Back (Palace / No QR Code – White)”,”Country of Origin Flags (Palace / No QR Code White Text)”,”Country of Origin Flags (Palace / No QR Code Black Text)”,”Specs Box Cartoon Network/Adult Swim (White)”,”Specs Box Cartoon Network/Adult Swim (Black)”,”Specs Box (Palace / No QR Code – White)”,”Specs Box (Palace / No QR Code – Black)”,”Specs Box Anime/Pop Culture (Black)”,”Specs Box Anime/Pop Culture (White)”,”Studio Ghibli Layout Elements”,”Adult Swim Layout Elements”,”Cartoon Network Layout Elements (Madman Logo Not present on front)”,”Directors Suite Layout Elements”,”Palace Layout Elements”,”SBS Template Elements”];
      removeLayers(arr);

Viewing 5 reply threads
  • You must be logged in to reply to this topic.
Forum Ads