Skip to content

Instantly share code, notes, and snippets.

@jdaiv
Created December 29, 2017 09:02
Show Gist options
  • Save jdaiv/282bc0f1049032f5e6354b143ee2af2e to your computer and use it in GitHub Desktop.
Save jdaiv/282bc0f1049032f5e6354b143ee2af2e to your computer and use it in GitHub Desktop.
var currentDoc = app.activeDocument
try {
// thanks https://stackoverflow.com/questions/27255364/getting-selected-layer-or-group-layers-array-using-javascript-photoshop-cs4 !
var descGrp = new ActionDescriptor()
var refGrp = new ActionReference()
refGrp.putEnumerated(
charIDToTypeID('Lyr '),
charIDToTypeID('Ordn'),
charIDToTypeID('Trgt'))
descGrp.putReference(charIDToTypeID('null'), refGrp)
executeAction(
stringIDToTypeID('groupLayersEvent'),
descGrp,
DialogModes.NO);
var layers = []
var activeLayers = currentDoc.activeLayer.layers
for (var i = 0; i < activeLayers.length; i++) {
layers.push(activeLayers[i])
}
// undo
currentDoc.activeHistoryState =
currentDoc.historyStates[currentDoc.historyStates.length - 2];
currentDoc.suspendHistory('Create groups from selected layers',
'run(currentDoc, layers)')
} catch (e) {
alert('Error executing script: ' + e)
}
function run (doc, layers) {
for (var i = 0; i < layers.length; i++) {
var layer = layers[i]
if (layer.isBackgroundLayer) return
var newFolder = doc.layerSets.add()
newFolder.name = layer.name
newFolder.move(layer, ElementPlacement.PLACEBEFORE)
layer.move(newFolder, ElementPlacement.INSIDE)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment