3 Replies Latest reply on Apr 18, 2017 1:35 AM by alessiot25166504

    Hide some grouped layer with script

    alessiot25166504

      Hi all,
      in an InDesign files (version 2017) I have a layered structure of this type

       



      With a script I need to make invisible all levels that comply with these characteristics:
      - the level name must contain the string "imm"
      - the level must be grouped with a higher level to be selected

      In the reported case, the script must disable levels
      - rect_imm_01 (belongs to the level "group_01" which is selected)
      - rect_imm_02 (belongs to the level "group_02" which is selected)

      should not disable "rect_imm_03" because "group_03" group is not selected.

       

      This should be the end result.


      Thank you.
      Alex

        • 1. Re: Hide some grouped layer with script
          cbishop01 Level 2

          something like this will hide the text frame

            
          app.activeDocument.pageItems.item("rect_imm_01").visible = false;
          app.activeDocument.pageItems.item("rect_imm_02").visible = false;
          app.activeDocument.pageItems.item("rect_imm_03").visible = false;

          not sure if you like dialog windows but you can put these in a UI with a check boxes and the selected Checkbox will hide the desired text frame like below

           

          Screen Shot 2017-04-13 at 11.44.22 AM.pngScreen Shot 2017-04-13 at 11.45.17 AM.png

          If you want this way i'll post the Script ("overkill" script but you can add as many variable to this as you want.) Below.

          • 2. Re: Hide some grouped layer with script
            cbishop01 Level 2

            again there are a easier and less complex UI checkbox scripts out there but this is the one i had handy.

            
                var EventManager = function()    
                {    
                var o  = {}; 
                return {    
             addEventListener:function(name, handler) {    
                if ( this.hasEventListener (name, handler) ) return;    
                  o[name] = o[name] || [];    
             o[name][o[name].length]=handler; 
                  },    
             dispatchEvent:function(name, data) { 
                var n, i=0;     
                if ( !o[name] || !(o[name] instanceof Array) || !o[name].length ) return;    
                  n = o[name].length;    
                   for ( i = 0; i<n; i++ ) {    
             o[name][i](data);    
                }    
               },    
             removeEventListener:function(name, handler) {    
                 var newArray = [], n;    
                  if ( !this.hasEventListener ( name, handler ) ) return;    
                    n = o[name].length;   
                while ( n-- ) {    
                  if ( (o[name][n] !== handler) ) {    
                  newArray [ newArray.length ] = o[name][n];    
                  }    
                  } 
                  o[name] = newArray ;    
                  },   
             hasEventListener:function(name, handler) {    
                  var n; 
                if ( !o[name] || !(o[name] instanceof Array) || !o[name].length ) return false;    
                 n = o[name].length;     
                  while ( n-- ) if ( o[name][n] === handler ) return true;  
                  return false;    
                  },    
             expose:function(name){    
                  if ( name ) {    
                  if ( !o[name] || !(o[name] instanceof Array) || o[name.length] ) return "no events set";    
                  else return o[name].toSource();    
                  }    
                  else {    
                  return o.toSource();    
                  }    
                  }    
                  }    
                };   
              
                var main = function() {    
                var ev = new EventManager(),    
                    u, actions = {};    
                
                var CBX = function(p, label, v, callback) {    
                var cbx = p.add('checkbox', undefined, label);   
                    cbx.onClick = function() {  
             ev.dispatchEvent ( (cbx.value? "ADD_ACTION":"REMOVE_ACTION"), {label:label, callback:callback} );  
                }  
                return cbx;    
              };    
                   
                var myResult;
                var w =new Window ('dialog {text: " ", orientation: "column", alignChildren:["fill","fill"],   properties: {closeButton:   false}}');
                
                    w.main =w.add ('group {preferredSize: [300, 200], alignChildren: ["left","fill"]}');
                    w.stubs  = w.main.add ('listbox', undefined, ['','Hide']); 
             w.stubs.preferredSize.width  = 130;  w.tabGroup  = w.main.add ('group {alignment: ["Fill","Fill"], orientation: "stack"}');
                //~  1st Tab. 
                    w.tabs  =[];
                    w.tabs[0]= w.tabGroup.add ('group');
                    w.tabs[0].add ('statictext {text: ""}');
                    w.tabs[0].add ('panel');
             with(w.tabs[0])  {
                 w.tabs[1]= w.tabGroup.add ('group');
             w.tabs[1].add('statictext {text: "Hide"}');
                w.tabs[1].add ('panel');  
               
                with(w.tabs[1])
                
              var p1, p2, p3, p4, p5; 
                    p1 = w.tabs[1].add("group{orientation: 'row', alignment: ['fill', 'top']}");
                    p2 = w.tabs[1].add("group{orientation: 'row', alignment: ['fill', 'top']}");
                    p3 = w.tabs[1].add("group{orientation: 'row', alignment: ['fill', 'top']}");
                  new CBX ( p1, 'rect_imm_01', false, cb1CallBack );  
                  new CBX ( p2, 'rect_imm_02', false, cb2CallBack );  
                  new CBX ( p3, 'rect_imm_03', false, cb3CallBack ); 
                   for(
                var i = 0;
                    i < w.tabs.length;
                    i++){
             w.tabs[i].orientation ='column';
             w.tabs[i].alignChildren ='fill';
             w.tabs[i].alignment =['fill','fill'];
             w.tabs[i].visible =false;
             }
             w.stubs.onChange  = showTab;
                function
                    showTab ()
                 {
             if(w.stubs.selection !==null){for(var i = w.tabs.length-1; i >= 0;
                i--)
                 {    
                w.tabs[i].visible =false;
             }
             w.tabs[w.stubs.selection.index].visible =true;
             }
             }
                w.onShow  =function()
                {
                w.stubs.selection  = 1;
                showTab;
             }
            }
            
              ev.addEventListener ( "ADD_ACTION", function(data) { 
                    ev.actions = ev.actions || {};  
             ev.actions[data.label] = data.callback; 
              });  
              
             ev.addEventListener ( "REMOVE_ACTION", function(data) {  
                    if ( !ev.actions) return;  
                    delete ev.actions[data.label];  
              });  
            
            // w.buttons  = w.add   
             var btn;
                w.buttons  = w.add ('group {alignment: "right"}');
                btn= w.add('group {alignment: "right"}');
                btn= w.buttons.add('button',u, 'OK' );  
                btn= w.buttons.add('button' , u, 'Cancel')
                btn.onClick = function () { 
                w.close(1); 
                } 
              
                 
             ev.addEventListener ( "ADD_ACTION", function(data) {    
                      ev.actions = ev.actions || {};    
                      ev.actions[data.label] = data.callback;    
                  });    
                    
             ev.addEventListener ( "REMOVE_ACTION", function(data) {    
                  if ( !ev.actions) return;    
                     delete ev.actions[data.label];    
                  });    
                  
                    ev.target = app.properties.activeDocument;  
                  
                  //add a listener so any event of type "DEFINE_TARGET" dispatched set ev.target value;  
             ev.addEventListener ( "DEFINE_TARGET", function(data) {     
                  
                    ev.target = data? app : app.properties.activeDocument; 
                  });    
            
                  if ( w.show() == 1 && ev.actions) {      
                
              //looping through all properties of the ev.actions object 
              //which are functions    
                
              for ( prop in ev.actions ) {      
             ev.actions[prop](ev.target);      
              }      
              }      
                
            
                function cb1CallBack(target) {    
              
             app.activeDocument.pageItems.item("rect_imm_01").visible = false;
              
            }    
              
                function cb2CallBack(target) {
                    app.activeDocument.pageItems.item("rect_imm_02").visible = false;
            
                  }
               
                function cb3CallBack() {
             app.activeDocument.pageItems.item("rect_imm_03").visible = false;
                    }  
                function cb4CallBack() {}
                function cb5CallBack(target) {
                
                
                    } 
                
                w.close(true);
                
            
             // text searh Parameters
            
                }
                   if ( !app.properties.activeDocument ) {  
                  alert('you need an open document at least');  
            }  
            else {  
              main();  
            }
            
            
            • 3. Re: Hide some grouped layer with script
              alessiot25166504 Level 1

              Thank you, the script is magnificent.
              But I was looking for something more automated. I have a document with four pages and each page there are at least 20 levels such as those that I have called "Group_" in the sample file.
              That's why I want to write something more automated which must work directly only with "Group_" that are selected.
              Also because I have posted a simpler structure, as an example, compared to the structure that actually I have in the document.
              Thank you.
              Alex