Skip to main content
Participant
November 1, 2011
Answered

selection.bounds operation?

  • November 1, 2011
  • 3 replies
  • 6201 views

I have this simple jsx script where I want to get the bounds of a selection:

try

    {

        var s = app.activeDocument.selection.bounds

        alert(s.toSource())

    }

catch(e)

    {

        alert("No selection")

    }

It does detect that there is a selection present but does not return that array of x,y coordinates that bounds the selecton.

The alert displays [({}),({}),({}),({})]

all elements of the array are determined to be undefined.

Any help is appreciated.

Thanks!

Ray

This topic has been closed for replies.
Correct answer raybeau528

I found the solution to the problem in this forum using this:

var selectionXY = [ app.activeDocument.selection.bounds[0].as('px'), app.activeDocument.selection.bounds[1].as('px') ];

Thanks to whoever provided the answer there!

Ray

3 replies

JJMack
Community Expert
Community Expert
November 1, 2011

I also think some versions of Photoshop  scripting selection bouns was either not posible or had a bug in it.

JJMack
Inspiring
November 1, 2011

I also think some versions of Photoshop  scripting selection bouns was either not posible or had a bug in it.

This is true. When the bounds property was first introduced (CS2) it didn't work. CS3+ should be fine.

Inspiring
November 1, 2011

try

    {

        var s = app.activeDocument.selection.bounds

        alert(s[0]+","+s[1]+","+s[2]+","+s[3]) // 0 and 1 are x and y for top left corner and 2 and 3 are x and y for bottom right corner.

    }

catch(e)

    {

        alert("No selection")

    }

Participant
November 1, 2011

Thanks. I'm pretty sure  tried that but kept getting 'undefined' empty sets.

What did work was s[1].as('px') ];

Inspiring
November 1, 2011

It depends on what your settings are. The .as('px') will always display the coordinates in pixels. If you are set to display rulers and type in pixels by default, that shouldn't be needed. Best to keep it just in case, though.

raybeau528AuthorCorrect answer
Participant
November 1, 2011

I found the solution to the problem in this forum using this:

var selectionXY = [ app.activeDocument.selection.bounds[0].as('px'), app.activeDocument.selection.bounds[1].as('px') ];

Thanks to whoever provided the answer there!

Ray