9 Replies Latest reply on Sep 7, 2014 7:23 PM by djbgraphicdesign

    And or statements

    djbgraphicdesign Level 1

      Trying to delete everything that has a certain stroke color and *either* has a width or hieght of less than 7.

       

      var docRef = app.activeDocument;

      var selectedObjects = docRef.selection;

      var CC = app.activeDocument.swatches.getByName ("CutContour").color;

      for(i = docRef.pathItems.length-1; i >= 0; i--){ 

      if(docRef.pathItems[i].strokeColor = CC && docRef.pathItems[i].width < 7 || docRef.pathItems[i].height < 7) 

      {  

        docRef.pathItems[i].remove();

       

       

      I can get it to work for just the stoke color, and the width or height, but not all 3 together.

       

      Thanks in advance, lots of helpful people on here and I really appreciate it.

        • 1. Re: And or statements
          pixxxel schubser Level 5

          Hi djbgraphicdesign,

          there are several ways to do this. (I hope, I understand you right.)

           

          One way could be:

          if(docRef.pathItems[i].strokeColor = CC && docRef.pathItems[i].width < 7 || docRef.pathItems[i].strokeColor = CC && docRef.pathItems[i].height < 7) {
          
          

           

           

          another way with nested if clauses:

          if(docRef.pathItems[i].strokeColor = CC )  {
              if(docRef.pathItems[i].width < 7 || docRef.pathItems[i].height < 7)  {
          
          

          (Do not forget the closing parentheses.)

           

          Have fun

           

          • 2. Re: And or statements
            Silly-V Adobe Community Professional

            Shouldn't all those be double equals? ==

             

             

            And I believe you can group them:

             

             

            1. if(docRef.pathItems[i].strokeColor == CC && (docRef.pathItems[i].width < 7 ||  docRef.pathItems[i].height < 7)){ 
            • 3. Re: And or statements
              pixxxel schubser Level 5

              Yes, of course. This should be double equals.

              (I copied the line number 5 in djbgraphicdesigns post and changed only the ands and ors – embarrassing).

               

               

              Unfortunately I can my posting now no longer change.

              • 4. Re: And or statements
                djbgraphicdesign Level 1

                Well I thought it was working earlier, but it is not, so sorry if you got a notification for my reply and then it wasn't there. What I did not realize is that I am having trouble deleting the items with the stroke color. I was unable to do it before, I thought I was but I didn't realize it was deleting everything regardless of the stroke color. So for now I am ignoring the width and height until I figure out what I am doing wrong here. I added the extra equal sign and now it's not deleting anything.

                 

                var docRef = app.activeDocument;

                var CC = docRef.swatches.getByName("CutContour").color;

                 

                for(i = docRef.pathItems.length-1; i >= 0; i--){

                if(docRef.pathItems[i].strokeColor == CC)

                {      

                docRef.pathItems[i].remove();

                }

                }

                 

                Not sure what I am doing wrong here. CutContour is the name of the swatch and it is in the swatches pallet. It is also a spot color so idk if that changes everything. Thanks a lot for your help guys I am still new to javascript and even tho it is not working yet I am learning a little bit about syntax and how it is used.

                • 5. Re: Re: And or statements
                  pixxxel schubser Level 5

                  Sorry, AND and OR works very well, but your script cannot work.

                  • CC gives you SpotColor with a RGB or a CMYK color value (and in script: working with spot color is a very hard working!)

                  Please check this:

                  var docRef = app.activeDocument;
                  var CC = app.activeDocument.swatches.getByName ("CutContour").color;
                  alert("CC: " + CC)
                  
                  try {
                      alert("CC: " + CC.spot.spotKind);
                      var CC_R = Math.round(CC.spot.color.red);
                      var CC_G = Math.round(CC.spot.color.green);
                      var CC_B = Math.round(CC.spot.color.blue);
                  
                      alert("RGB_array  [" + CC_R + ", " + CC_G + ", " + CC_B + "]");
                  
                      }
                  catch (e) {
                      alert("CutContour isn't RGB Spotcolor swatch");
                      //var CC_M = Math.round(CC.spot.color.magenta);
                      // and so on
                      }
                  
                  

                   

                  That's why IMHO it is better to work with:

                  var CC = app.activeDocument.swatches.getByName ("CutContour");
                  alert("CC: " + CC.name)
                  

                   

                  And:

                  • the double equal sign compares the values, the simple one gives the value to the object.
                  • do you know that your digit "7" means: 7points ?
                  • you never gets the width and the height, because the first if clause always says before: strokeColor is != your CC

                   

                  Perhaps this helps you a little bit.

                  • 6. Re: Re: Re: And or statements
                    Silly-V Adobe Community Professional

                    So his (or her) process should be more like:

                     

                    var docRef = app.activeDocument;
                    var maxWidth = 7*72; //inches
                    var maxHeight = 7*72;
                    
                    for(var i=docRef.pathItems.length-1; i>-1; i--){
                         var thisItem = docRef.pathItems[i];
                         if(thisItem.stroked == true && thisItem.strokeColor.spot.name == "CutContour"){
                              if(thisItem.width > maxWidth || thisItem.height > maxHeight){
                                   thisItem.remove();
                              }
                         }
                    }
                    
                    
                    
                    • 7. Re: Re: Re: Re: And or statements
                      pixxxel schubser Level 5

                      Hi Silly-V,

                      not bad. But unfortunately this cannot work. (ONLY if all stroked objects are stroked with spot colors)

                      Otherwise the script fails.

                       

                      That's why: replace your line 7 - 10

                           if(thisItem.stroked == true && thisItem.strokeColor.spot.name == "CutContour"){  
                                if(thisItem.width > maxWidth || thisItem.height > maxHeight){  
                                     thisItem.remove();  
                                }  
                      
                      

                       

                      with this:

                      if(thisItem.stroked == true && thisItem.strokeColor.typename == "SpotColor" && thisItem.strokeColor.spot.name == "CutContour") {
                          if(thisItem.width < maxWidth || thisItem.height < maxHeight) {
                              thisItem.remove();
                              }  
                      

                       

                      djbgraphicdesign,

                      if there are no other hidden infos from you, then this should do the job. You only have to change the size of maxWidth and maxHeight with your own conversion factor (for your units).

                       

                      Have fun

                       

                      • 8. Re: Re: Re: Re: And or statements
                        Silly-V Adobe Community Professional

                        Touché! I did not think of that one.

                        • 9. Re: Re: Re: Re: And or statements
                          djbgraphicdesign Level 1

                          Thanks guys, sorry it took a while for me to respond it's been a crazy week, but the script works great! Very happy about it, thanks again!