The method for adding values to the Crop tool options bar does not work

I’m working on a plugin whose purpose is to save cutting presets dynamically, but for this to be possible it is essential that the values are added to the Cut tool option bar programmatically. I did some research here in the community and saw that this is probably possible.
Illustration: Desired result.

Here is my code, but I don’t know what I’m doing wrong as nothing happens when the script is run.

const batchPlay = require("photoshop").action.batchPlay;
    // GET TOOL OPTIONS
    async function getCurrentToolOptions () {
        const result = await batchPlay(
        [
        {
            "_obj": "get",
            "_target": [
                {
                    "_property": "currentToolOptions"
                },
                {
                    "_ref": "application",
                    "_enum": "ordinal",
                    "_value": "targetEnum"
                }
            ],
            "_options": {
                "dialogOptions": "dontDisplay"
            }
        }
        ],{
        "synchronousExecution": false
        });
    return result[0].currentToolOptions;
    }
    
    // SET TOOL OPTIONS
    async function setCurrentToolOptions(currenttooloptions) {
          const result = await batchPlay(
          [
          {
              "_obj": "set",
              "_target": [
                  {
                  "_ref": "cropTool"
                  }
              ],
              "to": currenttooloptions
          }
          ],{
          "synchronousExecution": false
      });
    }
    
  
    async function optionCropWH() {
        try {
            await core.executeAsModal( async() => {
                let inputs = await getCurrentToolOptions();
                inputs.$CrpO.$CrHV._value = 15 +"cm";
                inputs.$CrpO.$CrWV._value = 21+"cm";
                await setCurrentToolOptions(inputs);  
              })
        } catch (error) {alert(error)}
    }

optionCropWH() 

I think you should call crop action with but change “dialogOptions” to wait for user input. I think this should be in DOM as well.

https://developer.adobe.com/photoshop/uxp/2022/ps_reference/classes/document/#crop and https://developer.adobe.com/photoshop/uxp/2022/ps_reference/classes/photoshop/ (displayDialogs)

1 Like

Hi @Jarda , I had already managed to create a method for calling the cut action, and it works perfectly, the biggest problem is that I lose all the information about the last cut and this makes it impossible for me to perform the same cut on different images.

The definitive solution would be to transfer the width and height values from the HTML inputs to the “currentToolOptions” values of the cropping tool.
I saw that it is possible to change the properties of “paintbrushTool”, but I cannot use the same method to transfer values to the cutting tool.
All suggestions are valid. Thanks.

I found the error in my code!
I placed:

inputs.$CrpO.$CrHV = 15;
inputs.$CrpO.$CrWV= 21;

instead of:

inputs.$CrpO.$CrHV._value = 15 +"cm";
inputs.$CrpO.$CrWV._value = 21+"cm";

And everything worked perfectly!

1 Like