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

And or statements

Contributor ,
Sep 03, 2014 Sep 03, 2014

Copy link to clipboard

Copied

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.strokeColor = CC && docRef.pathItems.width < 7 || docRef.pathItems.height < 7) 

{  

  docRef.pathItems.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.

TOPICS
Scripting

Views

803
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

correct answers 1 Correct answer

Community Expert , Sep 05, 2014 Sep 05, 2014

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

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

with this:

if(thisItem.stroked == true && thisItem.strokeColor.typename == "SpotColor" && thisItem.strokeColor.

...

Votes

Translate
Adobe
Community Expert ,
Sep 03, 2014 Sep 03, 2014

Copy link to clipboard

Copied

Hi djbgraphicdesign,

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

One way could be:

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

another way with nested if clauses:

if(docRef.pathItems.strokeColor = CC )  {

    if(docRef.pathItems.width < 7 || docRef.pathItems.height < 7)  {

(Do not forget the closing parentheses.)

Have fun

Votes

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
Valorous Hero ,
Sep 04, 2014 Sep 04, 2014

Copy link to clipboard

Copied

Shouldn't all those be double equals? ==

And I believe you can group them:

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

Votes

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
Community Expert ,
Sep 04, 2014 Sep 04, 2014

Copy link to clipboard

Copied

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.

Votes

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
Contributor ,
Sep 04, 2014 Sep 04, 2014

Copy link to clipboard

Copied

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.strokeColor == CC)

{      

docRef.pathItems.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.

Votes

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
Community Expert ,
Sep 04, 2014 Sep 04, 2014

Copy link to clipboard

Copied

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.

Votes

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
Valorous Hero ,
Sep 04, 2014 Sep 04, 2014

Copy link to clipboard

Copied

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;

     if(thisItem.stroked == true && thisItem.strokeColor.spot.name == "CutContour"){

          if(thisItem.width > maxWidth || thisItem.height > maxHeight){

               thisItem.remove();

          }

     }

}

Votes

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
Community Expert ,
Sep 05, 2014 Sep 05, 2014

Copy link to clipboard

Copied

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

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

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

Votes

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
Valorous Hero ,
Sep 05, 2014 Sep 05, 2014

Copy link to clipboard

Copied

Touché! I did not think of that one.

Votes

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
Contributor ,
Sep 07, 2014 Sep 07, 2014

Copy link to clipboard

Copied

LATEST

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!

Votes

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