from PSPApp import *
import PSPUtils

def ScriptProperties():
    return {
        'Author': 'Linda Nieuwenstein',
        'Copyright': u'Copyright (c) 2002-2006 Corel Corporation. All rights reserved.',
        'Description': "Split the current image to CMYK, putting the separate channels back as layers in a layer group.",
        'Host': 'Paint Shop Pro',
        'Host Version': '8.00'
        }


def Do(Environment):
    if PSPUtils.RequireADoc( Environment ) == App.Constants.Boolean.false:
        return
    
    # keep track of the doc we are starting with
    TargetDoc = App.TargetDocument
    
    # start by splitting the current image.
    App.Do( Environment, 'SplitToCMYK', {
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })

    # the split created 4 new documents and puts them at the end of the open list.
    # make a four element list that we can reorder.  At this point we don't
    # care about the contents, just that it has four elements
    NewDocs = App.Documents[-4:]                # grab the last four docs
          
    # now loop over the four newest docs.
    for ChannelDoc in App.Documents[-4:]:
        Name = ChannelDoc.Title
        if Name.find( PSPUtils.Black ) != -1:
          NewDocs[0] = ChannelDoc # make Black first
        elif Name.find( PSPUtils.Yellow ) != -1:
          NewDocs[1] = ChannelDoc # make Yellow next
        elif Name.find( PSPUtils.Magenta ) != -1:
          NewDocs[2] = ChannelDoc # then Magenta
        elif Name.find( PSPUtils.Cyan ) != -1:
          NewDocs[3] = ChannelDoc # make Cyan last
    
    FirstChannel = App.Constants.Boolean.true   # need special handling on the first one
    
    # We use explicit specification of which document to work on by adding a fourth
    # parameter to the App.Do call.  For operations on one of the single channel
    # images we use one of the saved docs.  For operations on the original doc we use
    # the saved TargetDoc.
    for Doc in NewDocs:  
        # this shouldn't be necessary, but at the moment paste as new layer has a bug,
        # so we promote manually.  This assumes the original image is RGB.
        App.Do( Environment, 'IncreaseColorsTo16Million', {}, Doc )
        
        # copy it to the clipboard        
        App.Do( Environment, 'Copy', {}, Doc  )
        
        # paste it into the existing document - note the specification of target doc
        App.Do( Environment, 'PasteAsNewLayer', {}, TargetDoc )

        # when we pasted it the layer came in as Raster Layer n, and we want to know what
        # channel it represents, so pick up the image title and use that to determine
        # the layer name.  Out of split to CMYK the image will be named CyanN, YellowN, MagentaN, and BlackN.
        NewLayerName = Doc.Title
        if NewLayerName.find( PSPUtils.Black ) != -1:
            NewLayerName = PSPUtils.BlackChannel
        elif NewLayerName.find( PSPUtils.Yellow ) != -1:
            NewLayerName = PSPUtils.YellowChannel
        elif NewLayerName.find( PSPUtils.Magenta ) != -1:
            NewLayerName = PSPUtils.MagentaChannel
        else:
            NewLayerName = PSPUtils.CyanChannel
            
        # now that we have the layer name, rename it
        App.Do( Environment, 'LayerProperties', {
                'General': {
                    'Name': NewLayerName, 
                    }, 
                'GeneralSettings': {
                    'ExecutionMode': App.Constants.ExecutionMode.Silent, 
                    'AutoActionMode': App.Constants.AutoActionMode.Default
                    }
                }, TargetDoc )
        
        # we want the channels to end up inside of a layer group, so after we
        # paste the first channel in create a layer group to hold it, and then select
        # the layer inside the group so subsequent creates go where we want them.
        if FirstChannel == App.Constants.Boolean.true:
            FirstChannel = App.Constants.Boolean.false

            # create the layer group
            App.Do( Environment, 'NewLayerGroup', {
                    'General': {
                        'Name': PSPUtils.CMYKChannels, 
                        }, 
                    'GeneralSettings': {
                        'ExecutionMode': App.Constants.ExecutionMode.Silent, 
                        'AutoActionMode': App.Constants.AutoActionMode.Match
                        }
                    }, TargetDoc )

            # select the member of the group rather than the group itself            
            NewLayer = App.Do( Environment, 'SelectLayer', {
                    'Path': (0,0,[1],App.Constants.Boolean.false), 
                    'GeneralSettings': {
                        'ExecutionMode': App.Constants.ExecutionMode.Silent, 
                        'AutoActionMode': App.Constants.AutoActionMode.Default
                        }
                    }, TargetDoc)
            
        # do the close in silent mode since we don't want to save changes                
        App.Do( Environment, 'FileClose', {
                'GeneralSettings': {
                    'ExecutionMode': App.Constants.ExecutionMode.Silent, 
                    'AutoActionMode': App.Constants.AutoActionMode.Match
                    }
                }, Doc )