"select" command is not currently available

In my old CEP code I had a function selectOrCreateChannel
The way it worked was it was trying to select a channel and if there was an exception, it was creating a new channel.

I tried to do the same with UXP and instead of exception it’s now a popup dialog saying that “select” command is not currently available.

What would be a correct way to build this function in UXP/batchPlay?

batchPlay returns a promise if you have the option set to "synchronousExecution": false

That means you can use something like

.then(result => {console.log(result)}, error => {console.log(error)})'

to handle whatever the promise returns.

I am using await syntax to execute a promise. In this case it should throw all errors as exceptions.
I am not getting any exceptions, only the dialog message

Here is a batchPlay I try to execute

async create(name, selectIfExists) {
      l.log("create", name, selectIfExists)
      if (selectIfExists) {
        try {
          await this.select(name)
        } catch (e) {
          l.info(e)
          await _addChannel(name, false)
          await this.select(name)
        }
      } else {
        await _addChannel(name, false)
      }
      l.log("created", name, selectIfExists)
    },

async select(name) {
      return await batchPlay(
        [
          {
            _obj: "select",
            _target: [
              {
                _ref: "channel",
                _name: name,
              },
            ],
            _isCommand: true,
            _options: {
              dialogOptions: "dontDisplay",
            },
          },
        ],
        {
          synchronousExecution: false,
          modalBehavior: "fail",
        }
      )
    },

certainly not the most elegant way, but maybe this is how it works

async function select(name) {

const result= await batchPlay(

 [

   {

     _obj: "select",

     _target: [

       {

         _ref: "channel",

         _name: name,

       },

     ],

     _isCommand: true,

     _options: {

       dialogOptions: "silent",

     },

   },

 ],

 {

   synchronousExecution: false,

   modalBehavior: "fail",

 }

)

if(result[0].message!=undefined){

  create(name)

}else{}

}

const batchPlay = require(“photoshop”).action.batchPlay;

async function create(name, selectIfExists) {

console.log(“create”, name, selectIfExists)

if (selectIfExists) {

 try {

   await this.select(name)

 } catch (e) {

   console.log.info(e)

   await _addChannel(name, false)

   await this.select(name)

 }

} else {

 await _addChannel(name, false)

}

console.log(“created”, name, selectIfExists)

}

async function select(name) {

const result= await batchPlay(

 [

   {

     _obj: "select",

     _target: [

       {

         _ref: "channel",

         _name: name,

       },

     ],

     _isCommand: true,

     _options: {

       dialogOptions: "silent",

     },

   },

 ],

 {

   synchronousExecution: false,

   modalBehavior: "fail",

 }

)

if(result[0].message!=undefined){

  create(name)

}else{}

}

async function _addChannel(name){

const result = await batchPlay(

[

  {

     "_obj": "make",

     "new": {

        "_obj": "channel",

        "colorIndicates": {

           "_enum": "maskIndicator",

           "_value": "maskedAreas"

        },

        "color": {

           "_obj": "RGBColor",

           "red": 255,

           "grain": 0,

           "blue": 0

        },

        "opacity": 50

     },

     "_isCommand": true,

     "_options": {

        "dialogOptions": "dontDisplay"

     }

  },

  {

     "_obj": "set",

     "_target": [

        {

           "_ref": "channel",

           "_enum": "ordinal",

           "_value": "targetEnum"

        }

     ],

     "to": {

        "_obj": "channel",

        "name": name

     },

     "_isCommand": true,

     "_options": {

        "dialogOptions": "dontDisplay"

     }

  }

],{

  "synchronousExecution": false,

  "modalBehavior": "fail"

});

}

select(“try”)

I found that the problem is in

_options: {
              dialogOptions: "dontDisplay",
            },

If I delete this part then no dialogs show anymore.
This code was generated by Alchemist, so I assumed it’s the way to go.

Another thing that I now don’t understand is how to find out that there was an error.
At the moment I just check if message is present in the batchPlay result and if it is – throw it as an exception