Hi, this comes from my snippets library - the original author is Cristian Buliarca, but apparently I rewrote it in coffeescript (compiled back to JS for this post, so the output is possibly not as readable as the original). Hope this helps, Davide Barranca --- www.davidebarranca.com www.cs-extensions.com
/*
* Close a LayerSet - by Cristian Buliarca
* http://ps-scripts.com/bb/viewtopic.php?f=9&t=3235
* @param {LayerSet} layerSet
* @return {LayerSet}
*/
var closeGroup;
closeGroup = function(layerSet) {
var TemporaryAlpha, activateLayerMask, addLayer, addToSelection, cTID, deleteMask, groupSelected, hasLayerMask, loadSelectionOfMask, m_BlendMode, m_Closed, m_LinkedLayers, m_Name, m_Opacity, m_Tmp, m_bHasMask, maskFromSelection, sTID, selectLayerMask, ungroup, x;
cTID = function(s) {
return charIDToTypeID(s);
};
sTID = function(s) {
return stringIDToTypeID(s);
};
ungroup = function() {
var m_Dsc01, m_Ref01;
m_Dsc01 = new ActionDescriptor();
m_Ref01 = new ActionReference();
m_Ref01.putEnumerated(cTID("Lyr "), cTID("Ordn"), cTID("Trgt"));
m_Dsc01.putReference(cTID("null"), m_Ref01);
try {
executeAction(sTID("ungroupLayersEvent"), m_Dsc01, DialogModes.NO);
} catch (_error) {}
};
addLayer = function() {
var m_ActiveLayer, m_NewLayer;
m_ActiveLayer = activeDocument.activeLayer;
m_NewLayer = activeDocument.artLayers.add();
m_NewLayer.move(m_ActiveLayer, ElementPlacement.PLACEBEFORE);
return m_NewLayer;
};
hasLayerMask = function() {
var m_Dsc01, m_Ref01;
m_Ref01 = new ActionReference();
m_Ref01.putEnumerated(sTID("layer"), cTID("Ordn"), cTID("Trgt"));
m_Dsc01 = executeActionGet(m_Ref01);
return m_Dsc01.hasKey(cTID("Usrs"));
};
activateLayerMask = function() {
var e, m_Dsc01, m_Ref01, m_TmpAlpha;
m_Dsc01 = new ActionDescriptor();
m_Ref01 = new ActionReference();
m_Ref01.putEnumerated(cTID("Chnl"), cTID("Chnl"), cTID("Msk "));
m_Dsc01.putReference(cTID("null"), m_Ref01);
try {
executeAction(cTID("slct"), m_Dsc01, DialogModes.NO);
} catch (_error) {
e = _error;
m_TmpAlpha = new TemporaryAlpha();
maskFromSelection();
activateLayerMask();
m_TmpAlpha.consume();
}
};
deleteMask = function(makeSelection) {
var m_Dsc01, m_Ref01;
if (makeSelection) {
loadSelectionOfMask();
}
m_Dsc01 = new ActionDescriptor();
m_Ref01 = new ActionReference();
m_Ref01.putEnumerated(cTID("Chnl"), cTID("Ordn"), cTID("Trgt"));
m_Dsc01.putReference(cTID("null"), m_Ref01);
try {
executeAction(cTID("Dlt "), m_Dsc01, DialogModes.NO);
} catch (_error) {}
};
selectLayerMask = function() {
var m_Dsc01, m_Ref01;
m_Dsc01 = new ActionDescriptor();
m_Ref01 = new ActionReference();
m_Ref01.putEnumerated(cTID("Chnl"), cTID("Chnl"), cTID("Msk "));
m_Dsc01.putReference(cTID("null"), m_Ref01);
m_Dsc01.putBoolean(cTID("MkVs"), false);
try {
executeAction(cTID("slct"), m_Dsc01, DialogModes.NO);
} catch (_error) {}
};
loadSelectionOfMask = function() {
var m_Dsc01, m_Ref01, m_Ref02;
selectLayerMask();
m_Dsc01 = new ActionDescriptor();
m_Ref01 = new ActionReference();
m_Ref01.putProperty(cTID("Chnl"), cTID("fsel"));
m_Dsc01.putReference(cTID("null"), m_Ref01);
m_Ref02 = new ActionReference();
m_Ref02.putEnumerated(cTID("Chnl"), cTID("Ordn"), cTID("Trgt"));
m_Dsc01.putReference(cTID("T "), m_Ref02);
try {
executeAction(cTID("setd"), m_Dsc01, DialogModes.NO);
} catch (_error) {}
};
maskFromSelection = function() {
var e, m_Dsc01, m_Ref01;
if (!hasLayerMask()) {
m_Dsc01 = new ActionDescriptor();
m_Dsc01.putClass(cTID("Nw "), cTID("Chnl"));
m_Ref01 = new ActionReference();
m_Ref01.putEnumerated(cTID("Chnl"), cTID("Chnl"), cTID("Msk "));
m_Dsc01.putReference(cTID("At "), m_Ref01);
m_Dsc01.putEnumerated(cTID("Usng"), cTID("UsrM"), cTID("RvlS"));
try {
executeAction(cTID("Mk "), m_Dsc01, DialogModes.NO);
} catch (_error) {
e = _error;
activeDocument.selection.selectAll();
maskFromSelection();
}
} else {
if (confirm("Delete existing mask?", true, "Warning")) {
activateLayerMask();
deleteMask();
}
}
};
groupSelected = function(name) {
var m_Dsc01, m_Dsc02, m_Ref01, m_Ref02;
m_Dsc01 = new ActionDescriptor();
m_Ref01 = new ActionReference();
m_Ref01.putClass(sTID("layerSection"));
m_Dsc01.putReference(cTID("null"), m_Ref01);
m_Ref02 = new ActionReference();
m_Ref02.putEnumerated(cTID("Lyr "), cTID("Ordn"), cTID("Trgt"));
m_Dsc01.putReference(cTID("From"), m_Ref02);
m_Dsc02 = new ActionDescriptor();
m_Dsc02.putString(cTID("Nm "), name);
m_Dsc01.putObject(cTID("Usng"), sTID("layerSection"), m_Dsc02);
executeAction(cTID("Mk "), m_Dsc01, DialogModes.NO);
return activeDocument.activeLayer;
};
addToSelection = function(layerName) {
var m_Dsc01, m_Ref01;
m_Dsc01 = new ActionDescriptor();
m_Ref01 = new ActionReference();
m_Ref01.putName(cTID("Lyr "), layerName);
m_Dsc01.putReference(cTID("null"), m_Ref01);
m_Dsc01.putEnumerated(sTID("selectionModifier"), sTID("selectionModifierType"), sTID("addToSelection"));
m_Dsc01.putBoolean(cTID("MkVs"), false);
try {
executeAction(cTID("slct"), m_Dsc01, DialogModes.NO);
} catch (_error) {}
};
TemporaryAlpha = function() {
activeDocument.selection.store((this.alpha = activeDocument.channels.add()));
activeDocument.selection.deselect();
this.consume = function() {
activeDocument.selection.load(this.alpha);
this.alpha.remove();
};
};
m_Name = layerSet.name;
m_Opacity = layerSet.opacity;
m_BlendMode = layerSet.blendMode;
m_LinkedLayers = layerSet.linkedLayers;
m_bHasMask = hasLayerMask();
if (m_bHasMask) {
loadSelectionOfMask();
}
if (layerSet.layers.length <= 1) {
addLayer();
m_Tmp = activeDocument.activeLayer;
m_Tmp.name = "dummy - feel free to remove me";
activeDocument.activeLayer = layerSet;
ungroup();
addToSelection("dummy - feel free to remove me");
groupSelected(m_Name);
} else {
activeDocument.activeLayer = layerSet;
ungroup();
groupSelected(m_Name);
}
m_Closed = activeDocument.activeLayer;
m_Closed.opacity = m_Opacity;
m_Closed.blendMode = m_BlendMode;
for (x in m_LinkedLayers) {
if (m_LinkedLayers .typename === "LayerSet") {
activeDocument.activeLayer.link(m_LinkedLayers );
}
}
if (m_bHasMask) {
maskFromSelection();
}
return m_Closed;
};
... View more