Skip to content
the poor coder

Photoshop extendscript javascript save to text file a list of layers

Poor Man
Published
Reading time
3 min read
On this page

How to Save a List of Layers in Photoshop using ExtendScript JavaScript#

As a graphic designer, I have often found myself needing to save a list of layers in Photoshop. This can be a tedious task, especially when there are multiple layers involved. Fortunately, with the help of ExtendScript JavaScript, this task can be automated.

Step 1: Open Photoshop and Create a New Document#

First, open Photoshop and create a new document. This can be done by clicking on File > New or by using the keyboard shortcut Ctrl+N (Windows) or Command+N (Mac).

Step 2: Create Some Layers#

Next, create some layers in the document. This can be done by clicking on the New Layer button in the Layers panel or by using the keyboard shortcut Shift+Ctrl+N (Windows) or Shift+Command+N (Mac).

Step 3: Open ExtendScript Toolkit#

Open ExtendScript Toolkit by clicking on File > Scripts > Open in ExtendScript Toolkit or by using the keyboard shortcut Alt+Ctrl+F12 (Windows) or Option+Command+F12 (Mac).

Step 4: Create a New Script#

In ExtendScript Toolkit, create a new script by clicking on File > New or by using the keyboard shortcut Ctrl+N (Windows) or Command+N (Mac).

Step 5: Write the Script#

Now it's time to write the script. The following code will save a list of layers to a text file:

Text

var doc = app.activeDocument;
var layers = doc.layers;
var layerCount = layers.length;
var layerNames = "";

for (var i = 0; i < layerCount; i++) {
  layerNames += layers[i].name + "\n";
}

var file = File.saveDialog("Save layer names", "Text files: *.txt");
file.open("w");
file.write(layerNames);
file.close();

This script gets the active document and all of its layers. It then loops through each layer and adds its name to a string called layerNames. Finally, it prompts the user to save the list to a text file.

Step 6: Run the Script#

To run the script, simply click on the Run button in ExtendScript Toolkit or use the keyboard shortcut Ctrl+R (Windows) or Command+R (Mac).

Once the script has finished running, a save dialog box will appear. Choose a location and file name for the text file and click Save.

Alternative Method: Export Layers to a File#

Another way to save a list of layers is to export them to a file. This can be done using the following script:

Text

var doc = app.activeDocument;
var outputFolder = Folder.selectDialog("Select output folder");
var layers = doc.layers;
var layerCount = layers.length;

for (var i = 0; i < layerCount; i++) {
  var layer = layers[i];
  var layerName = layer.name;
  var layerFileName = layerName.replace(/[^\w]/g, "_") + ".png";
  var outputFile = new File(outputFolder + "/" + layerFileName);
  
  layer.visible = true;
  doc.exportDocument(outputFile, ExportType.SAVEFORWEB);
  layer.visible = false;
}

This script exports each layer as a PNG file to a chosen output folder. The name of each exported file is based on the layer name.

Conclusion#

These are just a few ways to save a list of layers in Photoshop using ExtendScript JavaScript. With these tools, graphic designers can save time and energy by automating repetitive tasks.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC