Combining files with identical filenames

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

scotcht

Combining files with identical filenames

Post by scotcht »

Hey guys,

I'm looking for a solution. I have two sets of folders(a and b), both of which contain the same 64 tif files. The only difference is in their alpha channels. What I need is a batch that opens the matching files, combines the alpha channels, applies an action, then saves them in a new location.

In other words, I'd like to automate the following, one to one.
open (folder_a/1.tif) and (folder_b/1.tif) to create (folder_c/1.tif)
open (folder_a/2.tif) and (folder_b/2.tif) to create (folder_c/2.tif)
open (folder_a/3.tif) and (folder_b/3.tif) to create (folder_c/3.tif)
and so on...

Any suggestions?

Thanks for any consideration!
Mike Hale

Combining files with identical filenames

Post by Mike Hale »

When you say 'combines the alpha channels' do you mean you want the final image to have the alpha channels from each source image or do you mean you want to combine the two channels into one using channel mixer or apply image?

Everything else you requested is easy enough to script. Below is a rough outline
Code: Select allfunction SaveAsTIFF( inFileName, inEmbedICC, inLZW ) {
   var tiffSaveOptions = new TiffSaveOptions();
   tiffSaveOptions.embedColorProfile = inEmbedICC;
   if ( inLZW ) {
      tiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW;
   } else {
   tiffSaveOptions.imageCompression = TIFFEncoding.NONE;
   }
   app.activeDocument.saveAs( File( inFileName ), tiffSaveOptions );
};
function main(){
   var sourceAFolder = Folder.selectDialog ('Select the A image folder');
   if(sourceAFolder == null) return;// user did not select a folder
   var sourceBFolder = Folder.selectDialog ('Select the B image folder');
   if(sourceBFolder == null) return;// user did not select a folder
   var saveFolder = Folder.selectDialog ('Select the destination folder');
   if(saveFolder == null) return;// user did not select a folder
   
   var sourceAFiles = sourceAFolder.getFiles(/\.tif$/i);
   for(var fileIndex =0;fileIndex<sourceAFiles.length;fileIndex++){
      var fileName = sourceAFiles[fileIndex].name;
      var matchingFile = new File(sourceBFolder+'/'+fileName);
      // check to see if matching file in B folder exists
      if(!matchingFile.exists) continue;// skip rest of loop is match not found
      var aImage = app.open(sourceAFiles[fileIndex]);
      var bImage = app.open(matchingFile);
      // 'combine alpha channels ???
      // app.doAction ('Action Name', 'Set Name');
      var savePath = new File(saveFolder+'/'+fileName);
      // SaveAsTIFF( savePath, true, true );// saveAs one fo the open files to new folder
      aImage.close(SaveOptions.DONOTSAVECHANGES);// close the open images without saving
      bImage.close(SaveOptions.DONOTSAVECHANGES);
   }
}
scotcht

Combining files with identical filenames

Post by scotcht »

Hey Mike,

Thanks for the quick reply. I like what you've written up, though I haven't attempted to use it.
Just so you'll know, this is basically what I'm doing:

1. open file (a)
2. action: select all, copy, then close file (a)
3. open file (b)
4. action: paste contents from (a) as new layer
select rgb value (255,255,0) using color range
create alpha channel 2 since file (b) already contains information in alpha channel 1
fill the selection, within alpha channel 2, with a new rgb value of (3,3,3)
copy the newly filled selection and paste onto alpha channel 1
delete alpha channel 2
5. action: save as file (c)

thus, I'm creating an alpha channel from the info in file (a) and combining it with the alpha channel in file (b) to create one alpha channel.
I know I could accomplish my goal in a number of ways, but thought I'd share my approach for explanation's sake.
I took this approach to ensure I was getting a solid fill to paste on top of the existing channel.

My real challenge was to accomplish tasks using a batch between two files to create a third, which I think you solved.
I'll see what I can do with your solution. Feel free to flesh it out if you want, but don't feel obligated.

Thanks again!

Scott
Mike Hale

Combining files with identical filenames

Post by Mike Hale »

I would suggest that you open A, open B, then dupe the activeLayer from A to B, then close A.
Code: Select all      var aImage = app.open(sourceAFiles[fileIndex]);
      var bImage = app.open(matchingFile);
      aImage.activeLayer.duplicate( bImage );
      aImage.close(SaveOptions.DONOTSAVECHANGES);

I wonder if there is a way to do step 4 that doesn't require all the selecting/filling/channel dancing. Can you post a few sample images in the upload section?
scotcht

Combining files with identical filenames

Post by scotcht »

Are you suggesting I could add the the active layer from a to b, close a, then save b as c?
From there I could run the appropriate actions for all of folder c.

