1 Reply Latest reply on Jul 7, 2015 12:52 PM by Silly-V

    apply clipping mask to every top level group in document

    elDudereno Level 3

      Example file: https://drive.google.com/file/d/0BzEoJSYDhH_WcXhIalFRTk9qOGc/view?usp=sharing

       

      We can assume:

      • Grouped art on setups with a named die line.

      • All groups exist on 'Layer 1'

       

      I am looping through the groups and working with groups who's parent is 'layer'. I was looping through all the groups in the document, but when I created the clipping mask it creates a new group and screws up my loop. I am now looping only through the groups on 'Layer 1' and moving them to a new layer before doing the clipping mask. My script is breaking after the group has successfully moved to the new layer. I have a bunch of alerts in it for debugging.

       

      #target Illustrator
      
      
      var idocX = app.activeDocument;
      var li = idocX.layers;
      
      
      function meetTheParents(PageItem){
        if (PageItem.parent.typename = "Layer"){
        return "Layer";
        } else if (PageItem.parent.typename = "GroupItem"){
        return "Group";
        } 
      }
      
      function isLayerFree(layer){
        if (layer.locked == false && layer.visible == true){
        var gpa = layer.parent;
        isLayerFree(gpa);
        return true
        } else {
        return false
        }
      }
      
      
      var trLyr = li.add();
      trLyr.name = "TRASH";
      var l1 = li.getByName('Layer 1');
      for (j = l1.groupItems.length -1; j >= 0; j--) {
        app.executeMenuCommand ('deselectall');
        var iGroup = l1.groupItems[j];
        if (iGroup.hidden==false && iGroup.locked==false) {
        if (meetTheParents(iGroup) == "Layer"){
        if (isLayerFree(iGroup.parent)){
        iGroup.move(trLyr, ElementPlacement.PLACEATBEGINNING);
        // set die variable to die in current selection
        alert ("Group " + j + " moved to trash layer");
        alert ("trash layer path items length = " + trLyr.pathItems.length); // I'm getting '0' here even though I just moved items into it.
        var die = trLyr.pathItems.getByName('proofLine'); // script breaks here
        alert ("Die identified for this group");
      
        die.move(trLyr, ElementPlacement.PLACEATBEGINNING);
        for (a = 0; a < trLyr.pageItems.length; a++){
        var thing = trLyr.pageItems[a];
        if (thing.hidden==false && thing.locked==false){
        thing.selected = true;
        }
        }
        var sel = idocX.selection; // get selection
        alert (sel.length + " items in current selection.");
      
        // make clipped group of die with art inside
        if (sel.length==2) {// continue only if 2 objects are selected
        var igroup = idocX.groupItems.add(); // add a group that will be the clipping mask group
      
        var imask = sel[0]; // the mask is the object on top
        var ipath = sel[1]; // the "drawn object" is at the bottom
      
        var idup = imask.duplicate(); // duplicate the mask, to later get the fill and stroke to apply to the clipping path
      
        ipath.move (igroup, ElementPlacement.PLACEATBEGINNING); // add both path to the group
        imask.move (igroup, ElementPlacement.PLACEATBEGINNING);
      
        imask.clipping = true; // make the mask the clipping path
        igroup.clipped = true; // clip the everything in the group to the clipping mask
      
        if (idup.filled == true){
        imask.fillColor = idup.fillColor; // add fill color, same as the dup
        }
        imask.stroked = true; // stroke the mask
        imask.strokeWidth = idup.strokeWidth; // add stroke width, same as the dup
        imask.strokeColor = idup.strokeColor; // add stroke color, seme as the dup
      
        idup.remove(); // remove the duplicate
        }else { // show this message if other than 2 objects are selected
        alert("Select both, the Mask and the object that needs to be 'inside'.\rThe Mask needs to be the top most object.");
        }
        app.executeMenuCommand ('deselectall');
        igroup.move (l1, ElementPlacement.PLACEATBEGINNING);
        }
        }
        }
      }
      trLyr.remove();
      

       

      Inside my larger script it breaks silently, in this example script it gives me a "no such element". There should be such an element, I just put it there. After the script closes, the group has been moved, meaning that the named path is right where it should be.

        • 1. Re: apply clipping mask to every top level group in document
          Silly-V Adobe Community Professional

          Let's see if this will work:

           

          #target illustrator
          function test(){
              if(app.documents.length < 1){
                  return;
              }
              var doc = app.activeDocument;
              var lr = doc.layers[0];
              for(var i=0; i<lr.groupItems.length; i++){
                  var thisGroup = lr.groupItems[i];
                  doc.selection = null;
                  thisGroup.selected = true;
                  for(var j=0; j<doc.pageItems.length; j++){
                      var thisItem = doc.pageItems[j];
                      if(thisItem.selected){
                          if(thisItem.typename == "PathItem" && thisItem.name == "proofLine"){
                              thisGroup.clipped = true;
                              thisItem.clipping = true;
                          }
                      }
                  };
              };
          }
          
          
          test();