Delete Channel by index

const app = require("photoshop").app;
const batchPlay = require("photoshop").action.batchPlay;
const deleteChannels = async(indexID) => {
 return  await batchPlay(
    [
       {
          _obj: "delete",
          _target: [
             {
                _ref: "channel",
                _index: indexID
             }
          ],
          _options: {
             dialogOptions: "dontDisplay"
          }
       }
    ],{
       synchronousExecution: false,
       modalBehavior: "fail"
    });
}

await deleteChannels(5)

getting an error with this function “Uncaught Error: Event: delete may modify the state of Photoshop. Such events are only allowed from inside a modal scope”

trying to debug it but can’t get it to work…

many thanks for your help

Deleting channel changes the state. You need to wrap your action into modal execution

Thank you, I’ll have a look at the documentation see if i can figure it out.

thanks @Karmalakas came up with this and it seems it works! thanks so much for your help


const app = require("photoshop").app;
const batchPlay = require("photoshop").action.batchPlay;
const executeAsModal = require('photoshop').core.executeAsModal;

const deleteChannels = async (indexID) => { await executeAsModal(async() => {
  return batchPlay(
     [
        {
           _obj: "delete",
           _target: [
              {
                 _ref: "channel",
                 _index: indexID
              }
           ],
           _options: {
              dialogOptions: "dontDisplay"
           }
        }
     ],{
       
     });
 });
}
1 Like