I was informed the if-statement is incorrect. I can't find the syntax error ?

var curDoc = app.activeDocument;
var selall = app.executeMenuCommand("selectall");
var fillrmv = curDoc.pathItems[0].filled = false;
if (app.activeDocument.pathItems[0].filled = true) {
        for (i=0;i>fillrmv;i++) {
        curDoc.pathItems[0].filled = false;
    };
};

Recommended Answers

All 18 Replies

if (app.activeDocument.pathItems[0].filled = true) {

A single = is used to assign a value, a double == or triple === is used to check a value. So basically in this statement you are assigning true to filled instead of checking whether filled is true.

Also, in your for loop for (i=0;i>fillrmv;i++) { you are saying to continue as long as i is larger than fillrmv, but in var fillrmv = curDoc.pathItems[0].filled = false; you're assigning a boolean value to fillrmv. This is where "silly" JavaScript kicks in. Under water it's assigning truth values to things. If you ask JavaScript "is 1 larger than false" it will say "yep". If you ask it "is 1 smaller than false" it will say "nope". (because '0' is 'falsy' and '1' is 'truthy' and true is "larger than" false)

For a good laugh I recommend this (slightly dated) video about some funny quirks JavaScript has.

For an explanation on why JavaScript is so quirky I strongly suggest this video.

One of the things it explains is why, in JavaScript, this:

function() {

}

...is the only correct way and this:

function()
{

}

is a wrong way.

And many more things that are just a little different in JavaScript.

What I'm doing wrong in terms of my for-loop I assume is explain in those videos ?

I understand what is wrong considering the for loop should run though the variable fillrmv and set each object in the array to false but that is not happening.

Further...

The if statement expects an expression that evaluates to true or false. The statement

if (app.activeDocument.pathItems[0].filled == true)

could be better written as

if (app.activeDocument.pathItems[0].filled)

Not so much what you're doing wrong is explained in the videos but why it doesn't blow up when you do that. Because in most languages it will blow up and won't let you do that.

What I'm doing wrong

I think the start of your issue is this bit:

var fillrmv = curDoc.pathItems[0].filled = false;

It's pretty much always a bad idea to assign variables this way. In JavaScript it's sort of ok, because here there are no references, but in a lot of languages this would mean that fillrmv is a reference to filled which is set to false. If you then changed filled to true you'd expect fillrmv to stay false (and in JavaScript it does) but it would actually refer to filled and be true.

and set each object in the array to false

In order to do that you'd set fillrmv to the length of the pathItems array. Then inside your for loop you would say something like:

curDoc.pathItems[i].filled = false;

You can retrieve the length of an array by calling the length property. For instance:

var arr = ['one', 'two', 'three'];
for (var i = 0; i < arr.length; i++) {
    arr[i] = 'changed_'+i;
}

Edit: Keeping my answer slightly on the vague side because it looks a bit like homework, if it's not let us know.

Adding .length doesn't loop though each pathItem and set it's value to true ?

if (app.activeDocument.pathItems[0].filled.length == true) {

        for (i=0;i>fillrmv;i++) {
        curDoc.pathItems[0].filled == false;

    };
};
var fillrmv = curDoc.pathItems[0].filled = false;

It's pretty much always a bad idea to assign variables this way. In JavaScript it's sort of ok, because here there are no references, but in a lot of languages this would mean that fillrmv is a reference to filled which is set to false. If you then changed filled to true you'd expect fillrmv to stay false (and in JavaScript it does) but it would actually refer to filled and be true.

This was confusing, even if I change filled=true, it really is filled=true by terms of the variable fillrmv ?

I must assign filled a boolean value as in filled=true to filled.

Member Avatar for diafol

This...

if (app.activeDocument.pathItems[0].filled.length == true) {

        for (i=0;i>fillrmv;i++) {
        curDoc.pathItems[0].filled == false;

    };
};

Looks as if it's trying to check the same item for a false value (not assigning) for a weird number of times. The for loop is usually (start value , while condition, increment by) I think your construct is trying to swap "while" for "until". Or perhaps I'm missing something obvious.

If the total of all the path items is true, considering there are more then one. For each path item iterate though and set the filled from true to false.

Correct me but isn't that what the code is doing ? ;-)

Correct me but isn't that what the code is doing ?

It's not.

if (app.activeDocument.pathItems[0].filled.length == true) {

This is saying, if the length of the "array" of the filled property (which might be an array according to JS, it doesn't know better) equals true, then execute the statement. If that length happens to be 1 (which it is because it's not an array and only has 1 element) the statement will be true because in JS 1 equals true. It basically becomes if (1 == true) {.

for (i=0;i>fillrmv;i++) {

This literally means: start with a variable i that equals 0. As long as i is larger than fillrmv (which is a boolean value mind you) then keep increasing i with 1.

var fillrmv = curDoc.pathItems[0].filled = false;

Here you are saying that fillrmv should be set to the value of curDoc.pathItems[0].filled which should be set to false;
So you are setting the first element's filled property in curDoc.pathItems to false. But fillrmv is also set to false.

I think the main issue here is that JavaScript is very "relaxed" when it comes to variables. In other languages you would get errors that help you out, but JS allows for a lot of weird things. You normally can't put a boolean in a for loop, comparing a number to a boolean usually results in a mismatch. However, JS just assigns a truth value to the number (using vague rules that are very difficult to understand, hence the video) and then automagically continues with the rest of the code.

Anyway, what I think you're looking for is this:

var curDoc = app.activeDocument;
var selall = app.executeMenuCommand("selectall");

var items = curDoc.pathItems;
for(var i = 0; i < items.length; i++) {
    items[i].filled = false;        
}

This will set all the pathItems's filled property to false.

Edit: you can avoid a bit of hassle by using the triple === because then it looks at types as well. In other words 1 == true will return true but 1 === true will return false.

var fillrmv = curDoc.pathItems[0].filled = false;

The following line gives me a, undefined is not an object error ?

fillrmv[i].filled=false;

I understand what I probably have done, dumb mistake; especially I knew there was a better way then what I had in this former line;

I don't think you need fillrmv at all. Unless you want to store the .length in there or something.

var curDoc = app.activeDocument;

var selall = app.executeMenuCommand("selectall");
var fillrmv = curDoc.pathItems[0];

        for (i=0;i>fillrmv.length;i++) {
        fillrmv[i].filled=false;
        };

This code produces no error but doesn't iterate though each path and set its fill to false ?
pathItem is a property, if that helps ?

Change var fillrmv = curDoc.pathItems[0]; to var fillrmv = curDoc.pathItems; because you want it to be all the items and not just the first one. And change for (i=0;i>fillrmv.length;i++) { to for (i=0;i<fillrmv.length;i++) { because you want it to run for all the items (< instead of >).

commented: Patience +1 :) +15
    fillrmv[i].filled=false;

I now get an error that the following line is not an object;

What is the content of fillrmv[i] ? Maybe it doesn't have a property filled.

Is this scripting for Adobe Illustrator btw? Because I have no idea where you get things like app.activeDocument.pathItems from.

You can find the properties I list and the method here.

I don't know why it does that. You can try checking the content of pathItems and see if it actually contains anything, but I don't know if you can do that from within the program you're writing the script in. I have no experience in writing scripts with Adobe.

@Traevel I'm going to ask this at the Adobe Forum and update as to what the problem was. When you brought to my attention a few code changes, I had full confidence it was going to work.

@Traevel - There was nothing wrong with the code, I was getting the wrong class. Thank you for help on this particular problem ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.