Search code examples
javascriptautomationphotoshopphotoshop-script

How to update a PSD file linked layer with opened image


I am a bit stuck. I have created a script for Photoshop that opens a PSD file containing a few layers plus a linked one, sort of a template.

So far, I got things working for me by creating a droplet that runs the following steps and scripts:

  1. Opening the file/image that is dropped.
  2. Opening the PSD file (opening template, scripted).
  3. Updating all smart objects (including the linked layer, but limited by a specific name and location)(not scripted, recorded action).
  4. Applying the template features to this linked layer which has been updated previously (scripted).
  5. Saves a PNG file (scripted),
  6. And finally, closing up opened image and template documents (recorded action).

All is good so far. But this has a limitation. It will only work on one file at a time, with a specific name, at a specific location. So, for example, for the update linked layer to work, the name of the image must be 1.png inside Downloads folder only, in this case.

So my question is: How can I script this to run an iteration of the update on the linked layer using as source(s) the images dropped onto the droplet, regardless of file types (png, jpg, etc.), name, location, and index the output saved PNG file(s)? (1.png, 2.png, 3.png, and so on.)


Solution

  • I started working on my approach, and the steps previously mentioned:

    1 - Opening the file/image that is dropped onto the Photoshop droplet.

    This would happen automatically with the file—Photoshop will open the file dropped onto the droplet, thus triggering the specific actions sequence set forth. Step one solved. Next!

    2 - Opening the PSD file (opening template, scripted)

    With this step, I wrote a script that opens the template file. The first step in the action sequence would be this!

    var template = new File("/Users/name/Desktop/Folder/Template.psd"); // Of course, you can have your template file anywhere on your computer as long as the path to find it is correct. I have selected my desktop for testing purposes.
    app.open(template);
    

    That was another effortless one. Next!

    3 - Updating all smart objects (including the linked layer, but limited by a specific name and location)(not scripted, recorded action)

    I needed to ensure that the template would easily find its previously linked layer location for the update for this third step to work. I know there are ways to update this with a script, but I didn't want to tinker with that. It was too much of a hassle for me at this stage and with my limited knowledge. Therefore I've decided to accommodate the necessary so the template would find a familiar file name it would look for when the "Update all Modified Content" action is triggered.

    At this stage, there are two files opened in Photoshop, one the initial image that serves as a new source for the template, and the second file the template.psd, which contains the linked layer that needs to be updated with the content of the first file.

    First, I have saved a copy of the image using the name that the template would look for when searching for the linked layer's name. Next, I have saved a copy of the template using the location of the first image file to keep the initial template safe from all these actions. And third, I triggered the "Update all Modified Content" action. And voila, everything worked. The initial template had the linked file next to it. So the new template copy would search and find the file next to it, in the same place, folder/location, as it happened on the previews step that helped save it as such.

    // The following script will retrieve the path of the opened image and will save a copy that matches the name of the linked layer in the template in the same location.
    
    var image_doc = app.documents[0]; //If two or more documents are opened, this approach will help switch between them.
    var image_name = image_doc.name;
    var image_path = app.documents[0].path.fsName;
    var temp_image = new File("" + image_path + "/" + image_name + "");
    
    var opts, file;
    opts = new ExportOptionsSaveForWeb();
    opts.format = SaveDocumentType.PNG;
    
    var image_temp_name = "link.png";
    pngFile = new File("" + image_path + "/" + image_temp_name + "");
    image_doc.exportDocument(pngFile, ExportType.SAVEFORWEB, opts);
    
    
    // Save a copy of the template.psd in the same location as the image and the link.png file needed to update the linked layer.
    
    var temp_template = new File("" + image_path + "/" + image_name + "");
    app.open(template);
    
    var opts, file;
    opts = new PhotoshopSaveOptions();
    opts.format = SaveDocumentType.PHOTOSHOP;
    
    var template_temp_name = "template.psd";
    psdFile = new File(image_path + "/" + template_temp_name);
    psdSaveOptions = new PhotoshopSaveOptions();
    psdSaveOptions.embedColorProfile = true;
    psdSaveOptions.alphaChannels = true;
    activeDocument.saveAs(psdFile, psdSaveOptions, false, Extension.LOWERCASE);
    
    // After these Update all Modified Content action
    

    Now, four, five, and six are straightforward:

    4 - Applying the template features to this linked layer which has just been updated previously (scripted). Done!

    5 - Saves the newly formed template as a PNG file (scripted). Done!

    6 - And finally, closing up opened image and template documents (recorded action). Done!