for loop bug?

Discussion of actual or possible Photoshop Scripting Bugs, Anomalies and Documentation Errors

Moderators: Tom, Kukurykus

infernosnow

for loop bug?

Post by infernosnow »

Code: Select allvar docRef=app.activeDocument
preferences.rulerUnits = Units.PIXELS;

for(o=0;o<docRef.height;o++)
{
   for(k=0;k<docRef.width;k++)
   {
      alert("x: "+k+" y: "+o+" doc width: "+docRef.width+" doc height: "+docRef.height);
   }
}

this loops through the coordinates of every pixel, however for some reason it goes beyond what it should. If you have a 3x14 pixel document, the top left pixel is 0,0 then the next pixel is 1,0 and then 2,0 and then you bump down to the next row of pixels: 0,1 and 1,1 and 2,1 etc.... Since your image is only 3 pixels wide, your x coordinates will always be 0, 1, or 2. The y coordinates should work the same way, but they don't. It cycles from 0 to 14 when it should only cycle to 13. It should be noted that I am using < in the for loop (both of them actually) so the for loop should not execute if o is equal to the doc height, which is 14. But it DOES execute as the alert() will show. You will see that o reaches 14 when it shouldn't.
Mike Hale

for loop bug?

Post by Mike Hale »

Which version of Photoshop are you using? Your code works as expected with CS2/WinXP. That is to say the alerts go from 0 to 13.

Mike
infernosnow

for loop bug?

Post by infernosnow »

CS3 version 10.0

Even with this single for loop, it goes to 14:

Code: Select allfor(o=0;o<docRef.height;o++)
{
   alert("y: "+o+" doc height: "+docRef.height);
}
infernosnow

for loop bug?

Post by infernosnow »

here's another interesting fact: If I change docRef.height in the for loop to just 14, it works. It goes from 0 to 13. It seems to be reading docRef.height as 15 instead of 14 for some reason.
Mike Hale

for loop bug?

Post by Mike Hale »

My guess is that it is a UnitValue issue.

Try docRef.height.as('px')

Mike
infernosnow

for loop bug?

Post by infernosnow »

still doesn't work. Made a video showing exactly what happens:

http://www.youtube.com/watch?v=lnKDrgezigc
Mike Hale

for loop bug?

Post by Mike Hale »

I still think it's a UnitValue issue. I think something strange happens when the loop test the interger stored in 0 with the the UnitValue stored in docRef.height I can't explain exactly what happens or why it doesn't effect docRef.width in this case.

But if you force the converstion with parseInt(docRef.height) so the loop is testing two intergers it works in CS3 as expected.

Mike
infernosnow

for loop bug?

Post by infernosnow »

parseInt(docRef.height) does make it work, so that's good. Perhaps I'll have to start using that all the time just to be safe. I wonder why it +1's .height without it though. Hmmm....
xbytor

for loop bug?

Post by xbytor »

Assuming that you're preferences are set to PIXELS, docRef.height.value is the recommended way of getting the value out of a UnitValue object. Only do the parseInt() if you have to worry about portability back to CS.

-X
Mike Hale

for loop bug?

Post by Mike Hale »

xbytor wrote:docRef.height.value is the recommended way of getting the value out of a UnitValue object.

Perhaps so, but docRef.height.value still loops to 14 when it should stop at 13.

docRef.height.value displays as 14 but when o=14 in the loop o<docRef.height.value test true and the loop continues when it should have stopped.

Mike