PS script to switch between layers
Copy link to clipboard
Copied
I'm searching since months for a solution to use a shortcut to select the one layer above the selected or the layer underneath the selected one.
Because I dodge & burn with curves it's taking ages otherwise, And yes I know about the 50% grey layer 😉
If you have any idea, I know there was a script online called ToggleDBLayers but it's not online any more.
Happy for help!
Michael
Explore related tutorials & articles
Copy link to clipboard
Copied
Are you using a non-english keyboard with english Photoshop?
Because there are default keyboard shortcuts – which may be no use in aforementioned case.
Scripting-wise these should work:
//select next layer below;
var idslct = charIDToTypeID( "slct" );
var desc6 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref4 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idBckw = charIDToTypeID( "Bckw" );
ref4.putEnumerated( idLyr, idOrdn, idBckw );
desc6.putReference( idnull, ref4 );
var idMkVs = charIDToTypeID( "MkVs" );
desc6.putBoolean( idMkVs, false );
executeAction( idslct, desc6, DialogModes.NO );
//select next layer above;
var idslct = charIDToTypeID( "slct" );
var desc7 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref5 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idFrwr = charIDToTypeID( "Frwr" );
ref5.putEnumerated( idLyr, idOrdn, idFrwr );
desc7.putReference( idnull, ref5 );
var idMkVs = charIDToTypeID( "MkVs" );
desc7.putBoolean( idMkVs, false );
executeAction( idslct, desc7, DialogModes.NO );
Copy link to clipboard
Copied
Hey, thanks for your quick reply!
I'm using a German one.
So can I use any shortcut which I would like to select or do I have to change it in the script?
Sorry for asking a very newbie question.
Copy link to clipboard
Copied
Save the two scripts as jsx-files into the Presets/Scripts-Folder and the next time you start Photoshop you should be able to assign whatever shortcuts to them via Edit > Keyboad Shortcuts.
Copy link to clipboard
Copied
The built-in keyboard shortcuts and the scripting listener code are limited to visible layers. So if the eye icon is off for a particular layer, it will be skipped.
This may or may not be an issue depending on the situation.
If you need to select the next layer, regardless of visibility, then there are two scripts to do so:
BCM01_ selectBack.jsx
BCM02_ selectForward.jsx
Copy link to clipboard
Copied
And another option here, which is self contained (the previous scripts require another "library" script to be installed):
Copy link to clipboard
Copied
I just knocked these up!
EDIT: Does not work with layer groups/sets
Select the backward layer, regardless of visibility:
/*
Select Backward layer by Index.jsx
v1.0 - Stephen Marsh, 1st September 2021
Select the backward layer, relative to the current selected layer, regardless of visibility
Note:
Does not cycle through the bottom layer to the top layer
Does not work with layer sets/groups
*/
var backwardLayer = (getActiveLayerIndex() - 1);
selectLayerByIndex(backwardLayer);
function selectLayerByIndex(index) {
/* https://github.com/ES-Collection/Photoshop-Scripts/blob/master/Remove%20Unused%20Layers.jsx */
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), index);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref);
desc.putBoolean(charIDToTypeID("MkVs"), false);
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO);
}
function getActiveLayerIndex() {
/* https://github.com/Paul-Riggott/PS-Scripts/blob/master/getLayersetLayerIDs.jsx */
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
try {
activeDocument.backgroundLayer;
return executeActionGet(ref).getInteger(charIDToTypeID("ItmI")) - 1;
} catch (e) {
return executeActionGet(ref).getInteger(charIDToTypeID("ItmI"));
}
}
Select the forward layer, regardless of visibility:
/*
Select Forward layer by Index.jsx
v1.0 - Stephen Marsh, 1st September 2021
Select the forward layer, relative to the current selected layer, regardless of visibility
Note:
Does not cycle through the top layer to the bottom layer
Does not work with layer sets/groups
*/
var forwardLayer = (getActiveLayerIndex() + 1);
selectLayerByIndex(forwardLayer);
function selectLayerByIndex(index) {
/* https://github.com/ES-Collection/Photoshop-Scripts/blob/master/Remove%20Unused%20Layers.jsx */
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), index);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref);
desc.putBoolean(charIDToTypeID("MkVs"), false);
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO);
}
function getActiveLayerIndex() {
/* https://github.com/Paul-Riggott/PS-Scripts/blob/master/getLayersetLayerIDs.jsx */
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
try {
activeDocument.backgroundLayer;
return executeActionGet(ref).getInteger(charIDToTypeID("ItmI")) - 1;
} catch (e) {
return executeActionGet(ref).getInteger(charIDToTypeID("ItmI"));
}
}
Copy link to clipboard
Copied
Thank you, sorry for asking, but which keys do I use for switching the layers?
Copy link to clipboard
Copied
These are just scripts, after you install them in the presets scripts folder, and restart Photoshop – you can then assign custom keyboard shortcuts to them. The key binding/assignment is up to you.
Copy link to clipboard
Copied
A question for scripters: it seems that some apps let their scripts appear in right-click menus, like Br, I think some even allow the scripts to appear in menus, is it possible at all in Photoshop, or did you guys FR it?
Copy link to clipboard
Copied
I don't believe that the right-click is available for Photoshop, but I have seen some scripts installed in a custom location in the File > Scripts menu. This is not an area that I am strong in, so I too will be interested in the replies!
EDIT: Found it –
Copy link to clipboard
Copied
You can add them also to Filter menu of Photoshop as well as plugins if I remember well.
Copy link to clipboard
Copied
@PECourtejoie , @DavideBarranca gives an example of how to make a Script appear in the Filter menu here:
Copy link to clipboard
Copied
I also found this as well:
Execute script from filter menu
I added the code in top of the one of my script then put the script to the phooshop scripts folder. it worked.
I have a question does anybody know if it possible to grouple multiply scripts under one section.
Copy link to clipboard
Copied
I have a question does anybody know if it possible to grouple multiply scripts under one section.
By wanokuni
@wanokuni – Yes, Adobe has some default scripts that show this behaviour in File > Scripts:
_______________________
Flatten All Layer Effects
Flatten All Masks
_______________________
They are using the <category> tag:
<category>Flatten</category>
This is all documented in the Photoshop JavaScript Reference PDF under "JavaScript Resource", page 191 in the 2020 version PDF. It really helps to cross reference the code from actual scripts against this info.
Copy link to clipboard
Copied
When I put the script under the automate menu it did group them, but under the filder menu it didn not work.
_______________________
Add Script 1
Add Script 2
_______________________
<menu>automate</menu>
<category>Add</category>
It would be nice ti be able to group punch of my script under one group like my scripts and inside all my scripts.
I have read the JavaScript Resource", page 191 in the 2020 version PDF. I am still learning javascript about a month now :). do all the scripts have to have start with same name in in the script and in the title of the script?
Copy link to clipboard
Copied
Just for the sake of completeness, in the English locale, with an english keyboard, one can use Option-Shift-[ (Mac) or Alt+Shift+[ (PC) to select layers below or Option-Shift-] (Mac) or Alt+Shift+] to select layers above the current layer.
BTW, does the following work in German KB with the German Locale:
Nächste Ebene darunter/darüber auswählen |
Alt + , (Komma) oder . (Punkt) |
Wahltaste + , (Komma) oder . (Punkt) |
from: Standard-Tastaturbefehle
Copy link to clipboard
Copied
I know, my problem is that I'm using a German keyboard with an English Photoshop version.
Copy link to clipboard
Copied
Here is what I have done so far, it will toggle between the dodge and burn layer, but only when one of them is selected, if you have other layer selected it won't work, I am still learing javascript so, maybe someone can help with this issue.
const doc = app.activeDocument;
// Get layer by name
var dodgeLayer = doc.artLayers.getByName("Dodge");
var burnLayer = doc.artLayers.getByName("Burn");
if (doc.activeLayer == dodgeLayer) {
try {
doc.activeLayer = burnLayer;
} catch (e) {}
} else if (doc.activeLayer == burnLayer) {
try {
doc.activeLayer = dodgeLayer;
} catch (e) {}
}
Copy link to clipboard
Copied
To always select 'Burn' layer when 'Dodge' or some other is selected, add at end of first condition:
|| doc.activeLayer != burnLayer
Did you unmark my solution you originally marked as correct in: prevent overwrite files when saving
Copy link to clipboard
Copied
Thanks for the tip, i am also thinking about what if yu have background layer selected and the user try to use this script to select either a dodge or brun layer, Thats what I am not able to find a solution for it yet.
I have made your asnwer correct could be a mistake, i just fixed it now.
Copy link to clipboard
Copied
Background is treated as the other layer, so makes no difference. 'Burn' layer gets selected.
Copy link to clipboard
Copied
"I'm searching since months for a solution to use a shortcut to select the one layer above the selected or the layer underneath the selected one."
Are you having problem with Photoshop default Shortcut keys (Alt+[ and Alt+])?
Copy link to clipboard
Copied
those work great when you have a english keybaord and english Photoshop, but when you have a Finnish, swedinsh german... those shortcuts don't work, becuase we have actual letters in the keybaord, Å,Ö, Ä... where the [] are in english keybaord. I have same problem and those shortcuts and they can't be changed in Photoshop so, we either have to using a action or a script.
Copy link to clipboard
Copied
I had to hint the same but found it was posted already: PS script to switch between layers

