PDA

View Full Version : Batch Export Layers SCRIPT


mgeduld
04-17-2003, 02:23 PM
In an earlier thread, I asked if anyone knew a way to batch export layers so that each layer is saved as its own file. I want to do this so that I can create a frame-by-frame animation in a PSD file with each "frame" on its own layer. I then want to export each layer as a separate file so that I can bring them into After Effects as a sequence.

sPECtre suggested that I look into creating a solution using the new scripting features of PS 7. As I'd already intended to look into PS scripting, I jumped at the chance.

So if anyone's interested (and I realize this will be of marginal interest to most here), I've now created a working script. You can download it here:

ftp://www.grumblebee.com/ps/layersToPsds.js

or you can recreate it yourself by copy/pasting the code from the end of this message (below).

If you've never run a script before, you'll have to first download and install the script plugin (free) from here:

MAC: http://www.adobe.com/support/downloads/detail.jsp?ftpID=1535
WIN: http://www.adobe.com/support/downloads/product.jsp?product=39&platform=Windows

Install the plugin. Then you'll have to place my script in the scripts folder (inside the Photoshop Presets folder).

Finally, inside Photoshop, select File > Automate > Script..., choose layersToPsd from the available scripts, and then press the Run button.

Note: this script only exposts VISIBLE layers. I decided this would be a good idea. That way, if you don't want a layer included, just turn it off. The files are saved in the same folder as the source file (the original layered document), so YOU MUST HAVE THAT FILE SAVED before running the script. The created files are all PSDs. I could have chosen JPEGs or whatever, but After Effects accepts PSDs, so I went with those. If you want to use the script to export JPEGS, just run it as is -- then create an action saves PSDs to JPEGs (or some other file type of your choice) and batch apply it to the entire folder.

I have spent a small amount of time testing this, but it may have bugs. Let me know if you find any.

Thanks, sPECtre! That was a great project. I'm sure I'll do more PS scripting in the future.

====

if (documents.length > 0)
{
displayDialogs = DialogModes.NO;
var docRef = activeDocument;
var filename = docRef.name;
var nameParts = filename.split(".psd");
var nameRoot = nameParts[0];
var counter = 0;
for (var i = 0; i < docRef.layers.length; i++)
{
if (docRef.artLayers[i].visible)
{
docRef.activeLayer = docRef.layers[i]
docRef.layers[i].copy();
var docRef2 = documents.add(docRef.width,docRef.height,docRef.re solution,docRef.activeLayer.name);
docRef2.paste();
docRef2.artLayers[1].remove();
var fullPath = docRef.path+"/"+nameRoot+counter+".psd";
saveFile = new File (fullPath);
saveOptions = new PhotoshopSaveOptions();
saveOptions.alphaChannels = true;
saveOptions.annotations = false;
saveOptions.embedColorProfile = true;
saveOptions.layers = true;
saveOptions.spotColors = true;
activeDocument.saveAs (saveFile, saveOptions, true, Extension.LOWERCASE);
saveFile = saveOptions = null;
docRef2.close(SaveOptions.DONOTSAVECHANGES);
counter++;
}
}
}
docRef.selection.deselect();
docRef = null
docRef2 = null

strych9ine
04-17-2003, 02:30 PM
Wow... congratulations on writing your successful code. I've copied the "recipe" on my hard drive just in case. It wa very generous of you to share your find, thanks! :)))

mgeduld
04-17-2003, 02:50 PM
Thanks!

I forgot to mention that some of this code was cobbled together from other people's work on the adobe forums. Those forums are a great resource for anyone interested in PS scripting -- especially since adobe's documentation is so sparce.

sPECtre
04-18-2003, 04:27 AM
Thanks for sharing your discoveries!
It's very good to offer feedback, and generous!

If you encounter a Pierre Courtejoie at the Adobe U2U, say hello from my part ;)

Frayz
04-18-2003, 09:39 AM
...is scripting only in ps7??

mgeduld
04-18-2003, 11:42 AM
Yes, I believe it was added to PS7 and Illustrator 10. Both allow you to script in various languages, including Javascropt. Which is cool, because After Effects and LiveMotion also support Javascript (as well as Flash and web browsers).