Get selection bounds in UXP?

Is there a way to get the selection bounds in UXP through the DOM, or if not then maybe through batchPlay?

I’m basically looking to replicate app.activeDocument.selection.bounds from extendscript. I just want to get get the bounds for [left,top,right,bottom] in pixel values for the current selection.

I don’t see anything in the documentation for this in the new UXP DOM. However, not sure if there is more capability than what has been documented. Also, is there a timeline for when more DOM features are going to be added?

I’m guessing there may be away to do this through batchPlay??? I’m starting to become comfortable with using batchPlay to command Photoshop to do stuff. However, to get feedback from Photoshop, it is still very confusing to me. I think that I need a “batchPlay for Dummies” handbook :slight_smile:

Try out Alchemist:
selection

I saw that but it show that is a rectangle object. I’m not trying to make a selection of a rectangle, I want to get the current bounds of a selection which can be any shape. So I’m looking to get feedback from Photoshop, not command it to create a selection.

So for example, if the selection was the of a person from using the Select Subject tool, I want to get the [left,top,right,bottom] bounds from the edges of that selection. Can that be done with the descriptors? If so, is there documentation somewhere that shows how to do that? The descriptors seem like kind of a “black box” to me. I really wish there was some more documentation and examples for how to build them from scratch and retrieve information from them.

OK, so I see the generated code from this now in Alchemist. And I can see in Alchemist that the values change and are the edge bounds I and looking for. However, I am confused about how to actually extract the 4 values for top, left, right, bottom from the returned result. This code creates a result variable. However, it isn’t a simple 4 number array. How do I extract the 4 numbers I need from the result?

const result = await batchPlay(
[
   {
      "_obj": "get",
      "_target": [
         {
            "_property": "selection"
         },
         {
            "_ref": "document",
            "_id": 224
         }
      ],
      "_options": {
         "dialogOptions": "dontDisplay"
      }
   }
],{
   "synchronousExecution": false,
   "modalBehavior": "fail"
});

The batchPlay-function returns a promise of a Descriptor array, and if you’re trying to read a property, this property is still wrapped inside this Descriptor. In Alchemist you see the blue “0” that you open up to see what’s included. That’s the descriptor at index 0. So, to get your desired property from the Alchemist code you have to access it via result[0].property, in your case result[0].selection
Also, you need to closely observe the structure in Alchemist. There’s no number array, instead it’s an object with the keys _obj, bottom, left, right, top & solid.

Thanks for your help. Is there any way you can show me how to pull one of the values out of result[0].selection and assign it to a usable variable? I think it would sink in after that and I would be able to understand how to follow the path to pull values out of other descriptors.

I tried quite a few methods unsuccessfully. However, it is just still confusing without seeing a working example of actually pulling out one of the values. I really wish there was more documentation somewhere for this with some examples. I really think that is needed.

Sorry to ask you for more help. I feel like a complete moron trying to figure out the descriptors.

There’s essentially just two basic javascript classes you need to be aware of:

  • Object (with curly brackets): {key: value}
  • Array (with brackets): [value,value,value,…]

Inside an array can of course be anything, also other arrays or objects. In Alchemist you’ll see the [] or {} when you collapse the specific part in the tree view. If you expand it with the small arrow, it will be hidden for a better overview. When you see the 0 (or 1,2,…), that’s the index of the value in an array.

In your case: const selectionX1 = result[0].selection.left._value
Another example for the font size: (try to do this by yourself to understand the concept, choose Layer & Active in Alchemist and add the Descriptor, then follow the path in the tree view:)
const fontSize = result[0].textKey.textStyleRange[0].textStyle.size._value
Just to make it more clear: textStyleRange is an array, because Texts in Photshop can have multiple styles in itself. Just imagine you’d highlight the second half of a word and change its fontsize. The second half would be accessible via
const fontSize2 = result[0].textKey.textStyleRange[1].textStyle.size._value

Thank you so much. That info is very helpful.

I was so close too. I tried that exact line of code but without the _ in front of value. It clearly shows the underscore in Alchemist so I should have tried it.

Hopefully I will be treading water soon :slight_smile:

1 Like