Copy link to clipboard
Copied
Hello all,
I'm truing to adapt a script I found online to my own use. The script exports layer groups found in a document to PNG files, withthe names of the layers.
I already modified it to some degree - changed the save location and the naming scheme - but I want to add the following functionality:
whgenever a certain special character is found in the name of the layer - say a hashtag - I want photoshop to extract the layer's opacity and save it as a new fiel with the postfix "_Mask", while flattening the transparency of the original layer and saving it with no transparency. In other words, do what Layer Mask-->from Transparency does.
I have background in scripting (Python, JS, Actionscript, Maxscript) but Photoshop scripting is relatively new to me and I'm finiding it difficult to get my head around all these weird ID codes.
Tried to install the Listener but I get quite a few lines of code for each action and again difficult to figure out what's going on and what the relevant lines are.
So - I'm attaching the script I have so far (renamed from jsx to txt). Instead of the parts I was unable to figure out, I added commnets that describe what I want to do in those lines. Can someone look it over and offer pointers what to need to put in there instead of those comments?
Thanks in advance!
Copy link to clipboard
Copied
if (Layername.indexof(maskchar) == Layername.length)
Isn't doing what you want.
If your looking for # in a string use
if (Layername.indexOf(maskchar) != -1)
See in this example:
var maskchar = "#";
var msg = "";
var Layernames = ["Layer 1#", "Layer #1", "#Layer 1", "Layer 1"];
for (var i = 0; i < Layernames.length; i++)
{
var Layername = Layernames[i];
if (Layername.indexOf(maskchar) != -1)
{
msg += maskchar + " found in " + Layername + "\n";
}
else msg += "No " + maskchar + " in " + Layername + "\n";
}
alert(msg);
Copy link to clipboard
Copied
//Layer Mask-->from Transparency
By @Michael Gros
Hi Michael, I pulled these bits out of some other scripts and put them together for the task of creating a layer mask from a layer's transparency:
// Check for active layer by jazz-y
// No check for open doc, this is assumed!
s2t = stringIDToTypeID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayers'));
r.putEnumerated(s2t("document"), s2t("ordinal"), s2t("targetEnum"));
if (!executeActionGet(r).getList(p).count) {
alert('A layer must be selected!');
} else {
// No check for layer transparency or empty layer, it is assumed that the active layer is suitable!
// Load layer transparency
loadLayerTrans();
// Add layer mask from selection
// No check added for an active selection!
makeLayerMask('RvlS');
}
function loadLayerTrans() {
try {
var idset = stringIDToTypeID("set");
var desc323 = new ActionDescriptor();
var idnull = stringIDToTypeID("null");
var ref75 = new ActionReference();
var idchannel = stringIDToTypeID("channel");
var idselection = stringIDToTypeID("selection");
ref75.putProperty(idchannel, idselection);
desc323.putReference(idnull, ref75);
var idto = stringIDToTypeID("to");
var ref76 = new ActionReference();
var idchannel = stringIDToTypeID("channel");
var idchannel = stringIDToTypeID("channel");
var idtransparencyEnum = stringIDToTypeID("transparencyEnum");
ref76.putEnumerated(idchannel, idchannel, idtransparencyEnum);
desc323.putReference(idto, ref76);
executeAction(idset, desc323, DialogModes.NO);
} catch (e) {
return false;
}
}
function makeLayerMask(maskType) {
try {
if (maskType == undefined) maskType = 'RvlA'; //from selection
// =======================================================
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 ref1 = new ActionReference();
var idChnl = charIDToTypeID("Chnl");
var idChnl = charIDToTypeID("Chnl");
var idMsk = charIDToTypeID("Msk ");
ref1.putEnumerated(idChnl, idChnl, idMsk);
desc3.putReference(idAt, ref1);
var idUsng = charIDToTypeID("Usng");
var idUsrM = charIDToTypeID("UsrM");
var idRvlA = charIDToTypeID(maskType);
desc3.putEnumerated(idUsng, idUsrM, idRvlA);
executeAction(idMk, desc3, DialogModes.NO);
} catch (e) {
return false;
}
}
Where I have added in the comments "No check for..." it is either naturally assumed or I didn't consider the check/test to be worthwhile in keeping the code short. Of course, if you do need to check or test a certain condition, then more code would be required.
Copy link to clipboard
Copied
//copy contents of the mask //paste said contents to new document var saveFile= File(oldPath +"/textures/" +basename +"_" +newname +"_mask.png"); SavePNG(saveFile);
By @Michael Gros
To save an active layer with a layer mask to PNG: