Delete all channels with the same name

I have two questions about channels. 1- Is it possible to delete all channels with the same name?
2- Is it possible to determine if a channel with a specific name exists?

Please vote here for Channels class Trello :slight_smile:

1 Like

I’m pretty sure that both is possible, although I haven’t tried it. You can log channel operations (like delete and get) using Alchemist.

Usually this is the _target property when logging:

{
  "_ref": "channel",
  "_enum": "ordinal",
  "_value": "targetEnum"
}

You could try to use something like

{
  "_ref": "channel",
  "_name": "yourChannelName"
}

Maybe it works

1 Like

for the second point the only solution I have found is this. But it is inelegant . @simonhenke what do you think?:

async function getChannel(name) {

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

        const result = await batchPlay(

          [

            {

              "_obj": "get",

              "_target": [

                {

                  "_ref": "channel",

                  "_name": name,

                },]

            },    

          ], {

          "synchronousExecution": false,

        });

              

if(result[0].message ){

    return false

}else{

    return true

}}

Looks fine to me, except for the function naming / return value.
If it’s called getChannel, it should rather return the channel (result[0]) or undefined, instead of a boolean value, IMO.
What it’s doing right now is more like a function channelExists()

1 Like

@simonhenke thanks, later I’ll fix the code better

I solved the first point in this way, also a bit ugly, but it works. If you have any better ideas to improve the code I would appreciate it

async function channelExists(name) {
    const batchPlay = require("photoshop").action.batchPlay;
    const result = await batchPlay(
      [
        {
          "_obj": "get",
          "_target": [
            {
              "_ref": "channel",
              "_name": name,

            },]
        },    
      ], {
      "synchronousExecution": false,

    });
    
  
if(result[0].message ){
return false
}else{
return true
}}


async function deleteChannelByName(name){ 
const batchPlay = require('photoshop').action.batchPlay 
const result = await batchPlay( 
[ 
{
 "_target": [
  {
   "_ref": "channel",
   "_name": name,

  }
 ],
 "_isCommand": true,
 "_obj": "delete",
 "_options": {
  "dialogOptions": "dontDisplay"
 }
},
],{ 
"synchronousExecution": false, 

});}


async function getNumberChannel() {
    const batchPlay = require("photoshop").action.batchPlay;
    const result = await batchPlay(
      [
        {
          "_obj": "get",
          "_target": [
            {
              "_ref": "channel",
              "_enum": "ordinal",
              "_value": "targetEnum"
            },
            {
              "_ref": "document",
              "_enum": "ordinal",
              "_value": "targetEnum"
            }
          ],
          "_options": {
            "dialogOptions": "dontDisplay"
          }
        }
      ], {
      "synchronousExecution": false,

    });
    const risultato = result [0].count
return risultato}


async function deleteAllChannelByName(name){

    let numberChannel=await getNumberChannel()
for (i=0;i<=numberChannel;i++){
    if(await channelExists(name)==true){
    await  deleteChannelByName(name)}else{}

}
}

Thanks for posting this topic @svumme . It’s something I was looking to do as well. Based some of your code, I came up with the following to remove multiple channels with the same name. It will actually remove multiple channels of different names as specified by the code. I’m a bit of a hacker when it comes to UXP, but this works for me.

const numberOfChannels = await require('photoshop').action.batchPlay([
        //Get the number of channels
        { "_obj": "get", "_target":{ "_ref": "document", "_enum": "ordinal", "_value": "targetEnum" } }], {})
    .then(result => {return result[0].numberOfChannels;}, error => console.log(error))
    
    if (numberOfChannels > 3) {
        let j;
        for (j = numberOfChannels; j > 3; j--) {
            channelIndex = await require("photoshop").action.batchPlay([
                //Get channel with specified channel index of j
                {"_obj": "get","_target": [{"_ref": "channel", "_index": j},]},   
            ], {"synchronousExecution": false, modalBehavior: "wait"}).then (result => {
                if (result[0].channelName === "channel_name_0" || result[0].channelName === "channel_name_1" || result[0].channelName === "channel_name_2" || result[0].channelName === "channel_name_3" || result[0].channelName === "channel_name_4"){
                    require('photoshop').action.batchPlay([
                        //Delete channel with index of j
                        { "_obj": "delete","_target": { "_ref": "channel", "_index": j }, "_options": { "dialogOptions": "dontDisplay" } },
                    ],{"synchronousExecution": false, "modalBehavior": "wait"});   
                }
            }, error => console.log(error)) // end batchPlay
        } //end for
    }//end if numberOfChannels > 3
1 Like

Thanks for sharing @AnthonyK much better your script, often if I reach the result, and the script works I continue with all other work. I’ll take a little something from your code too :laughing:.