// Copyright 2005.  Reindeer Graphics, Inc.  All rights reserved.
// Convert all graphic layers into channels

if (app.documents.length == 0)
{
  alert ("There are no layers");
}
else
{
  	var docRef = app.activeDocument;					//current document
  	var layerCount = docRef.layers.length;				//number of layers in this document
  														//note: layer 0 is on top.
	if (layerCount > 1)
	{
		//force it to grayscale
		var id70 = charIDToTypeID( "CnvM" );
		    var desc21 = new ActionDescriptor();
		    var id71 = charIDToTypeID( "T   " );
		    var id72 = charIDToTypeID( "Grys" );
		    desc21.putClass( id71, id72 );
		    var id73 = charIDToTypeID( "Mrge" );
		    desc21.putBoolean( id73, false );
		executeAction( id70, desc21, DialogModes.NO );	
		
		//name the first channel after the base layer
		docRef.activeLayer = docRef.layers[docRef.layers.length-1];
		var basename = docRef.activeLayer.name;

		for (var i = 0; i < layerCount - 1; i++)
		//var i = 1;
		{
			var grabLayer = docRef.layers[docRef.layers.length-2];//next to bottom layer
  			var channelRef;
			if (grabLayer.kind != LayerKind.TEXT)
			{
				var aChannelArray = new Array();		//channel list
				docRef.activeLayer = docRef.layers[docRef.layers.length-2];	//select next to bottom layer to copy it
				var currentname = docRef.activeLayer.name;
				docRef.selection.selectAll();
				docRef.selection.copy();
				docRef.activeLayer.remove();			//delete it
				docRef.activeLayer = docRef.layers[docRef.layers.length-1];
				channelRef = docRef.channels.add();		//create a channel
				//select just new channel
				aChannelArray[0] = channelRef;			
				docRef.activeChannels = aChannelArray;	//activate this one channel
				docRef.selection.selectAll();
				docRef.paste();							//paste into channel
				//now name the channel
				var id55 = charIDToTypeID( "setd" );
				    var desc17 = new ActionDescriptor();
				    var id56 = charIDToTypeID( "null" );
				        var ref12 = new ActionReference();
				        var id57 = charIDToTypeID( "Chnl" );
				        var id58 = charIDToTypeID( "Ordn" );
				        var id59 = charIDToTypeID( "Trgt" );
				        ref12.putEnumerated( id57, id58, id59 );
				    desc17.putReference( id56, ref12 );
				    var id60 = charIDToTypeID( "T   " );
				        var desc18 = new ActionDescriptor();
				        var id61 = charIDToTypeID( "Nm  " );
				        desc18.putString( id61, currentname );
				    var id62 = charIDToTypeID( "Chnl" );
				    desc17.putObject( id60, id62, desc18 );
				executeAction( id55, desc17, DialogModes.NO );
			}
			else
			{
				grabLayer.remove();				//delete it
			}
		}
		//make the image multichannel
		// =======================================================
		var id92 = charIDToTypeID( "CnvM" );
		    var desc27 = new ActionDescriptor();
		    var id93 = charIDToTypeID( "T   " );
		    var id94 = charIDToTypeID( "MltC" );
		    desc27.putClass( id93, id94 );
		executeAction( id92, desc27, DialogModes.NO );
		//now, select channel #0 (actually select the one named "black"
		// =======================================================
		var id135 = charIDToTypeID( "slct" );
		    var desc39 = new ActionDescriptor();
		    var id136 = charIDToTypeID( "null" );
		        var ref21 = new ActionReference();
		        var id137 = charIDToTypeID( "Chnl" );
		        ref21.putName( id137, "Black" );
		    desc39.putReference( id136, ref21 );
		executeAction( id135, desc39, DialogModes.NO );
		// rename channel name to basename
		var id95 = charIDToTypeID( "setd" );
		    var desc28 = new ActionDescriptor();
		    var id96 = charIDToTypeID( "null" );
		        var ref14 = new ActionReference();
		        var id97 = charIDToTypeID( "Chnl" );
		        var id98 = charIDToTypeID( "Ordn" );
		        var id99 = charIDToTypeID( "Trgt" );
		        ref14.putEnumerated( id97, id98, id99 );
		    desc28.putReference( id96, ref14 );
		    var id100 = charIDToTypeID( "T   " );
		        var desc29 = new ActionDescriptor();
		        var id101 = charIDToTypeID( "Nm  " );
		        desc29.putString( id101, basename);
		    var id102 = charIDToTypeID( "SCch" );
		    desc28.putObject( id100, id102, desc29 );
		executeAction( id95, desc28, DialogModes.NO );
		
	}
	docRef.selection.deselect();
}
