Hey all,
I am looking for a solution to extract all the masks in a .psd file into single "normal" jpgs or tiffs.
For example: if a psd has 30 layers and only 10 of them have a layer mask, the script should be able to save all 10 masks into 10 single files. Would it be possible to integrate it into a Batch process, where I can apply this to several files?
Does anyone have a suggestion or idea?
Thanks in advance!!
Oeschen
saving layer-masks as jpgs
-
pfaffenbichler
saving layer-masks as jpgs
I think Image > Calculations (set to Result > New Document) might provide a quick way to get a new file, at least I think it should be faster than copy/pasting.
Unfortunately ScriptingListener.plugin seems to record this with names, so the code should be amended for index instead to avoid issues if more than one Layer or more than one open Document have the same name.
Edit: Does this help?
Code: Select all// save tiffs of all layer masks in the active file;
// 2014, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
main();
};
////////////////////////////////////
function main () {
var myDocument = app.activeDocument;
// 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"));
// work through layers;
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 hasLayerMask = layerDesc.getBoolean(stringIDToTypeID('hasUserMask'));
var theName = layerDesc.getString(stringIDToTypeID('name'));
// if not layer group end do stuff;
if (layerSet != "layerSectionEnd" && isBackground != true && hasLayerMask == true) {
createNewDocFromLayerMask(myDocument, m);
};
}
catch (e) {};
};
};
////// create new doc from layer mask //////
function createNewDocFromLayerMask (theDoc, index) {
// document;
var refD = new ActionReference();
refD.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var docDesc = executeActionGet(refD);
var theItemIndex = docDesc.getInteger(stringIDToTypeID('itemIndex'));
// layer;
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), index);
var layerDesc = executeActionGet(ref);
var hasLayerMask = layerDesc.getBoolean(stringIDToTypeID('hasUserMask'));
var theName = layerDesc.getString(stringIDToTypeID('name'));
if (hasLayerMask == true) {
// =======================================================
var idMk = charIDToTypeID( "Mk " );
var desc2 = new ActionDescriptor();
var idNw = charIDToTypeID( "Nw " );
var idDcmn = charIDToTypeID( "Dcmn" );
desc2.putClass( idNw, idDcmn );
var idUsng = charIDToTypeID( "Usng" );
var desc3 = new ActionDescriptor();
var idT = charIDToTypeID( "T " );
var ref1 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idChnl = charIDToTypeID( "Chnl" );
var idMsk = charIDToTypeID( "Msk " );
ref1.putEnumerated( idChnl, idChnl, idMsk );
ref1.putIndex(charIDToTypeID("Lyr "), index);
var idDcmn = charIDToTypeID( "Dcmn" );
ref1.putIndex( idDcmn, theItemIndex );
desc3.putReference( idT, ref1 );
var idSrctwo = charIDToTypeID( "Src2" );
var ref2 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idChnl = charIDToTypeID( "Chnl" );
var idMsk = charIDToTypeID( "Msk " );
ref2.putEnumerated( idChnl, idChnl, idMsk );
ref2.putIndex(charIDToTypeID("Lyr "), index);
var idDcmn = charIDToTypeID( "Dcmn" );
ref2.putIndex( idDcmn, theItemIndex );
desc3.putReference( idSrctwo, ref2 );
var idClcl = charIDToTypeID( "Clcl" );
desc2.putObject( idUsng, idClcl, desc3 );
executeAction( idMk, desc2, DialogModes.NO );
// make grayscale;
var idCnvM = charIDToTypeID( "CnvM" );
var desc2 = new ActionDescriptor();
desc2.putClass( charIDToTypeID( "T " ), charIDToTypeID( "Grys" ) );
desc2.putBoolean( charIDToTypeID( "Mrge" ), true );
desc2.putBoolean( charIDToTypeID( "Rstr" ), false );
executeAction( idCnvM, desc2, DialogModes.NO );
// check for existing file;
var thePath = theDoc.path+"/"+theDoc.name.match(/(.*)\.[^\.]+$/)[1]+"_"+theName+"_mask.tif";
var theNumber = 1;
while (File(thePath).exists == true) {
var thePath = theDoc.path+"/"+theDoc.name.match(/(.*)\.[^\.]+$/)[1]+"_"+theName+theNumber+"_mask.tif";
theNumber++
};
// save;
saveAsTif (app.activeDocument, thePath);
app.activeDocument.close();
//
app.activeDocument = theDoc;
}
};
////// save pdf //////
function saveAsTif (myDocument, thePath) {
// tif options;
tifOpts = new TiffSaveOptions();
tifOpts.embedColorProfile = true;
tifOpts.imageCompression = TIFFEncoding.TIFFLZW;
tifOpts.alphaChannels = false;
tifOpts.byteOrder = ByteOrder.MACOS;
tifOpts.layers = false;
// save;
myDocument.saveAs((new File(thePath)), tifOpts, false);
};
Unfortunately ScriptingListener.plugin seems to record this with names, so the code should be amended for index instead to avoid issues if more than one Layer or more than one open Document have the same name.
Edit: Does this help?
Code: Select all// save tiffs of all layer masks in the active file;
// 2014, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
main();
};
////////////////////////////////////
function main () {
var myDocument = app.activeDocument;
// 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"));
// work through layers;
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 hasLayerMask = layerDesc.getBoolean(stringIDToTypeID('hasUserMask'));
var theName = layerDesc.getString(stringIDToTypeID('name'));
// if not layer group end do stuff;
if (layerSet != "layerSectionEnd" && isBackground != true && hasLayerMask == true) {
createNewDocFromLayerMask(myDocument, m);
};
}
catch (e) {};
};
};
////// create new doc from layer mask //////
function createNewDocFromLayerMask (theDoc, index) {
// document;
var refD = new ActionReference();
refD.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var docDesc = executeActionGet(refD);
var theItemIndex = docDesc.getInteger(stringIDToTypeID('itemIndex'));
// layer;
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), index);
var layerDesc = executeActionGet(ref);
var hasLayerMask = layerDesc.getBoolean(stringIDToTypeID('hasUserMask'));
var theName = layerDesc.getString(stringIDToTypeID('name'));
if (hasLayerMask == true) {
// =======================================================
var idMk = charIDToTypeID( "Mk " );
var desc2 = new ActionDescriptor();
var idNw = charIDToTypeID( "Nw " );
var idDcmn = charIDToTypeID( "Dcmn" );
desc2.putClass( idNw, idDcmn );
var idUsng = charIDToTypeID( "Usng" );
var desc3 = new ActionDescriptor();
var idT = charIDToTypeID( "T " );
var ref1 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idChnl = charIDToTypeID( "Chnl" );
var idMsk = charIDToTypeID( "Msk " );
ref1.putEnumerated( idChnl, idChnl, idMsk );
ref1.putIndex(charIDToTypeID("Lyr "), index);
var idDcmn = charIDToTypeID( "Dcmn" );
ref1.putIndex( idDcmn, theItemIndex );
desc3.putReference( idT, ref1 );
var idSrctwo = charIDToTypeID( "Src2" );
var ref2 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idChnl = charIDToTypeID( "Chnl" );
var idMsk = charIDToTypeID( "Msk " );
ref2.putEnumerated( idChnl, idChnl, idMsk );
ref2.putIndex(charIDToTypeID("Lyr "), index);
var idDcmn = charIDToTypeID( "Dcmn" );
ref2.putIndex( idDcmn, theItemIndex );
desc3.putReference( idSrctwo, ref2 );
var idClcl = charIDToTypeID( "Clcl" );
desc2.putObject( idUsng, idClcl, desc3 );
executeAction( idMk, desc2, DialogModes.NO );
// make grayscale;
var idCnvM = charIDToTypeID( "CnvM" );
var desc2 = new ActionDescriptor();
desc2.putClass( charIDToTypeID( "T " ), charIDToTypeID( "Grys" ) );
desc2.putBoolean( charIDToTypeID( "Mrge" ), true );
desc2.putBoolean( charIDToTypeID( "Rstr" ), false );
executeAction( idCnvM, desc2, DialogModes.NO );
// check for existing file;
var thePath = theDoc.path+"/"+theDoc.name.match(/(.*)\.[^\.]+$/)[1]+"_"+theName+"_mask.tif";
var theNumber = 1;
while (File(thePath).exists == true) {
var thePath = theDoc.path+"/"+theDoc.name.match(/(.*)\.[^\.]+$/)[1]+"_"+theName+theNumber+"_mask.tif";
theNumber++
};
// save;
saveAsTif (app.activeDocument, thePath);
app.activeDocument.close();
//
app.activeDocument = theDoc;
}
};
////// save pdf //////
function saveAsTif (myDocument, thePath) {
// tif options;
tifOpts = new TiffSaveOptions();
tifOpts.embedColorProfile = true;
tifOpts.imageCompression = TIFFEncoding.TIFFLZW;
tifOpts.alphaChannels = false;
tifOpts.byteOrder = ByteOrder.MACOS;
tifOpts.layers = false;
// save;
myDocument.saveAs((new File(thePath)), tifOpts, false);
};
-
oeschen
saving layer-masks as jpgs
Interesting idea, but somehow it does not work for me when I try using it. I have CS6. Did you apply the scipt on an open file without selection the layers?
I get the following error:
Error 8800: General Photoshop Error occured. This functionality may not be available in this version of Photoshop.
- The command “Select” is not currently available.
Line: 12
-> executeAction(cTID("slct"), desc, DialogModes.NO );
Am I just using the "wrong" version of PS?
Thanks for helping out here!
Cheers
I get the following error:
Error 8800: General Photoshop Error occured. This functionality may not be available in this version of Photoshop.
- The command “Select” is not currently available.
Line: 12
-> executeAction(cTID("slct"), desc, DialogModes.NO );
Am I just using the "wrong" version of PS?
Thanks for helping out here!
Cheers
-
pfaffenbichler
saving layer-masks as jpgs
As
Code: Select allexecuteAction(cTID("slct"), desc, DialogModes.NO )
does not seem to be part of the code I posted I have to wonder what you are doing.
Anyway, this is an amended version.
Code: Select all// save tiffs of all layer masks in the active file;
// 2014, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
main();
};
////////////////////////////////////
function main () {
var myDocument = app.activeDocument;
// 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"));
// work through layers;
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 hasLayerMask = layerDesc.getBoolean(stringIDToTypeID('hasUserMask'));
var theName = layerDesc.getString(stringIDToTypeID('name'));
// if not layer group end do stuff;
if (layerSet != "layerSectionEnd" && isBackground != true && hasLayerMask == true) {
createNewDocFromLayerMask(myDocument, m);
};
}
catch (e) {};
};
};
////// create new doc from layer mask //////
function createNewDocFromLayerMask (theDoc, index) {
selectLayerByIndex(index,false);
// document;
var refD = new ActionReference();
refD.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var docDesc = executeActionGet(refD);
var theItemIndex = docDesc.getInteger(stringIDToTypeID('itemIndex'));
// layer;
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), index);
var layerDesc = executeActionGet(ref);
var hasLayerMask = layerDesc.getBoolean(stringIDToTypeID('hasUserMask'));
var theName = layerDesc.getString(stringIDToTypeID('name'));
if (hasLayerMask == true) {
// =======================================================
var idMk = charIDToTypeID( "Mk " );
var desc2 = new ActionDescriptor();
var idNw = charIDToTypeID( "Nw " );
var idDcmn = charIDToTypeID( "Dcmn" );
desc2.putClass( idNw, idDcmn );
var idUsng = charIDToTypeID( "Usng" );
var desc3 = new ActionDescriptor();
var idT = charIDToTypeID( "T " );
var ref1 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idChnl = charIDToTypeID( "Chnl" );
var idMsk = charIDToTypeID( "Msk " );
ref1.putEnumerated( idChnl, idChnl, idMsk );
ref1.putIndex(charIDToTypeID("Lyr "), index);
var idDcmn = charIDToTypeID( "Dcmn" );
ref1.putIndex( idDcmn, theItemIndex );
desc3.putReference( idT, ref1 );
var idSrctwo = charIDToTypeID( "Src2" );
var ref2 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idChnl = charIDToTypeID( "Chnl" );
var idMsk = charIDToTypeID( "Msk " );
ref2.putEnumerated( idChnl, idChnl, idMsk );
ref2.putIndex(charIDToTypeID("Lyr "), index);
var idDcmn = charIDToTypeID( "Dcmn" );
ref2.putIndex( idDcmn, theItemIndex );
desc3.putReference( idSrctwo, ref2 );
var idClcl = charIDToTypeID( "Clcl" );
desc2.putObject( idUsng, idClcl, desc3 );
executeAction( idMk, desc2, DialogModes.NO );
// make grayscale;
var idCnvM = charIDToTypeID( "CnvM" );
var desc2 = new ActionDescriptor();
desc2.putClass( charIDToTypeID( "T " ), charIDToTypeID( "Grys" ) );
desc2.putBoolean( charIDToTypeID( "Mrge" ), true );
desc2.putBoolean( charIDToTypeID( "Rstr" ), false );
executeAction( idCnvM, desc2, DialogModes.NO );
// check for existing file;
var thePath = theDoc.path+"/"+theDoc.name.match(/(.*)\.[^\.]+$/)[1]+"_"+theName+"_mask.tif";
var theNumber = 1;
while (File(thePath).exists == true) {
var thePath = theDoc.path+"/"+theDoc.name.match(/(.*)\.[^\.]+$/)[1]+"_"+theName+theNumber+"_mask.tif";
theNumber++
};
// save;
saveAsTif (app.activeDocument, thePath);
app.activeDocument.close();
//
app.activeDocument = theDoc;
}
};
////// save pdf //////
function saveAsTif (myDocument, thePath) {
// tif options;
tifOpts = new TiffSaveOptions();
tifOpts.embedColorProfile = true;
tifOpts.imageCompression = TIFFEncoding.TIFFLZW;
tifOpts.alphaChannels = false;
tifOpts.byteOrder = ByteOrder.MACOS;
tifOpts.layers = false;
// save;
myDocument.saveAs((new File(thePath)), tifOpts, false);
};
// 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);
}
};
Code: Select allexecuteAction(cTID("slct"), desc, DialogModes.NO )
does not seem to be part of the code I posted I have to wonder what you are doing.
Anyway, this is an amended version.
Code: Select all// save tiffs of all layer masks in the active file;
// 2014, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
main();
};
////////////////////////////////////
function main () {
var myDocument = app.activeDocument;
// 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"));
// work through layers;
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 hasLayerMask = layerDesc.getBoolean(stringIDToTypeID('hasUserMask'));
var theName = layerDesc.getString(stringIDToTypeID('name'));
// if not layer group end do stuff;
if (layerSet != "layerSectionEnd" && isBackground != true && hasLayerMask == true) {
createNewDocFromLayerMask(myDocument, m);
};
}
catch (e) {};
};
};
////// create new doc from layer mask //////
function createNewDocFromLayerMask (theDoc, index) {
selectLayerByIndex(index,false);
// document;
var refD = new ActionReference();
refD.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var docDesc = executeActionGet(refD);
var theItemIndex = docDesc.getInteger(stringIDToTypeID('itemIndex'));
// layer;
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), index);
var layerDesc = executeActionGet(ref);
var hasLayerMask = layerDesc.getBoolean(stringIDToTypeID('hasUserMask'));
var theName = layerDesc.getString(stringIDToTypeID('name'));
if (hasLayerMask == true) {
// =======================================================
var idMk = charIDToTypeID( "Mk " );
var desc2 = new ActionDescriptor();
var idNw = charIDToTypeID( "Nw " );
var idDcmn = charIDToTypeID( "Dcmn" );
desc2.putClass( idNw, idDcmn );
var idUsng = charIDToTypeID( "Usng" );
var desc3 = new ActionDescriptor();
var idT = charIDToTypeID( "T " );
var ref1 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idChnl = charIDToTypeID( "Chnl" );
var idMsk = charIDToTypeID( "Msk " );
ref1.putEnumerated( idChnl, idChnl, idMsk );
ref1.putIndex(charIDToTypeID("Lyr "), index);
var idDcmn = charIDToTypeID( "Dcmn" );
ref1.putIndex( idDcmn, theItemIndex );
desc3.putReference( idT, ref1 );
var idSrctwo = charIDToTypeID( "Src2" );
var ref2 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idChnl = charIDToTypeID( "Chnl" );
var idMsk = charIDToTypeID( "Msk " );
ref2.putEnumerated( idChnl, idChnl, idMsk );
ref2.putIndex(charIDToTypeID("Lyr "), index);
var idDcmn = charIDToTypeID( "Dcmn" );
ref2.putIndex( idDcmn, theItemIndex );
desc3.putReference( idSrctwo, ref2 );
var idClcl = charIDToTypeID( "Clcl" );
desc2.putObject( idUsng, idClcl, desc3 );
executeAction( idMk, desc2, DialogModes.NO );
// make grayscale;
var idCnvM = charIDToTypeID( "CnvM" );
var desc2 = new ActionDescriptor();
desc2.putClass( charIDToTypeID( "T " ), charIDToTypeID( "Grys" ) );
desc2.putBoolean( charIDToTypeID( "Mrge" ), true );
desc2.putBoolean( charIDToTypeID( "Rstr" ), false );
executeAction( idCnvM, desc2, DialogModes.NO );
// check for existing file;
var thePath = theDoc.path+"/"+theDoc.name.match(/(.*)\.[^\.]+$/)[1]+"_"+theName+"_mask.tif";
var theNumber = 1;
while (File(thePath).exists == true) {
var thePath = theDoc.path+"/"+theDoc.name.match(/(.*)\.[^\.]+$/)[1]+"_"+theName+theNumber+"_mask.tif";
theNumber++
};
// save;
saveAsTif (app.activeDocument, thePath);
app.activeDocument.close();
//
app.activeDocument = theDoc;
}
};
////// save pdf //////
function saveAsTif (myDocument, thePath) {
// tif options;
tifOpts = new TiffSaveOptions();
tifOpts.embedColorProfile = true;
tifOpts.imageCompression = TIFFEncoding.TIFFLZW;
tifOpts.alphaChannels = false;
tifOpts.byteOrder = ByteOrder.MACOS;
tifOpts.layers = false;
// save;
myDocument.saveAs((new File(thePath)), tifOpts, false);
};
// 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);
}
};
-
oeschen
saving layer-masks as jpgs
WOW!!!
Such great work. Not sure why I had issues with the first script but the second you sent works great!!
Big thanks from Berlin!
Such great work. Not sure why I had issues with the first script but the second you sent works great!!
Big thanks from Berlin!