Skip to main content
Participant
October 10, 2022
Answered

If Named Channel Exists, Run Action 1, Else Create Named Channel via Action 2 and Run Action 1

  • October 10, 2022
  • 2 replies
  • 435 views

//I'm new to photoshop scripting with some basic Java knowledge. (i.e., Any and all assistance is //appreciated and I'm in my infancy—take it easy! 🙂

******

Overview:
I have Action 1 that creates a masked vibrancy layer dependent on a named channel existing. That named channel is created by Action 2. In lieu of regular errors cropping up, I would like to "test" for that channel's existence and run the appropriate Action(s).

 

Either: Run Action 1 because named channel exists

Or: Create named channel via Action 2, then run Action 1 because named channel now exists.

 

//Said another way, I want to avoid:

 

  • Failed attempts at Action 1 AND
  • Creating multiple Named Channels via Action 2
 
//My attempts @ If/Else and Try/Catch are failing entirely and, surprisingly, I can't find a resource that //walks me through this arrangement—not even through example.
 
Actions from Script Listener:
 
Action 1: Add Smart Vibrancy, Subtle
 
var idPly = charIDToTypeID( "Ply " );
    var desc247 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref1 = new ActionReference();
        var idActn = charIDToTypeID( "Actn" );
        ref1.putName( idActn, "Add Smart Vibrancy, Subtle" );
        var idASet = charIDToTypeID( "ASet" );
        ref1.putName( idASet, "——— Smart Vibrancy ———" );
    desc247.putReference( idnull, ref1 );
executeAction( idPly, desc247, DialogModes.NO );
 
Action 2: Make Saturation Channel
 
var idPly = charIDToTypeID( "Ply " );
    var desc318 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref6 = new ActionReference();
        var idActn = charIDToTypeID( "Actn" );
        ref6.putName( idActn, "Make Saturation Channel" );
        var idASet = charIDToTypeID( "ASet" );
        ref6.putName( idASet, "???   Isolate and Select   ???" );
    desc318.putReference( idnull, ref6 );
executeAction( idPly, desc318, DialogModes.NO );
 
 

Some Final Thoughts/Questions:

  • Actions 1+2 are currently in separate Action Sets. Could be combined if needed but not preferred.
  • I understand I'm combining Action Manager code and DOM code. Is that correct?
  • I understand that I may be asking for a lot here. Feel free to point me to a more appopriate resource rather than investing heavily in my need.

    THANK YOU!
This topic has been closed for replies.
Correct answer Stephen Marsh

@Dream Anchor Photography 

 

Try the following... Change the channel name variable from "test" and the Action and Action Set names as required.

 

/*
Conditionally Run Actions If Channel Exists.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/if-named-channel-exists-run-action-1-else-create-named-channel-via-action-2-and-run-action-1/td-p/13256654
*/

try {

    var chaName = "test";
    activeDocument.channels[chaName];
    //alert("The channel exists!");

    // Run Action 1 from Set 1
    app.doAction("Action 1", "Action Set 1");

    // Select the channel by name
    //var channelByName = new Array(app.activeDocument.channels[chaName]);
    //activeDocument.activeChannels = channelByName;

} catch (e) {
    
    //alert("The channel doesn't exist!");
    // Run Action 2 from Set 2
    app.doAction("Action 2", "Action Set 2");
    // Run Action 1 from Set 1
    app.doAction("Action 1", "Action Set 1");
}

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

2 replies

Stephen Marsh
Community Expert
Community Expert
October 18, 2022

@Dream Anchor Photography  – So, how did the script work for you?

Participant
October 18, 2022
Worked perfectly. And, such a simple construct compared to what I was
trying to create. I've got some learning to do, that's for sure.

Thanks for the help. Looks like you help a lot of people around here.
Stephen Marsh
Community Expert
Community Expert
October 18, 2022

Great, you're welcome, I like to give back to the community where I am able.

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
October 11, 2022

@Dream Anchor Photography 

 

Try the following... Change the channel name variable from "test" and the Action and Action Set names as required.

 

/*
Conditionally Run Actions If Channel Exists.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/if-named-channel-exists-run-action-1-else-create-named-channel-via-action-2-and-run-action-1/td-p/13256654
*/

try {

    var chaName = "test";
    activeDocument.channels[chaName];
    //alert("The channel exists!");

    // Run Action 1 from Set 1
    app.doAction("Action 1", "Action Set 1");

    // Select the channel by name
    //var channelByName = new Array(app.activeDocument.channels[chaName]);
    //activeDocument.activeChannels = channelByName;

} catch (e) {
    
    //alert("The channel doesn't exist!");
    // Run Action 2 from Set 2
    app.doAction("Action 2", "Action Set 2");
    // Run Action 1 from Set 1
    app.doAction("Action 1", "Action Set 1");
}

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html