# Conditional BatchPlay values

**URL:** <https://forums.creativeclouddeveloper.com/t/conditional-batchplay-values/3019>\
**Category:** UXP Plugin API\
**Created:** [June 2, 2021, 7:45am UTC](https://forums.creativeclouddeveloper.com/t/conditional-batchplay-values/3019 "2021-06-02T07:45:09Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndreasResch](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/andreasresch/32/1423_2.png) [@AndreasResch](https://forums.creativeclouddeveloper.com/u/AndreasResch)\
**Post date:** [June 2, 2021, 7:45am UTC](https://forums.creativeclouddeveloper.com/t/conditional-batchplay-values/3019/1 "2021-06-02T07:45:09Z")

</div>

Hello.

I’m not a Javascript expert. I’m trying to write a “resizeImage” function with the parameters "width, height, interpolationtype). In CEP, when I left either the “width” or the “height” value undefined, the image would scale proprtionally. In BatchPlay to do that, you would either have to use a line with the “width” value OR the “height” value. When you use both, the proportional scaling doesn’t work.

Here’s the code …

async function resizeImage(width, height, interpolationtype) {  
const result = await batchPlay(  
[  
{  
“\_obj”: “imageSize”,  
“width”: {"\_unit": “pixelsUnit”, “\_value”: width},  
“height”: {"\_unit": “pixelsUnit”, “\_value”: height },  
“interfaceIconFrameDimmed”: {  
“\_enum”: “interpolationType”,  
“\_value”: interpolationtype  
},  
“\_isCommand”: false,  
“\_options”: {  
“dialogOptions”: “dontDisplay”  
}  
}  
],{  
“synchronousExecution”: false,  
“modalBehavior”: “fail”  
});  
}

Is there a way to either have the “width” line in there OR the “height” line in there, depending on if the parameter is assigned as “undefined” in the function or not? So the call “resizeImage(800, undefined, “bicubic”)” should scale the image to a width of 800px;

---

<div class="post-metadata">

**Author:** ![ddbell](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/ddbell/32/1030_2.png) [@ddbell](https://forums.creativeclouddeveloper.com/u/ddbell)\
**Post date:** [June 2, 2021, 8:10am UTC](https://forums.creativeclouddeveloper.com/t/conditional-batchplay-values/3019/2 "2021-06-02T08:10:07Z")

</div>

If you record in Alchemist and only change one value with constrain proportions on the it will give you this. It works. You can use either width or height. You just leave the other omitted.

```
async function resizeHeight(pix){

const batchPlay = require("photoshop").action.batchPlay;

const result = await batchPlay(
[
   {
      "_obj": "imageSize",
      "height": {
         "_unit": "pixelsUnit",
         "_value": pix
      },
      "scaleStyles": true,
      "constrainProportions": true,
      "interfaceIconFrameDimmed": {
         "_enum": "interpolationType",
         "_value": "automaticInterpolation"
      },
      "_isCommand": true,
      "_options": {
         "dialogOptions": "dontDisplay"
      }
   }
],{
   "synchronousExecution": false,
   "modalBehavior": "fail"
});

}
```

---

<div class="post-metadata">

**Author:** ![AndreasResch](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/andreasresch/32/1423_2.png) [@AndreasResch](https://forums.creativeclouddeveloper.com/u/AndreasResch)\
**Post date:** [June 2, 2021, 8:12am UTC](https://forums.creativeclouddeveloper.com/t/conditional-batchplay-values/3019/3 "2021-06-02T08:12:12Z")

</div>

Yeah. I’ve seen that. But I want to pass both values (width and height) in the parameters of the function and adapt the code inside the BatchPlay routine accordingly.

---

<div class="post-metadata">

**Author:** ![ddbell](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/ddbell/32/1030_2.png) [@ddbell](https://forums.creativeclouddeveloper.com/u/ddbell)\
**Post date:** [June 2, 2021, 8:20am UTC](https://forums.creativeclouddeveloper.com/t/conditional-batchplay-values/3019/4 "2021-06-02T08:20:20Z")

</div>

If you pass both values then you are controlling the aspect ratio so constrain proportions doesn’t apply. Say for example you have an image that is 2400x3000. That is a 4:5 aspect ratio. Now say you pass 1000x1500 for the resize. That is a 2:3 aspect ratio. You have just changed the aspect ratio. If you already are controlling the aspect ration the you don’t need the constrain proportion.

It works the same way in the Photoshop UI. With the constrain proportion on, you can only control one value in the UI. As soon as you change one value, Photoshop controls the other automatically.

How I do it is have 2 functions, one for width and the other for height. I just use the one I need based on whether I want to give it width or height. It works fine.

I suppose you could cram it all in one function and put some other controls in it for do that correct thing but not sure I understand the need for that.

---

<div class="post-metadata">

**Author:** ![ddbell](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/ddbell/32/1030_2.png) [@ddbell](https://forums.creativeclouddeveloper.com/u/ddbell)\
**Post date:** [June 2, 2021, 8:34am UTC](https://forums.creativeclouddeveloper.com/t/conditional-batchplay-values/3019/5 "2021-06-02T08:34:12Z")

</div>

If you need one function you can do something like this. Then you give it the pixels dimensions for one side and tell it whether that is width or height. then it auto calculates the other side based on the correct aspect ratio.

```
async function resizeImage(pix,side){

const batchPlay = require("photoshop").action.batchPlay;

if(side=="width"){

const result = await batchPlay(
[
   {
      "_obj": "imageSize",
      "width": {
         "_unit": "pixelsUnit",
         "_value": pix
      },
      "scaleStyles": true,
      "constrainProportions": true,
      "interfaceIconFrameDimmed": {
         "_enum": "interpolationType",
         "_value": "automaticInterpolation"
      },
      "_isCommand": true,
      "_options": {
         "dialogOptions": "dontDisplay"
      }
   }
],{
   "synchronousExecution": false,
   "modalBehavior": "fail"
});
    
}

else {

const result = await batchPlay(
[
   {
      "_obj": "imageSize",
      "height": {
         "_unit": "pixelsUnit",
         "_value": pix
      },
      "scaleStyles": true,
      "constrainProportions": true,
      "interfaceIconFrameDimmed": {
         "_enum": "interpolationType",
         "_value": "automaticInterpolation"
      },
      "_isCommand": true,
      "_options": {
         "dialogOptions": "dontDisplay"
      }
   }
],{
   "synchronousExecution": false,
   "modalBehavior": "fail"
});
    
}

}
```

---

<div class="post-metadata">

**Author:** ![AndreasResch](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/andreasresch/32/1423_2.png) [@AndreasResch](https://forums.creativeclouddeveloper.com/u/AndreasResch)\
**Post date:** [June 2, 2021, 8:36am UTC](https://forums.creativeclouddeveloper.com/t/conditional-batchplay-values/3019/6 "2021-06-02T08:36:34Z")

</div>

For now I’m also using two functions, but it’s not the most elegant way and CEP handled it rather nicely. There I could type … resizeImage (undefined, “800 px”, 72, ResampleMethod.BICUBIC ) or resizeImage (“1000 px”, undefined, 72, ResampleMethod.BICUBIC ) and the function knew what to do without having two separate functions.

Aside from this specific function, a solution to adapt the BatchPlay code conditionally might come in handy in other places as well. So this is more of a Javascript issue overall.

---

<div class="post-metadata">

**Author:** ![ddbell](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/ddbell/32/1030_2.png) [@ddbell](https://forums.creativeclouddeveloper.com/u/ddbell)\
**Post date:** [June 2, 2021, 8:40am UTC](https://forums.creativeclouddeveloper.com/t/conditional-batchplay-values/3019/7 "2021-06-02T08:40:57Z")

</div>

The function I posted should work. It could be condensed quite a bit. That was straight out of Alchemist and just shoved into an if/then.

---

<div class="post-metadata">

**Author:** ![AndreasResch](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/andreasresch/32/1423_2.png) [@AndreasResch](https://forums.creativeclouddeveloper.com/u/AndreasResch)\
**Post date:** [June 2, 2021, 8:58am UTC](https://forums.creativeclouddeveloper.com/t/conditional-batchplay-values/3019/8 "2021-06-02T08:58:50Z")

</div>

I managed to get it done by using your idea and two complete BatchPlay blocks. It would have been nicer and cleaner if I could have just changed the two affected lines with the width and height paramaters. But I guess that’s not possible. As I said - I’m a Javascript noob.

Here’s the complete function (which should exist in the API anyway) …

// RESIZE IMAGE NEW  
app.Document.prototype.resizeImage = async function(width, height, interpolationtype) {

if (width) {  
const result = await batchPlay(  
[  
{  
“\_obj”: “imageSize”,  
“width”: {"\_unit": “pixelsUnit”, “\_value”: width},  
“constrainProportions”: true,  
“interfaceIconFrameDimmed”: {  
“\_enum”: “interpolationType”,  
“\_value”: interpolationtype  
},  
“\_isCommand”: false,  
“\_options”: {  
“dialogOptions”: “dontDisplay”  
}  
}  
],{  
“synchronousExecution”: false,  
“modalBehavior”: “fail”  
});  
} else {  
const result = await batchPlay(  
[  
{  
“\_obj”: “imageSize”,  
“height”: {"\_unit": “pixelsUnit”, “\_value”: height},  
“constrainProportions”: true,  
“interfaceIconFrameDimmed”: {  
“\_enum”: “interpolationType”,  
“\_value”: interpolationtype  
},  
“\_isCommand”: false,  
“\_options”: {  
“dialogOptions”: “dontDisplay”  
}  
}  
],{  
“synchronousExecution”: false,  
“modalBehavior”: “fail”  
});  
}  
}

---

<div class="post-metadata">

**Author:** ![simonhenke](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/simonhenke/32/957_2.png) [@simonhenke](https://forums.creativeclouddeveloper.com/u/simonhenke)\
**Post date:** [June 2, 2021, 11:21am UTC](https://forums.creativeclouddeveloper.com/t/conditional-batchplay-values/3019/9 "2021-06-02T11:21:02Z")

</div>

It **is** possible. The (Action)Descriptors that batchPlay takes as parameters are simply Javascript objects.  
So what you want to do is conditionally add or don’t add the `width` or `height` property to said object.  
The easiest way to do this is by using the spread syntax paired with a ternary operator, for example

```
{
  // some object
  ...(condition ? {someProperty: 'someValue'} : {})
}

```

So for the imageSize function it would look something like this:

```
function __imageSize(width?: number, height?: number, _unit: Unit = 'pixelsUnit', 
scaleStyles: boolean = true, constrainProportions: boolean = true, interpolation: InterpolationType = 'automaticInterpolation', noise?: number) {
  return {
    _obj: 'imageSize',
    ...(width ? {
      width: {
        _unit,
        _value: width,
      },
    } : {}),
    ...(height ? {
      height: {
        _unit,
        _value: height,
      },
    } : {}),    
    scaleStyles,
    constrainProportions,
    interfaceIconFrameDimmed: {
      _enum: 'interpolationType',
      _value: interpolation,
    },
    ...(interpolation === 'preserveDetailsUpscale' && noise? {noise} : {}),
  }
}

```

The functions returns the Descriptor object, which u can then play with BatchPlay.

---

<div class="post-metadata">

**Author:** ![AndreasResch](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/andreasresch/32/1423_2.png) [@AndreasResch](https://forums.creativeclouddeveloper.com/u/AndreasResch)\
**Post date:** [June 2, 2021, 1:25pm UTC](https://forums.creativeclouddeveloper.com/t/conditional-batchplay-values/3019/10 "2021-06-02T13:25:15Z")

</div>

Thanks a lot for your insight. I tried a simpler, inline version, but can’t get it to work. Would something like this be possible, or do I have to use the exact syntax that you’ve used.

// RESIZE IMAGE NEW  
app.Document.prototype.resizeImage = async function(width, height, interpolationtype) {  
const result = await batchPlay(  
[  
{  
“\_obj”: “imageSize”,  
…(width ? {“width”: {"\_unit": “pixelsUnit”, “\_value”: width}} : {}),  
…(height ? {“height”: {"\_unit": “pixelsUnit”, “\_value”: height}} : {}),  
“constrainProportions”: true,  
“interfaceIconFrameDimmed”: {  
“\_enum”: “interpolationType”,  
“\_value”: interpolationtype  
},  
“\_isCommand”: false,  
“\_options”: {  
“dialogOptions”: “dontDisplay”  
}  
}  
],{  
“synchronousExecution”: false,  
“modalBehavior”: “fail”  
});  
}

---

<div class="post-metadata">

**Author:** ![simonhenke](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/simonhenke/32/957_2.png) [@simonhenke](https://forums.creativeclouddeveloper.com/u/simonhenke)\
**Post date:** [June 2, 2021, 2:59pm UTC](https://forums.creativeclouddeveloper.com/t/conditional-batchplay-values/3019/11 "2021-06-02T14:59:57Z")

</div>

Looks fine at first glance. Why did you add the `_isCommand: false` though?  
Also, if you use code highlighting for the code you paste here it becomes much easier to read.

---

<div class="post-metadata">

**Author:** ![AndreasResch](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/andreasresch/32/1423_2.png) [@AndreasResch](https://forums.creativeclouddeveloper.com/u/AndreasResch)\
**Post date:** [June 2, 2021, 4:02pm UTC](https://forums.creativeclouddeveloper.com/t/conditional-batchplay-values/3019/12 "2021-06-02T16:02:20Z")

</div>

Somehow I get an error. The “isCommand” is just there from copying from Alchemist. I’ll check the function again later. Maybe just some stupid bracket issue.

---

<div class="post-metadata">

**Author:** ![kerrishotts](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/kerrishotts/32/8_2.png) [@kerrishotts](https://forums.creativeclouddeveloper.com/u/kerrishotts)\
**Post date:** [June 2, 2021, 4:41pm UTC](https://forums.creativeclouddeveloper.com/t/conditional-batchplay-values/3019/13 "2021-06-02T16:41:57Z")

</div>

Try without the type definitions (those are for TypeScript):

> [@simonhenke](#):
>
> ```auto
> function __imageSize(width, height, _unit = 'pixelsUnit', 
> scaleStyles = true, constrainProportions = true, interpolation = 'automaticInterpolation', noise) {
> return {
> _obj: 'imageSize',
> ...(width ? { width: { _unit, _value: width, }, } : {}),
> ...(height ? { height: { _unit, _value: height, }, } : {}),    
> scaleStyles,
> constrainProportions,
> interfaceIconFrameDimmed: { _enum: 'interpolationType', _value: interpolation, },
> ...(interpolation === 'preserveDetailsUpscale' && noise? {noise} : {}),
> }
> }
> 
> ```

Alternatively if the `...` and `?` syntax is confusing (which it can be, especially one hasn’t been immersed in the recent JS standards improvements), you can also do it this way (remember that these are _just_ JS objects, and can thus be modified like any other object.)

```auto
async function resizeImage(width, height) {
  let imageSizeDescriptor = {
    "_obj": "imageSize",
    "scaleStyles": true,
    "constrainProportions": true,
    "interfaceIconFrameDimmed": {
      "_enum": "interpolationType",
      "_value": "automaticInterpolation"
    },
    "_options": {
      "dialogOptions": "dontDisplay"
    }
  };

  if (width !== undefined && width !== null) {
    imageSizeDescriptor.width = {
      "_unit": "pixelsUnit",
      "_value": width
    };
  }
  if (height !== undefined && height !== null) {
    imageSizeDescriptor.height = {
      "_unit": "pixelsUnit",
      "_value": height
    };
  }

  return batchPlay([imageSizeDescriptor], {
    "synchronousExecution": false,
    "modalBehavior": "fail"
  });
}

```

---

<div class="post-metadata">

**Author:** ![AndreasResch](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/andreasresch/32/1423_2.png) [@AndreasResch](https://forums.creativeclouddeveloper.com/u/AndreasResch)\
**Post date:** [June 2, 2021, 5:58pm UTC](https://forums.creativeclouddeveloper.com/t/conditional-batchplay-values/3019/14 "2021-06-02T17:58:08Z")

</div>

Thanks for the examples. These are really helpful.
