Load a selection and apply to all layers as a layer mask

I have saved a selection as a channel called “circle”.
I want to apply this selection as a layer mask to all layers

This is the script I have so far


tell application "Adobe Photoshop CC 2015.5"
	activate
	set theDOC to the current document
		
	tell theDOC
		set totalLayers to count each layer
		repeat with indice from 1 to totalLayers by 1
	        
			tell layer indice

                           -- load the channel
		               tell theDOC
					load selection of it from channel "circle" of it
				end tell
				
				
                -- I now need a magic command to apply that selection to the current layer, inside this loop

			end tell
			
		end repeat
		
	end tell
	
end tell

Hi,

For any ‘missing’ commands from Photoshop, I use the Script Listener plugin which generates some javascript. You can then make any constant values into variables if you have to. In this case you just need to replace your commented line with:

do javascript "var idMk = charIDToTypeID( 'Mk  ' );
    var desc233 = new ActionDescriptor();
    var idNw = charIDToTypeID( 'Nw  ' );
    var idChnl = charIDToTypeID( 'Chnl' );
    desc233.putClass( idNw, idChnl );
    var idAt = charIDToTypeID( 'At  ' );
        var ref24 = new ActionReference();
        var idChnl = charIDToTypeID( 'Chnl' );
        var idChnl = charIDToTypeID( 'Chnl' );
        var idMsk = charIDToTypeID( 'Msk ' );
        ref24.putEnumerated( idChnl, idChnl, idMsk );
    desc233.putReference( idAt, ref24 );
    var idUsng = charIDToTypeID( 'Usng' );
    var idUsrM = charIDToTypeID( 'UsrM' );
    var idRvlS = charIDToTypeID( 'RvlS' );
    desc233.putEnumerated( idUsng, idUsrM, idRvlS );
executeAction( idMk, desc233, DialogModes.NO );"

and the first line inside the repeat loop needs to be

set current layer to layer indice

so that the correct layer is targeted