Possible to change channel order or reindex/sort channels

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

JaysonHazelbaker

Possible to change channel order or reindex/sort channels

Post by JaysonHazelbaker »

From what I've gathered from the scripting documents it appears that what I'd like to accomplish may not be feasible. Basically I'd like to sort channels based on their name. In this case searching a LAB multichannel document for potential CMYK type process channels in order to put them in their respective order before converting to CMYK mode.

Is anything like this possible?

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Mike Hale

Possible to change channel order or reindex/sort channels

Post by Mike Hale »

There is no direct way to reorder channels. But you may be able to workaround that limitation by duping the channels in the order you wish them then deleting the originals. This would only work with non-component channels. i.e. you can't delete L, a, or b channel from a Lab mode document. The other option is to dupe the channels into a new multi channel document in the order you wish then convert that document to the desired mode.
IsraelThompson

Possible to change channel order or reindex/sort channels

Post by IsraelThompson »

Just to add to this, rearranging channel order is actually quite possible, aside from your default RGB, CMYK, LAB channels. I've cooked up an example below:

Code: Select all#target photoshop
app.bringToFront()

var docRef = app.activeDocument
var adjustedIndex = (docRef.mode == DocumentMode.CMYK ? 4 : 3) // CMYK or RGB

var chans = ['Alpha 9', 'Alpha 8', 'Alpha 7', 'Alpha 6']; chans.sort() // a default list of expected channels

// list of existing document channels
//var chans = []; for ( var i = adjustedIndex; i < docRef.channels.length; i++ ) { chans[chans.length] = docRef.channels.name }; chans.sort()

for ( var i = 0; i < chans.length; i++ ) {
    try { docRef.activeChannels = [docRef.channels.getByName(chans)]; adjustedIndex = adjustedIndex + 1; moveChannelTo(adjustedIndex) } catch (e) {}
}

function moveChannelTo (idx) {
    try {
        var idmove = charIDToTypeID( "move" )
        var desc18 = new ActionDescriptor()
        var idnull = charIDToTypeID( "null" )
        var ref2 = new ActionReference()
        var idChnl = charIDToTypeID( "Chnl" )
        var idOrdn = charIDToTypeID( "Ordn" )
        var idTrgt = charIDToTypeID( "Trgt" )
        ref2.putEnumerated( idChnl, idOrdn, idTrgt )
        desc18.putReference( idnull, ref2 )
        var idT = charIDToTypeID( "T   " )
        var ref3 = new ActionReference()
        var idChnl = charIDToTypeID( "Chnl" )
        ref3.putIndex( idChnl, idx )
        desc18.putReference( idT, ref3 )
        executeAction( idmove, desc18, DialogModes.NO )
    } catch (e) {}
}