Created
May 10, 2018 18:49
-
-
Save unycone/af2d07d689c18c14dce91ed3f5bf77b2 to your computer and use it in GitHub Desktop.
A photoshop script to flatten hierarchy of layer groups without merging layers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Extract layers (flatten layer hierarchy, without merging) and remove empty layers | |
// Notice: layer effects on groups are not concerened | |
#target photoshop | |
if (!app.documents.length) { | |
alert('No document is opened', 'No document'); | |
} | |
else { | |
app.activeDocument.suspendHistory('Flatten Layer Hierarchy', 'processLayers()'); | |
} | |
function processLayers() | |
{ | |
while (app.activeDocument.layerSets.length) { | |
extractLayers(app.activeDocument); | |
} | |
removeEmptyLayers (app.activeDocument); | |
} | |
function extractLayers(doc) | |
{ | |
var toRemove = []; | |
for (var i = doc.layerSets.length-1; i >= 0; i--) { | |
//$.writeln(doc.layerSets[i].name); | |
var parent = doc.layerSets[i]; | |
var children = parent.layers; | |
var toMove = []; | |
for (var j = 0; j < children.length; j++) { | |
toMove.push(children[j]); | |
} | |
for (var j = 0; j < toMove.length; j++) { | |
moveLayerToLayerSet(toMove[j].id, parent.id); | |
} | |
toRemove.push(parent); | |
} | |
for (var i = 0; i < toRemove.length; i++) { | |
toRemove[i].remove(); | |
} | |
} | |
function moveLayerToLayerSet( fromID, toID ){ | |
var desc = new ActionDescriptor(); | |
var ref = new ActionReference(); | |
ref.putIdentifier( charIDToTypeID('Lyr '), Number(fromID) ); | |
desc.putReference( charIDToTypeID('null'), ref ); | |
var ref2 = new ActionReference(); | |
ref2.putIndex( charIDToTypeID('Lyr '), getLayerIndexByID(toID) ); | |
desc.putReference( charIDToTypeID('T '), ref2 ); | |
desc.putBoolean( charIDToTypeID('Adjs'), false ); | |
desc.putInteger( charIDToTypeID('Vrsn'), 5 ); | |
try{ | |
executeAction( charIDToTypeID('move'), desc, DialogModes.NO ); | |
} catch(e) { | |
alert(e); | |
} | |
} | |
function getLayerIndexByID(ID){ | |
var backgroundCounter = 0; | |
if (app.activeDocument.artLayers.length) { | |
backgroundCounter = activeDocument.artLayers[activeDocument.artLayers.length-1].isBackgroundLayer ? 1 : 0; | |
} | |
var ref = new ActionReference(); | |
ref.putIdentifier( charIDToTypeID('Lyr '), ID ); | |
return executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-backgroundCounter; | |
} | |
function isEmptyLayer(layer) { | |
if (layer.kind == LayerKind.TEXT) { | |
if ("" == layer.textItem.contents) { | |
return true; | |
} | |
} | |
else { | |
if (layer.bounds[2] == 0 && layer.bounds[3] == 0) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function removeEmptyLayers(doc) { | |
for (var i = doc.artLayers.length - 1; i >= 0; i--) { | |
if (isEmptyLayer(doc.artLayers[i])) { | |
if (app.activeDocument.layers.length > 1) { | |
doc.artLayers[i].remove(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment