Layers to Channels - Script stopped working?

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

Moderators: Tom, Kukurykus

goldencat
Posts: 2
Joined: Mon Nov 21, 2022 9:33 am

Layers to Channels - Script stopped working?

Post by goldencat »

Hi there,

Wondering if someone might be able to help me or point me in the right direction if it exists working elsewhere.

I have do 3D work and for comping in PS have tons of alpha channels for masking. Years ago a script guy i worked with created a script where you would place all your alphas in a folder, run the script and it would convert them all to channels with names. For some reason when I upgraded photoshop it stopped working (it was quite a while ago).

Does anyone know if this is an easy fix, or if an existing script exists elsewhere? It would save me a metric ton of time.

Cheers

Script attached:
Layers To Channels.jsx.zip
(1.68 KiB) Downloaded 1657 times
https://www.dropbox.com/scl/fi/tf9sj88d ... t3feb&dl=0
goldencat
Posts: 2
Joined: Mon Nov 21, 2022 9:33 am

Re: Layers to Channels - Script stopped working?

Post by goldencat »

Bump. No one? I will literally pay someone to get this working :ugeek:
DanielG
Posts: 1
Joined: Fri Apr 05, 2024 10:13 am

Re: Layers to Channels - Script stopped working?

Post by DanielG »

I've the same issue but it's only related to the newest Photoshop 2024 Version 25.6. In my case every access to a single channel array element will lead to a crash of PS. If you're able just try using your script in older Versions.
User avatar
Stephen_A_Marsh
Posts: 32
Joined: Sun Aug 04, 2019 12:37 pm

Re: Layers to Channels - Script stopped working?

Post by Stephen_A_Marsh »

goldencat wrote: Tue Mar 05, 2024 11:14 pm Bump. No one? I will literally pay someone to get this working :ugeek:
For me, the issue was with the DOM code for paste.

Replacing this with AM code resolved the issue (tested in v2021 and 2024):

Code: Select all

//enable double clicking
#target photoshop
app.bringToFront();
//store document
var docRef = app.activeDocument;
//store active layer
var layerRef = docRef.activeLayer.typename;
//make sure a group is selected
if(layerRef == "LayerSet"){
	//if there is no layers outside the set - make one
	if(docRef.layers.length == 1){
		var groupRef = docRef.activeLayer;
		var newLayer = docRef.artLayers.add();
		newLayer.name = "white";
		//select all so we can apply a fill to the selection 
		docRef.selection.selectAll();
		//create a color to be used with the fill command 
		var colorRef = new SolidColor();
		colorRef.rgb.red = 255;
		colorRef.rgb.green = 255;
		colorRef.rgb.blue = 255;
		//fill the current selection 
		docRef.selection.fill(colorRef);
		//deselect selection
		docRef.selection.deselect();
		docRef.activeLayer = groupRef;
	}
	//move group to top
	docRef.activeLayer.move(docRef.layers[0],ElementPlacement.PLACEBEFORE);
	//get the amount of layers in the group
	groupLength = docRef.layerSets[0].layers.length;
	//cycle through the group layers
	for(var i = 0; i < groupLength; i ++){
		//get the layer's name
		var layerRef = docRef.layerSets[0].layers[0];
		var layerName = layerRef.name;
		//make sure the layer is visible
		layerRef.visible = true; 
		//check for a selection
		if(checkSelection(docRef)){
            //select all and copy to clipboard
            docRef.selection.selectAll();
            docRef.selection.copy(true);//"true" copies merged  layers
            //add the new channel
            var chanRef = docRef.channels.add();
            chanRef.name = layerName;
			//docRef.paste();
			// AM code for paste channel
            var s2t = function (s) {
                return app.stringIDToTypeID(s);
            };
            var descriptor = new ActionDescriptor();
            descriptor.putEnumerated(s2t("antiAlias"), s2t("antiAliasType"), s2t("antiAliasNone"));
            descriptor.putClass(s2t("as"), s2t("pixel"));
            executeAction(s2t("paste"), descriptor, DialogModes.NO);
		}   
		//delete the layer
		layerRef.remove();
		//cleanup when finished
		if(i == groupLength - 1){
            docRef.activeLayer.remove();
            docRef.selection.deselect();
            app.purge(PurgeTarget.CLIPBOARDCACHE);
            alert("Done! \nRemember to pay Jeremy $1 every time you use this ;-)\n\nP.S. Also remember to pay Stephen as well!");
		}
	}
}
else{
	alert("You have to have a Group selected.");
}

//checks to see if there is a selection on the document
function checkSelection(document){
	var mode = app.displayDialogs;
	var res = false;
	try{
        app.displayDialogs = DialogModes.NO;
        document.selection.load(docRef.channels[0], SelectionType.REPLACE);//0 = red, 1 = green, 2 = blue
        document.selection.contract(0);
        res = true;
	}
    catch(e){
	}
    finally{
        document.selection.deselect();
        app.displayDialogs = mode;
	}
	return res;
}