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”)