I'm afraid I can't upload any of the actual images, so I created some basic examples. However, it wouldn't let me upload in tif format, and I wasn't sure what format would retain the alpha channel. I do think I found a better way of mixing the two channels without all the fuss though, via an action.

Will the second script you wrote stand alone? I'm pretty new to the whole scripting game. I can kind of follow it, but writing and comprehending it is a stretch.

Thanks again
Mike Hale

Combining files with identical filenames

Post by Mike Hale »

scotcht wrote:Will the second script you wrote stand alone?

No, it was meant to replace part of the first script I posted in this thread.

I wasn't suggesting that you could dupe the layer from A then saveAs to a new file. I assumed that you wanted to all the processing in one pass. But if that works with your action...

If a two pass approach works I think this combines the two snippets I posted to do the first pass.
Code: Select allfunction SaveAsTIFF( inFileName, inEmbedICC, inLZW ) {
   var tiffSaveOptions = new TiffSaveOptions();
   tiffSaveOptions.embedColorProfile = inEmbedICC;
   if ( inLZW ) {
     tiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW;
   } else {
   tiffSaveOptions.imageCompression = TIFFEncoding.NONE;
   }
   app.activeDocument.saveAs( File( inFileName ), tiffSaveOptions );
};
function main(){
   var sourceAFolder = Folder.selectDialog ('Select the A image folder');
   if(sourceAFolder == null) return;// user did not select a folder
   var sourceBFolder = Folder.selectDialog ('Select the B image folder');
   if(sourceBFolder == null) return;// user did not select a folder
   var saveFolder = Folder.selectDialog ('Select the destination folder');
   if(saveFolder == null) return;// user did not select a folder
   
   var sourceAFiles = sourceAFolder.getFiles(/\.tif$/i);
   for(var fileIndex =0;fileIndex<sourceAFiles.length;fileIndex++){
      var fileName = sourceAFiles[fileIndex].name;
      var matchingFile = new File(sourceBFolder+'/'+fileName);
      // check to see if matching file in B folder exists
      if(!matchingFile.exists) continue;// skip rest of loop is match not found
      var aImage = app.open(sourceAFiles[fileIndex]);
      var bImage = app.open(matchingFile);
      aImage.activeLayer.duplicate( bImage );
      aImage.close(SaveOptions.DONOTSAVECHANGES);
      // 'combine alpha channels ???
      // app.doAction ('Action Name', 'Set Name');
      var savePath = new File(saveFolder+'/'+fileName);
      SaveAsTIFF( savePath, true, true );// saveAs one fo the open files to new folder
      // close the open images without saving
      bImage.close(SaveOptions.DONOTSAVECHANGES);
   }
}

If you still want to upload your sample tiff files you could zip them and upload the zip file as long as it is not too big. Even if you are happy with using an action I would like to see the samples to try to come up with a one pass approach just as an exercise.
scotcht

Combining files with identical filenames

Post by scotcht »

Hey Mike,

Sorry I went quiet. I tried your script. I don't know if I am supposed to add anything to it, but as it stands it didn't seem to do anything. I had to move on to something else, and I'm just getting back to it. I think for my purposes, the two pass option will be best. So a script to combine the matching files into one file with two layers would give me the freedom to apply actions as needed. Do you mind revisiting your script? I've attached a source folder that contains folder a and b with matching files. One set has an alpha channel, but you can ignore that aspect if you'd like. I greatly appreciate your help and effort.
scotcht

Combining files with identical filenames

Post by scotcht »

I discovered someone with a similar request over here:
http://ps-scripts.com/bb/viewtopic.php?f=9&t=3100
It works pretty well. I guess the next step would be to add the action to the script. However, I'm having trouble with my action now. Is there a way to force an action to continue past an error during a batch? I know you can log them, but once it gets to an error, it leaves the file open and moves on to the next file.
flyinhawaiian
Posts: 2
Joined: Fri Oct 05, 2018 1:05 am

Re: Combining files with identical filenames

Post by flyinhawaiian »

Hey folks, I have nearly the identical need as scotcht did a few years back. Sounds like he found a working script at:http://ps-scripts.com/bb/viewtopic.php?f=9&t=3100
Problem is, it no longer seems to be accessible. Anyone have it downloaded or something like it? I've seen the other scripts like this, but for one reason or another, this seems the most useable for my needs. Anyway, I'd appreciate it if someone could repost. Thanks in advance!!!
flyinhawaiian
Posts: 2
Joined: Fri Oct 05, 2018 1:05 am

Re: Combining files with identical filenames

Post by flyinhawaiian »

Nevermind. :) Apparently this is a fairly common request and I was able to make some modifications to a script I found here to suit my needs:
https://forums.adobe.com/message/5215284#5215284