Skip to main content
Participating Frequently
March 27, 2019
Question

Transferring channels between documents with same name

  • March 27, 2019
  • 3 replies
  • 2044 views

Sometimes I have to send my images (most of the time) to external service providers like clippingpath.

To save time I upload tiffs and they mask the portraits so I can change the background.

i would really like to have the alpha channels in my original psd document to keep a clean file structure.

Sometimes there are 50 portraits, sometimes there are 100. the Images which contain the masks have the same name as the psd working files. most of the time they contain one extra channel, sometimes more.

to do all this work manually or semi-Auto process also takes to much time. I would like to run a script like the one with transferring the paths, so I can do other work while the channels are transferred to the psd files.

Here is an another thread which can be used as a reference and maybe it can help to find the right solution:

Script for Transfering paths between documents?.. help

thank you again in advice and best regards

This topic has been closed for replies.

3 replies

willcampbell7
Legend
April 14, 2019

sebastians78614123​ Did the code solve your need?

William Campbell
willcampbell7
Legend
April 2, 2019

I whipped up a script this morning, using sections of code from other scripts, reassembled to serve this purpose.

 

The script is simply a selection of source and target folders, and OK and cancel. The source folder is read into an array of image names minus extension, then the target folder files are looped over and checked to find any matches in the source folder. For any found, the alpha channels are copied from the source image to the target image.

 

The script does not copy layer masks or spot channels. I assumed that was the desire -- alpha channels only. The script can be easily adapted to copy other items from one matching image to another.

 

Please, if this works, mark your question as answered. Thanks.

 

Code below, or download here: https://www.marspremedia.com/software/photoshop/copy-alpha-channels.zip

 

#target photoshop

 

var btnCancel;

var btnFolderSource;

var btnFolderTarget;

var btnOk;

var completionMessage;

var filesSource = [];

var filesTarget = [];

var folderSource;

var folderTarget;

var g; // group

var p; // panel

var proceed;

var txtFolderSource;

var txtFolderTarget;

var w; // window

 

// CREATE USER INTERFACE

 

w = new Window("dialog", "Copy Alpha Channels");

 

// Panel 'Source images'

p = w.add("panel", undefined, "Source images");

g = p.add("group");

btnFolderSource = g.add("button", undefined, "Folder...");

txtFolderSource = g.add("statictext", undefined, undefined, {truncate: "middle"});

txtFolderSource.preferredSize = [336, -1];

 

// Panel 'Target images'

p = w.add("panel", undefined, "Target images");

g = p.add("group");

btnFolderTarget = g.add("button", undefined, "Folder...");

txtFolderTarget = g.add("statictext", undefined, undefined, {truncate: "middle"});

txtFolderTarget.preferredSize = [336, -1];

 

// Action Buttons

g = w.add("group");

g.alignment = "center";

btnOk = g.add("button", undefined, "OK");

btnCancel = g.add("button", undefined, "Cancel");

 

// UI ELEMENT EVENT HANDLERS

 

btnFolderSource.onClick = function () {

    var f = Folder.selectDialog("Select folder of source images");

    if (f) {

        txtFolderSource.text = Folder.decode(f.fsName);

        folderSource = f;

    }

};

 

btnFolderTarget.onClick = function () {

    var f = Folder.selectDialog("Select folder of target images");

    if (f) {

        txtFolderTarget.text = Folder.decode(f.fsName);

        folderTarget = f;

    }

};

 

btnOk.onClick = function () {

    if (!folderSource) {

        alert("Select folder of source images.");

        return;

    }

    if (!folderTarget) {

        alert("Select folder of target images.");

        return;

    }

    proceed = true;

    w.close();

};

 

btnCancel.onClick = function () {

    w.close();

};

 

// DISPLAY THE DIALOG

 

w.show();

 

// INTERFACE DONE AND SHOWING

// When dialog is closed execution will continue below...

 

if (proceed) {

    app.displayDialogs = DialogModes.NO;

    try {

        process();

        if (!proceed) {

            completionMessage = "User canceled.";

        } else {

            completionMessage = "Processing complete.";

        }

    } catch (e) {

        if (e.number == 8007) { // user cancelled

            completionMessage = "User canceled.";

            proceed = false;

        } else {

            completionMessage = "An error has occurred.\nLine " + e.line + ": " + e.message;

        }

    }

    alert(completionMessage);

}

 

// ====================================================================

//              END PROGRAM EXECUTION, BEGIN FUNCTIONS

// ====================================================================

 

 

