Search code examples
photoshopautomatorphotoshop-script

Automating Photoshop to edit en rename +600 files with the name of the folder


I have +600 product images on my mac already cut out and catalogued in their own folder. They are all PSD's and I need a script that will do the following.

  • Grab the name of the folder
  • Grab all the PSD's in said folder
  • Combine them in one big PSD in the right order (the filenames are saved sequentially as 1843, 1845, 1846 so they need to open in that order)
  • save that PSD
  • save the separate layers as PNG with the name from the folder + _1, _2, _3

I have previous experience in Bash (former Linux user) and tried for hours in Automator but to no success.


Solution

  • Welcome to Stack Overflow. The quick answer is yes this is possible to do via scripting. I might even suggest breaking down into two scripts, one to grab and save the PSDs and the second to save out the layers.

    It's not very clear about "combining" the PSDs or about "separate layers, only I don't know if they are different canvas sizes, where you want each PSD to be positioned (x, y offsets & layering) Remember none of use have your files infront of us to refer from. In short, if you write out pseudo code of what is it you expect your code to do it makes it easier to answer your question.

    Here's a few code snippets to get you started: This will open a folder and retrieve alls the PSDs as an array:

    // get all the files to process
    var folderIn = Folder.selectDialog("Please select folder to process");
    if (folderIn != null)
    {
      var tempFileList = folderIn.getFiles();
    }
    
    
    var fileList = new Array(); // real list to hold images, not folders
    
    for (var i = 0; i < tempFileList.length; i++)
    {
      // get the psd extension
      var ext = tempFileList[i].toString();
      ext = ext.substring(ext.lastIndexOf("."), ext.length);
    
      if (tempFileList[i] instanceof File) 
        {
          if (ext == ".psd") fileList.push (tempFileList[i]);
          // else (alert("Ignoring " + tempFileList[i]))
        }
    }
    
    alert("Files:\n" + fileList.length);
    

    You can save a png with this

    function save_png(afilePath)
    {
      // Save as a png
      var pngFile = new File(afilePath);
      pngSaveOptions = new PNGSaveOptions();
      pngSaveOptions.embedColorProfile = true;
      pngSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
      pngSaveOptions.matte = MatteType.NONE; pngSaveOptions.quality = 1;
      activeDocument.saveAs(pngFile, pngSaveOptions, false, Extension.LOWERCASE);
    }
    

    To open a psd just use

    app.open(fileRef);   
    

    To save it

    function savePSD(afilePath)
    {
      // save out psd
      var psdFile = new File(afilePath);
      psdSaveOptions = new PhotoshopSaveOptions();
      psdSaveOptions.embedColorProfile = true;
      psdSaveOptions.alphaChannels = true;  
      activeDocument.saveAs(psdFile, psdSaveOptions, false, Extension.LOWERCASE);
    }