Skip to main content
Participant
September 13, 2023
Question

Photoshop API: How to export a layer group to a new PSD file?

  • September 13, 2023
  • 1 reply
  • 1239 views

Hello. I have a PSD file consisting of several layer groups. I'm trying to use the Photoshop JavaScript API to extract these layer groups into separate PSD files. This is the relevant section of my code:

 

const axios = require('axios');
...
let psdData = {
    "inputs": [{
        "href": presignedUrl,
        "storage": "external"
    }],
    "options": <options>
    },
    "outputs": [{
        "storage": "external",
        "type": <PSD type>,
        "href": uploadURL
    }]
}

let config = {
    method: 'post',
    maxBodyLength: Infinity,
    url: '<PSD service URL>',
    headers: {
        'Authorization': 'Bearer ' + aioToken,
        'x-api-key': aioApiKey,
        headers
    },
    data: JSON.stringify(psdData)
};

let resp = await axios.request(config);

 

The layer groups have predictable names, that could be hardcoded if needed (although this is not ideal).
What should go into <options>, <PSD type> and <PSD service URL> to achieve the desired result?
This topic has been closed for replies.

1 reply

cfjedimaster
Inspiring
September 14, 2023

I believe you want to use "Create renditions". The endpoint is https://image.adobe.io/pie/psdService/renditionCreate and it's documented here: https://developer.adobe.com/photoshop/photoshop-api-docs/api/#tag/Photoshop/operation/renditionCreate

 

However, it doesn't look like it supports layer groups, just layer IDs/names, but if they are predictable as well as the groups, in theory that would work.

Participant
September 14, 2023

Hello. Thanks for your help. I finally achieved what I wanted by using /actionJSON. I found out that the layer groups were actually artboards. However, they seem to work the same way as layers and layer groups.

Just in case someone runs into the same issue, this is the solution I found:

  1. Create an array with the hardcoded names of the artboards.
  2. Loop through the array. On each iteration:
    • Call the actionJSON endpoint.
    • Remove all the artboards that are not the current one.
    • Save the result as PSD or JPG.

This is my new code:

const axios = require('axios');
//...
const artboardNames = ["960x400", "1080x566", "1080x1350", "640x960", "960x1280", "1080x1920", "1940x180", "240x1200", "1280x960", "1600x500", "1080x1080", "1920x1080"];

artboardNames.forEach(async (artboardName) => {
  const artboardsToRemove = artboardNames.filter(element => element !== artboardName);
  const removeArtboardActions = artboardsToRemove.map((element) => ({ "_obj": "delete", "_target": [{ "_name": element, "_ref": "layer" }] }));

  let psdData = {
    "inputs": [{
      "href": presignedUrl,
      "storage": "external"
    }],
    "options": {
      "actionJSON": [
        ...removeArtboardActions
      ]
    },
    "outputs": [{
      "storage": "external",
      "type": "image/jpeg",
      "href": uploadURL
    }]
  }

  let config = {
    method: 'post',
    maxBodyLength: Infinity,
    url: 'https://image.adobe.io/pie/psdService/actionJSON',
    headers: {
      'Authorization': 'Bearer ' + aioToken,
      'x-api-key': aioApiKey,
      headers
    },
    data: JSON.stringify(psdData)
  };

  let resp = await axios.request(config);
  
  //...
})

I know this is kinda involved, since it deletes the undesired artboards instead of simply exporting the desired one, but it meets my goals.

Maybe /renditionCreate would be a clearer way to achieve the same result. However, I also need to perform other actions on the same PSD (like text and background changes), and actionJSON allows me to do them all with a single endpoint instead of having to call different ones separately (like /text for the text, /renditionCreate for the renditions and /smartObject for the Smart Objects).

Raymond Camden
Community Manager
Community Manager
September 14, 2023

Very cool and thank you for sharing (this is the same person as above, I just accidentally used my personal account before).