Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joonaspaakko/1daa42a3ab01b8ce79fd742f755ab6b1 to your computer and use it in GitHub Desktop.
Save joonaspaakko/1daa42a3ab01b8ce79fd742f755ab6b1 to your computer and use it in GitHub Desktop.
/*
NOTE:
1. Makes the assumption that only one layer is selected (only really matters when going down)
2. Doesn't care if the adjacent layer is a group or not
*/
var options = {
direction: 'up',
loop: true,
};
var doc = app.activeDocument;
var originalLayer = doc.activeLayer;
var originalLayerId = originalLayer.id;
var siblings = originalLayer.parent.layers;
for (var layerIndex = 0; layerIndex < siblings.length; layerIndex++) {
var layer = siblings[layerIndex];
var layerId = layer.id;
// Found selected layer (again... in the array of its siblings, so we know which layers are next to it)
if ( layerId === originalLayerId ) {
// Find adjacent layer by adding or subtracting 1
var adjacentIndex;
if ( options.direction === 'up' ) {
adjacentIndex = layerIndex - 1;
} else {
adjacentIndex = layerIndex + 1;
}
// Wrap around
if ( options.loop ) {
if ( adjacentIndex < 0 ) adjacentIndex = siblings.length - 1;
if ( adjacentIndex >= siblings.length ) adjacentIndex = 0;
}
// Clamp to siblings length
else {
if ( adjacentIndex < 0 ) adjacentIndex = 0;
if ( adjacentIndex >= siblings.length ) siblings.length - 1;
}
// Select adjacent layer if it exists
var adjacentLayer = siblings[ adjacentIndex ];
// if ( adjacentLayer ) doc.activeLayer = adjacentLayer;
if ( adjacentLayer ) selectLayerById( adjacentLayer.id );
}
}
function cTID(s) { return app.charIDToTypeID(s); };
function sTID(s) { return app.stringIDToTypeID(s); };
function selectLayerById( id ) {
var result = false;
try {
var ref = new ActionReference();
ref.putIdentifier(cTID('Lyr '), id);
var desc = new ActionDescriptor();
desc.putReference(cTID('null'), ref);
// Retain layer visibility
desc.putBoolean(sTID('makeVisible'), checkLayerStateById( id, 'visible'));
const groupExpanded = checkLayerStateById( id, 'expanded');
// Select layer
executeAction(cTID('slct'), desc, DialogModes.NO);
// Keep group expanded or not
expandGroupById( id, groupExpanded);
result = true;
} catch(e) {}
return result;
}
function checkLayerStateById( id, state ) {
if ( state === 'expanded' ) state = 'layerSectionExpanded';
var ref = new ActionReference();
ref.putProperty( cTID("Prpr"), sTID( state ));
ref.putIdentifier( cTID( "Lyr " ), id );
try { return executeActionGet(ref).getUnitDoubleValue( sTID( state ) ) == 1 ? true : false; } catch(e) { return undefined }
}
function expandGroupById( id, expand, nested ) {
var ref = new ActionReference();
var desc = new ActionDescriptor();
ref.putProperty(cTID('Prpr'), sTID('layerSectionExpanded'));
ref.putIdentifier( cTID( "Lyr " ), id );
desc.putBoolean(sTID('to'), !!expand);
desc.putBoolean(sTID('recursive'), !!nested );
desc.putReference(cTID('null'), ref);
executeAction(cTID('setd'), desc, DialogModes.NO);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment