/*
<javascriptresource>
<name>BCM> ToogleOpen_CloseGroup...</name>
<category>BCM</category>
</javascriptresource>
*/
//For code readability
// http://ps-scripts.com/bb/viewtopic.php?f=9&t=3235
function getActiveLayerIndex() {
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
return executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ));
};
function cTID(s){return charIDToTypeID(s)}
function sTID(s){return stringIDToTypeID(s)}
// =============================
// The main function
function closeGroup(layerSet) {
var m_Name = layerSet.name;
var m_Opacity = layerSet.opacity;
var m_BlendMode = layerSet.blendMode;
var m_LinkedLayers = layerSet.linkedLayers;
var currINDEX = getActiveLayerIndex();
var m_bHasMask = hasLayerMask();
if(m_bHasMask) loadSelectionOfMask();
if(layerSet.layers.length <= 1) {
addTempLayerSetIn(currSetIDX-1);
makeActiveByIndex(currSetIDX+2, false);
ungroup();
groupSelected(m_Name);
deleteTempLayerSetbyIdx(currSetIDX+1);
} else {
makeActiveByIndex(currSetIDX, false);
ungroup();
groupSelected(m_Name);
}
var m_Closed = activeDocument.activeLayer;
m_Closed.opacity = m_Opacity;
m_Closed.blendMode = m_BlendMode;
for(x in m_LinkedLayers) {
if(m_LinkedLayers[x].typename == "LayerSet")
activeDocument.activeLayer.link(m_LinkedLayers[x]);
}
if(m_bHasMask) maskFromSelection();
return m_Closed;
}
// =============================
// Below are all necessary subroutines for the main function to work
function ungroup() {
var m_Dsc01 = new ActionDescriptor();
var 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(e) {}
}
function addLayer() {
var m_ActiveLayer = activeDocument.activeLayer;
var m_NewLayer = activeDocument.layerSets.add();
m_NewLayer.move(m_ActiveLayer, ElementPlacement.PLACEBEFORE);
return m_NewLayer;
}
function hasLayerMask() {
var m_Ref01 = new ActionReference();
m_Ref01.putEnumerated( sTID( "layer" ), cTID( "Ordn" ), cTID( "Trgt" ));
var m_Dsc01= executeActionGet( m_Ref01 );
return m_Dsc01.hasKey(cTID('Usrs'));
}
function activateLayerMask() {
var m_Dsc01 = new ActionDescriptor();
var 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(e) {
var m_TmpAlpha = new TemporaryAlpha();
maskFromSelection();
activateLayerMask();
m_TmpAlpha.consume();
}
}
function deleteMask(makeSelection) {
if(makeSelection) {
loadSelectionOfMask();
}
var m_Dsc01 = new ActionDescriptor();
var 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(e) {}
}
function selectLayerMask() {
var m_Dsc01 = new ActionDescriptor();
var 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(e) {}
}
function loadSelectionOfMask() {
selectLayerMask();
var m_Dsc01 = new ActionDescriptor();
var m_Ref01 = new ActionReference();
m_Ref01.putProperty( cTID( "Chnl" ), cTID( "fsel" ) );
m_Dsc01.putReference( cTID( "null" ), m_Ref01 );
var 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(e) {}
}
function maskFromSelection() {
if(!hasLayerMask()) {
var m_Dsc01 = new ActionDescriptor();
m_Dsc01.putClass( cTID( "Nw " ), cTID( "Chnl" ) );
var 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(e) {
activeDocument.selection.selectAll();
maskFromSelection();
}
} else {
if(confirm("Delete existing mask?", true, "Warning")) {
activateLayerMask();
deleteMask();
}
}
}
function groupSelected(name) {
var m_Dsc01 = new ActionDescriptor();
var m_Ref01 = new ActionReference();
m_Ref01.putClass( sTID( "layerSection" ) );
m_Dsc01.putReference( cTID( "null" ), m_Ref01 );
var m_Ref02 = new ActionReference();
m_Ref02.putEnumerated( cTID( "Lyr " ), cTID( "Ordn" ), cTID( "Trgt" ) );
m_Dsc01.putReference( cTID( "From" ), m_Ref02 );
var 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;
}
function addToSelection(layerName) {
var m_Dsc01 = new ActionDescriptor();
var 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(e) {}
}
function TemporaryAlpha() {
activeDocument.selection.store((this.alpha = activeDocument.channels.add()));
activeDocument.selection.deselect();
this.consume = function() {
activeDocument.selection.load(this.alpha);
this.alpha.remove();
}
}
function makeActiveByIndex( idx, visible ){
if( idx.constructor != Array ) idx = [ idx ];
for( var i = 0; i < idx.length; i++ ){
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIndex(charIDToTypeID( 'Lyr ' ), idx[i])
desc.putReference( charIDToTypeID( 'null' ), ref );
if( i > 0 ) {
var idselectionModifier = stringIDToTypeID( 'selectionModifier' );
var idselectionModifierType = stringIDToTypeID( 'selectionModifierType' );
var idaddToSelection = stringIDToTypeID( 'addToSelection' );
desc.putEnumerated( idselectionModifier, idselectionModifierType, idaddToSelection );
}
desc.putBoolean( charIDToTypeID( 'MkVs' ), visible );
executeAction( charIDToTypeID( 'slct' ), desc, DialogModes.NO );
}
}
function deleteActiveLayer(){
// =======================================================
var idDlt = charIDToTypeID( "Dlt " );
var desc752 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref529 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref529.putEnumerated( idLyr, idOrdn, idTrgt );
desc752.putReference( idnull, ref529 );
executeAction( idDlt, desc752, DialogModes.NO );
}
function isLayerSet( idx ) {
var propName = stringIDToTypeID( 'layerSection' );
var ref = new ActionReference();
ref.putProperty( charIDToTypeID( "Prpr" ) , propName);
ref.putIndex( charIDToTypeID ( "Lyr " ), idx );
var desc = executeActionGet( ref );
var type = desc.getEnumerationValue( propName );
var res = typeIDToStringID( type );
// alert(res);
return res == 'layerSectionStart' ? true:false;
}
function openGroup1(theGroup) {
currSetIDX= getActiveLayerIndex();
if(isLayerSet( currSetIDX ))
{
getNamesPlusIDsOfLayerSet();
}
makeActiveByIndex(currSetIDX, false);
}
function getNamesPlusIDsOfLayerSet(){
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
var count = executeActionGet(ref).getInteger(charIDToTypeID('Cnt '));
var parId = executeActionGet(ref).getInteger(stringIDToTypeID( 'layerID' ));
var Names=[];
var x = 0;
var y = 0;
var r = 0;
currINDEX = getActiveLayerIndex();
var i = currINDEX;
for(i; i > 0 ; i--){
ref = new ActionReference();
ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
var desc = executeActionGet(ref);
var layerName = desc.getString(charIDToTypeID( 'Nm ' ));
var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));
var ls = desc.getEnumerationValue(stringIDToTypeID("layerSection"));
ls = typeIDToStringID(ls);
// alert(layerName+": _ :"+ls);
if(ls == "layerSectionStart"){x++};
if(layerName.match(/^<\/Layer group/) )
{
y ++;
r = x - y;
if(r == 0 && ls == "layerSectionEnd"){break};
continue
};
if(ls == "layerSectionContent"){makeActiveByIndex(i,false);break};
var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));
var isLayerSet =( layerType == 'layerSectionContent') ? false:true;
Names.push([[Id],[layerName],[isLayerSet]]);
};
return Names;
};
function getLayersNb()//function to find out if the number of layers in the document
{
var ref = new ActionReference();
ref.putProperty( charIDToTypeID( 'Prpr' ), stringIDToTypeID('numberOfLayers') );
ref.putEnumerated( charIDToTypeID( "Dcmn" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
var desc = executeActionGet(ref);
var numberOfLayers = desc.getInteger(stringIDToTypeID('numberOfLayers'));
return numberOfLayers;
}
function toogleOpenCloseSet(){
myALayerIDX = getActiveLayerIndex();
myGroupP = app.activeDocument.activeLayer;
if(!isLayerSet(myALayerIDX)){
myGroupP = app.activeDocument.activeLayer.parent;
if(myGroupP.typename != "Document"){
if(isSetOpened1(myGroupP)){closeGroup(myGroupP)}else{openGroup1(myGroupP)};
}
}else{
if(isSetOpened1(myGroupP)){closeGroup(myGroupP)}else{openGroup1(myGroupP)};
}
}
function getFristLayerSetChildVisible(){
xx = false;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
var count = executeActionGet(ref).getInteger(charIDToTypeID('Cnt '));
var parId = executeActionGet(ref).getInteger(stringIDToTypeID( 'layerID' ));
var Names=[];
var x = 0;
var y = 0;
var r = 0;
currINDEX = getActiveLayerIndex();
var i = currINDEX;
for(i; i > 0 ; i--){
ref = new ActionReference();
ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
var desc = executeActionGet(ref);
var layerName = desc.getString(charIDToTypeID( 'Nm ' ));
var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));
var ls = desc.getEnumerationValue(stringIDToTypeID("layerSection"));
ls = typeIDToStringID(ls);
var vis = desc.getInteger(stringIDToTypeID( 'visible' ));
// alert(layerName+": _ :"+vis);
/* if(desc.hasKey(stringIDToTypeID("visible")))
{
alert(desc.getType(stringIDToTypeID("visible")));
};*/
if(ls == "layerSectionStart"){x++};
if(vis == 1 && Id!=parId && r!=0){
var theOBj = {id:Id, lname:layerName, idx:i};
xx =true;
Names.push(theOBj);
break;
}
if(layerName.match(/^<\/Layer group/) )
{
y ++;
r = x - y;
if(r == 0 && ls == "layerSectionEnd"){break};
continue
}
}
return Names;
}
function getLastChildIdx(){
xx = false;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
var count = executeActionGet(ref).getInteger(charIDToTypeID('Cnt '));
var parId = executeActionGet(ref).getInteger(stringIDToTypeID( 'layerID' ));
currINDEX = getActiveLayerIndex();
var i = currINDEX;
var x = 0;
var y = 0;
var r = 0;
var lastChIdx = 0;
for(i; i > 0 ; i--){
ref = new ActionReference();
ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
var desc = executeActionGet(ref);
var layerName = desc.getString(charIDToTypeID( 'Nm ' ));
var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));
var ls = desc.getEnumerationValue(stringIDToTypeID("layerSection"));
ls = typeIDToStringID(ls);
var vis = desc.getInteger(stringIDToTypeID( 'visible' ));
if(ls == "layerSectionStart"){x++};
if(layerName.match(/^<\/Layer group/) )
{
y ++;
r = x - y;
if(r == 0 && ls == "layerSectionEnd")
{
lastChIdx = i;
break;
};
}
// alert(x+" _ "+y+" _ "+r+" _ "+layerName);
}
return lastChIdx;
}
function getNbOfChilds(){
xx = false;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
var count = executeActionGet(ref).getInteger(charIDToTypeID('Cnt '));
var parId = executeActionGet(ref).getInteger(stringIDToTypeID( 'layerID' ));
currINDEX = getActiveLayerIndex();
var i = currINDEX;
var nb = 0;
var x = 0;
var y = 0;
var r = 0;
for(i; i > 0 ; i--){
ref = new ActionReference();
ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
var desc = executeActionGet(ref);
var layerName = desc.getString(charIDToTypeID( 'Nm ' ));
var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));
var ls = desc.getEnumerationValue(stringIDToTypeID("layerSection"));
ls = typeIDToStringID(ls);
var vis = desc.getInteger(stringIDToTypeID( 'visible' ));
if(ls == "layerSectionStart"){x++};
if(layerName.match(/^<\/Layer group/) )
{
y ++;
r = x - y;
if(r == 0 && ls == "layerSectionEnd")
{
break
};
continue
}
nb++;
}
return nb;
}
function isSetOpened1( group ){
app.activeDocument.activeLayer = group;
xx = true;
currSetIDX = getActiveLayerIndex();
addTempLayerSetIn(currSetIDX-1);
var fIdx = getActiveLayerIndex();
makeActiveByIndex(currSetIDX+2, false);
if(fIdx == getActiveLayerIndex())
{
xx = false;
}
deleteTempLayerSetbyIdx(currSetIDX+1);
return xx;
}
function addTempLayerSetIn(idxx){
// =======================================================
var idMk = charIDToTypeID( "Mk " );
var desc58 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref63 = new ActionReference();
var idlayerSection = stringIDToTypeID( "layerSection" );
ref63.putClass( idlayerSection );
desc58.putReference( idnull, ref63 );
executeAction( idMk, desc58, DialogModes.NO );
// =======================================================rename
var idsetd = charIDToTypeID( "setd" );
var desc202 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref209 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref209.putEnumerated( idLyr, idOrdn, idTrgt );
desc202.putReference( idnull, ref209 );
var idT = charIDToTypeID( "T " );
var desc203 = new ActionDescriptor();
var idNm = charIDToTypeID( "Nm " );
desc203.putString( idNm, "mb-dummy tempTestLayerSetOpen_Closed" );
var idLyr = charIDToTypeID( "Lyr " );
desc202.putObject( idT, idLyr, desc203 );
executeAction( idsetd, desc202, DialogModes.NO );
// =======================================================move
var idmove = charIDToTypeID( "move" );
var desc59 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref64 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref64.putEnumerated( idLyr, idOrdn, idTrgt );
desc59.putReference( idnull, ref64 );
var idT = charIDToTypeID( "T " );
var ref65 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
ref65.putIndex( idLyr, idxx );
desc59.putReference( idT, ref65 );
var idAdjs = charIDToTypeID( "Adjs" );
desc59.putBoolean( idAdjs, false );
var idVrsn = charIDToTypeID( "Vrsn" );
desc59.putInteger( idVrsn, 5 );
executeAction( idmove, desc59, DialogModes.NO );
}
function deleteTempLayerSetbyIdx(idxx){
// =======================================================
var idDlt = charIDToTypeID( "Dlt " );
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIndex(charIDToTypeID( 'Lyr ' ), idxx);
// ref.putIdentifier(charIDToTypeID( 'Lyr ' ), idxx);
desc.putReference( charIDToTypeID( 'null' ), ref );
executeAction( idDlt, desc, DialogModes.NO );
}
toogleOpenCloseSet();