Skip to main content
September 17, 2014
Answered

Export Layer to File with Alpha

  • September 17, 2014
  • 1 reply
  • 1228 views

Hi,

i´m looking to create a Automation that takes every layer in a Photoshop Document and Export it to a TIFF with an Alpha Channel.

There are quite few Actions that Export the selected Layer as an TIFF with Alpha and there is of course the "Export Layer to Files" Function.

I just dont know how i can merge these to Functions and it would be great if i could get this functionality in a droplet.

Maybe you can guide me in the right direction

This topic has been closed for replies.
Correct answer c.pfaffenbichler

Sorry i guess i phrased taht a bit to complicated. The Action already exports the selected/visible layer. I need a way to automate this.

So it automatically selects layer 1 and exports it selects layer 2 and so forth.


This processes pixel and type layers and Masks on Groups would be applied, maybe you want to give it a try.

// save tifs without transparency but with alpha channels of pixel and type layers;

// 2014, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

var myDocument = app.activeDocument;

var basename = myDocument.name.match(/(.*)\.[^\.]+$/)[1];

try {var docPath = myDocument.path}

catch (e) {var docPath = "~/Desktop"};

var theDup = myDocument.duplicate("thecopy", false);

deleteAlphaChannels();

main(theDup, docPath, basename);

theDup.close(SaveOptions.DONOTSAVECHANGES);

};

////////////////////////////////////

// getAllLayersBounds.jsx

function main (theFile, docPath, basename) {

showAllLayers();

// get number of layers;

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var applicationDesc = executeActionGet(ref);

var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));

//////

for (var m = 0; m <= theNumber; m++) {

try {

var ref = new ActionReference();

ref.putIndex( charIDToTypeID( "Lyr " ), m);

var layerDesc = executeActionGet(ref);

var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));

var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));

var layerKind = layerDesc.getInteger(stringIDToTypeID("layerKind"));

var theName = layerDesc.getString(stringIDToTypeID('name'));

// "adjustment"

// if not layer group;

if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" /*&& isBackground != true*/) {

if (layerKind == 1 || layerKind == 3) {

hideOthers (m);

// make duplicate;

var theDup2 = theFile.duplicate("thecopy2", true);

addTransparencyAsChannelaAndFlatten();

// save and close;

saveCopyAsTif (theDup2, docPath+"/"+basename+"_"+theName+".tif");

theDup2.close(SaveOptions.DONOTSAVECHANGES);

hideOthers (m)

};

//

};

}

catch (e) {};

};

};

////// stuff //////

function addTransparencyAsChannelaAndFlatten () {

try {

// layer mask from transparency;

// =======================================================

var idMk = charIDToTypeID( "Mk  " );

    var desc3 = new ActionDescriptor();

    var idNw = charIDToTypeID( "Nw  " );

    var idChnl = charIDToTypeID( "Chnl" );

    desc3.putClass( idNw, idChnl );

    var idAt = charIDToTypeID( "At  " );

        var ref2 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idChnl = charIDToTypeID( "Chnl" );

        var idMsk = charIDToTypeID( "Msk " );

        ref2.putEnumerated( idChnl, idChnl, idMsk );

    desc3.putReference( idAt, ref2 );

    var idUsng = charIDToTypeID( "Usng" );

    var idUsrM = charIDToTypeID( "UsrM" );

    var idTrns = charIDToTypeID( "Trns" );

    desc3.putEnumerated( idUsng, idUsrM, idTrns );

executeAction( idMk, desc3, DialogModes.NO );

// alpha channel from layer mask;

// =======================================================

var idDplc = charIDToTypeID( "Dplc" );

    var desc4 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref3 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref3.putEnumerated( idChnl, idOrdn, idTrgt );

    desc4.putReference( idnull, ref3 );

executeAction( idDplc, desc4, DialogModes.NO );

// remove layer mask;

// =======================================================

var idDlt = charIDToTypeID( "Dlt " );

    var desc5 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref4 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idChnl = charIDToTypeID( "Chnl" );

        var idMsk = charIDToTypeID( "Msk " );

        ref4.putEnumerated( idChnl, idChnl, idMsk );

    desc5.putReference( idnull, ref4 );

executeAction( idDlt, desc5, DialogModes.NO );

// flatten;

// =======================================================

var idFltI = charIDToTypeID( "FltI" );

executeAction( idFltI, undefined, DialogModes.NO );

} catch (e) {}

};

// by mike hale, via paul riggott;

// http://forums.adobe.com/message/1944754#1944754