function process() {

    var baseName;

    var baseNamesSource = [];

    var docSource;

    var docTarget;

    var source;

    var target;

    var i;

    var ii;

    // Gather collection of source files.

    filesSource = folderSource.getFiles(function (f) {

        if (f instanceof File && !f.hidden) {

            return true;

        }

        return false;

    });

    // Compile array of source files names minus extension.

    for (i = 0; i < filesSource.length; i++) {

        baseNamesSource = File.decode(filesSource.name).replace(/\.[^\.]+$/, "");

    }

    // Loop through target files.

    filesTarget = folderTarget.getFiles(function (f) {

        if (f instanceof File && !f.hidden) {

            return true;

        }

        return false;

    });

    if (filesTarget.length) {

        // BEGIN PROCESSING

        progress(filesTarget.length);

        // Loop through all files.

        for (i = 0; i < filesTarget.length; i++) {

            source = null;

            target = filesTarget;

            progress.message(File.decode(target.name));

            // Look for matching source file.

            // Same name minus extension.

            baseName = File.decode(target.name).replace(/\.[^\.]+$/, "");

            for (ii = 0; ii < baseNamesSource.length; ii++) {

                if (baseNamesSource[ii] == baseName) {

                    // Match.

                    source = filesSource[ii];

                    break;

                }

            }

            if (source) {

                // Found match. Open images and process.

                try {

                    docSource = app.open(source);

                    docTarget = app.open(target);

                } catch (e) {

                    if (e.number == 8007) { // user cancelled

                        proceed = false;

                        return;

                    }

                    throw e;

                }

                processDoc(docSource, docTarget);

                docSource.close(SaveOptions.DONOTSAVECHANGES);

                docTarget.close(SaveOptions.SAVECHANGES);

            }

            progress.increment();

            if (!proceed) {

                break;

            }

        }

        progress.close();

        // END PROCESSING

    }

}

 

function processDoc(docSource, docTarget) {

    var channel;

    var i;

    app.activeDocument = docSource;

    // Loop through channels looking for alpha channels.

    for (i = 0; i < docSource.channels.length; i++) {

        channel = docSource.channels;

        if (channel.kind == ChannelType.MASKEDAREA || channel.kind == ChannelType.SELECTEDAREA) {

            // Channel is alpha. Copy to target document.

            channel.duplicate(docTarget, ElementPlacement.PLACEATEND);

        }

    }

}

 

function progress(steps) {

    var w = new Window("palette", "Progress");

    var b;

    var t = w.add("statictext");

    t.preferredSize = [450, -1];

    if (steps) {

        b = w.add("progressbar", undefined, 0, steps);

        b.preferredSize = [450, -1];

    }

    w.add("statictext", undefined, "Press ESC to cancel");

    progress.close = function () {

        w.close();

    };

    progress.increment = function () {

        b.value++;

    };

    progress.message = function (message) {

        t.text = message;

        app.refresh();

    };

    w.show();

}

William Campbell
Stephen Marsh
Community Expert
Community Expert
March 28, 2019

I am new to scripting, so I can only provide part answers to hopefully get the ball rolling.

The following code snippet will copy all channels from the first open doc index 0 to the second open doc index 1:

var docChannels = app.documents[0].channels;

var targetDoc = app.documents[1]

for (i = 0; i < docChannels.length; i++ ){

        docChannels.duplicate(targetDoc);

}

The problem is that it will also copy the R, G, B or C, M, Y, K channels too.

I have not worked out how to format the code using .channels.kind = ChannelType.MASKEDAREA or another method such as starting the loop from the channel index # for the first RGB or CMYK alpha channel. Sure, the unwanted channels could be deleted, however it would be faster/better if they were not copied in the first place.

Once only the alphas are copying over, the challenge will still be batching the process of opening two files with the same name with different filename extensions, then saving the file with the alphas copied over and then cycling through all other images. It would also need a log or alert to indicate any failed images where a pair was not found. Plus 100 other things that I have not thought of yet! :]

Stephen Marsh
Community Expert
Community Expert
April 1, 2019

Bump, OK incremental progress, so that turned out to be annoyingly simple! All I had to do was change a single digit to reference the first alpha channel… So, presuming an RGB source file:

#target photoshop

var sourceDoc = app.documents[1].channels; 

var targetDoc = app.documents[0] 

for (i = 3; i < sourceDoc.length; i++ ){  

        sourceDoc.duplicate(targetDoc); 

}

app.displayDialogs = DialogModes.NO; 

app.documents[1].close(SaveOptions.DONOTSAVECHANGES);

app.documents[0].close(SaveOptions.SAVECHANGES);

I changed the document orders around as the PSD and TIFF file with the same name were opening in the opposite order than my test files that used different names.

So, can anybody post code to open a pair of files from the same source directory, both with the same filename with only the extension being different? Then loop through the source directory repeating the process using code such as in the above example?

sebastians78614123 worst case is that you could manually open pairs of files, the press a keyboard shortcut that was assigned to the script, which would then automatically copy all the alphas from the source file to the target file, automatically saving and closing as necessary… Then repeat the process again for each pair of images.

Stephen Marsh
Community Expert
Community Expert
April 1, 2019

EDIT: The following topic thread would appear to be a good candidate for the code​ to open/cycle through image pairs, but I can’t get it to work with my code above...