function selectLayerByIndex(index,add){

add = undefined ? add = false:add

var ref = new ActionReference();

    ref.putIndex(charIDToTypeID("Lyr "), index);

    var desc = new ActionDescriptor();

    desc.putReference(charIDToTypeID("null"), ref );

       if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );

      desc.putBoolean( charIDToTypeID( "MkVs" ), false );

   try{

    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );

}catch(e){

alert(e.message);

}

};

////// delete alpha channels //////

function deleteAlphaChannels () {

var ref = new ActionReference();

//ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var docDesc = executeActionGet(ref);

var theChannels = docDesc.getString(stringIDToTypeID("numberOfChannels"));

// check channels;

for (var m = theChannels; m > 0; m--) {

var ref2 = new ActionReference();

ref2.putIndex( charIDToTypeID("Chnl"), m);

var channelDesc = executeActionGet(ref2);

var alpha = channelDesc.hasKey(stringIDToTypeID("alphaChannelOptions"));

// delete alpha channels;

if (alpha ==true) {

// keep spot channels;

if (channelDesc.getObjectValue(stringIDToTypeID("alphaChannelOptions")).getObjectValue(stringIDToTypeID("color")).hasKey(stringIDToTypeID("book")) == false) {

// =======================================================

var idDlt = charIDToTypeID( "Dlt " );

    var desc7 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1 = new ActionReference();

  ref1.putIndex( charIDToTypeID("Chnl"), m);

    desc7.putReference( idnull, ref1 );

executeAction( idDlt, desc7, DialogModes.NO );

};

};

};

};

////// save pdf //////

function saveCopyAsTif (myDocument, thePath) {

// tif options;

tifOpts = new TiffSaveOptions();

tifOpts.embedColorProfile = true;

tifOpts.imageCompression = TIFFEncoding.TIFFLZW;

tifOpts.alphaChannels = true;

tifOpts.byteOrder = ByteOrder.MACOS;

tifOpts.layers = true;

// save copy;

myDocument.saveAs((new File(thePath)), tifOpts, true);

};

////// hide others //////

function hideOthers (theIndex) {

// hide others;

// =======================================================

var idShw = charIDToTypeID( "Shw " );

    var desc10 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var list4 = new ActionList();

            var ref7 = new ActionReference();

            ref7.putIndex( charIDToTypeID( "Lyr " ), theIndex );

        list4.putReference( ref7 );

    desc10.putList( idnull, list4 );

    var idTglO = charIDToTypeID( "TglO" );

    desc10.putBoolean( idTglO, true );

executeAction( idShw, desc10, DialogModes.NO );

};

// by mike hale, via paul riggott;

// http://forums.adobe.com/message/1944754#1944754

function selectLayerByIndex(index,add){

add = undefined ? add = false:add

var ref = new ActionReference();

    ref.putIndex(charIDToTypeID("Lyr "), index);

    var desc = new ActionDescriptor();

    desc.putReference(charIDToTypeID("null"), ref );

       if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );

      desc.putBoolean( charIDToTypeID( "MkVs" ), false );

   try{

    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );

}catch(e){

alert(e.message);

}

};

////// show all layers //////

function showAllLayers () {

// get number of layers;

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var applicationDesc = executeActionGet(ref);

var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));

//////

for (var m = 0; m <= theNumber; m++) {

try {

// =======================================================

var idHd = charIDToTypeID( "Shw " );

    var desc9 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var list1 = new ActionList();

            var ref3 = new ActionReference();

            ref3.putIndex( charIDToTypeID( "Lyr " ), m );

        list1.putReference( ref3 );

    desc9.putList( idnull, list1 );

executeAction( idHd, desc9, DialogModes.NO );

}

catch (e) {};

};

};

1 reply

c.pfaffenbichler
Community Expert
Community Expert
September 18, 2014

Export Layers To Files.jsx has more than 10.000 lines (much of it comments, it seems, but still …), so if you are new to Photoshop Scripting I doubt you will have an easy time trying to adapt that Script.

I think you should, as you don’t need the file format options, rather go with a simpler approach.

How do you intend to handle Groups and Adjustment Layers?

September 19, 2014

Thank you for your input. Seems like a good idea.

I found a Photoshop Action (www.erik-krause.de) which takes the selected visible layer creates an alpha and exports it as tiff.

So pretty much what i wanted, if i now could get rid of the user inputting which layers he wants so i could create a droplet, this would be great.

We seldomly have Groups and Adjustment Layers, typically we have a layerd psd with different text layers which need to be exported.

Avid struggles with psd and transparency. If we had a droplet the avid operator could easily convert the files without having to ask in the graphics departement.

c.pfaffenbichler
Community Expert
Community Expert
September 19, 2014

So you want to only export the selected and visible Layers?