Copy link to clipboard
Copied
Hello, I'm trying to create a script that moves the active layer one position up (or down) in the layer organization.
I've found some scripts to move the active layer to the top of the stack, but not exactly what I'm looking for.
The main idea would be that I don't know or have a predefined list of layers. Just that I'd like to move my current layer one position on the layer list.
Is it possible to code that?
-
www.instagram.com/mugen_labz/
1 Correct answer
@Max Mugen
Give this a Try:
// Get the active document
var doc = app.activeDocument;
// Get an array of all existing layers
var allLayers = [];
for (var i = 0; i < doc.layers.length; i++) {
allLayers.push(doc.layers[i]);
}
// Get the active layer
var activeLayer = doc.activeLayer;
// Get the index of the active layer
var activeLayerIndex = -1;
for (var i = 0; i < allLayers.length; i++) {
if (allLayers[i] == activeLayer) {
activeLayerIndex = i;
break;
}
}
// Create
...
Explore related tutorials & articles
Copy link to clipboard
Copied
Hello!
First a disclaimer, I know nothing about scripting illustrator 🙂
However, I do know that you can move a layer up (or down) one step by hitting the CTRL/CMD + [ or ]
Which you could record as an action.
Based on that, you could script it as well.
I hope it helps (There are lots of very knowledgeable people in here who know how to script, so hang in there 🙂 )
Copy link to clipboard
Copied
oh, that would be nice indeed
just tried it, didn't work for me,
I've tried every combination with ctrl, shift, alt + "+"
-
www.instagram.com/mugen_labz/
Copy link to clipboard
Copied
See the answers below, but just to let you know, sometimes shortcuts depend on the mapping of your keyboard and language. Sometimes even UK/US mappings are different, so it's worth considereing, if you can tell what kind of keyboard you have maybe someone will be able to confirm the right shortcut for the task...
Copy link to clipboard
Copied
@Max Mugen
Give this a Try:
// Get the active document
var doc = app.activeDocument;
// Get an array of all existing layers
var allLayers = [];
for (var i = 0; i < doc.layers.length; i++) {
allLayers.push(doc.layers[i]);
}
// Get the active layer
var activeLayer = doc.activeLayer;
// Get the index of the active layer
var activeLayerIndex = -1;
for (var i = 0; i < allLayers.length; i++) {
if (allLayers[i] == activeLayer) {
activeLayerIndex = i;
break;
}
}
// Create a dialog window to ask the user to move the active layer up or down
var moveUpDownDialog = new Window("dialog", "Move Active Layer");
// Add a button group to the dialog window
var moveUpDownButtonGroup = moveUpDownDialog.add("group");
moveUpDownButtonGroup.orientation = "row";
// Add a "Move Up" button to the button group
var moveUpButton = moveUpDownButtonGroup.add("button", undefined, "Move Up");
moveUpButton.onClick = function() {
// Check if the active layer can be moved up
if (activeLayerIndex > 0) {
// Get the layer above the active layer
var layerAbove = allLayers[activeLayerIndex - 1];
// Move the active layer above the layer above it
activeLayer.move(layerAbove, ElementPlacement.PLACEBEFORE);
// Close the dialog window
moveUpDownDialog.close();
}
};
// Add a "Move Down" button to the button group
var moveDownButton = moveUpDownButtonGroup.add("button", undefined, "Move Down");
moveDownButton.onClick = function() {
// Check if the active layer can be moved down
if (activeLayerIndex < allLayers.length - 1) {
// Get the layer below the active layer
var layerBelow = allLayers[activeLayerIndex + 1];
// Move the active layer below the layer below it
activeLayer.move(layerBelow, ElementPlacement.PLACEAFTER);
// Close the dialog window
moveUpDownDialog.close();
}
};
// Show the dialog window
moveUpDownDialog.show();
Copy link to clipboard
Copied
Works like a charm !
thank you very much 🙂
Thank you also for commenting the code, so I can learn, tweak an modify
very appreciated 😄
-
www.instagram.com/mugen_labz/
Copy link to clipboard
Copied
work perfect......
Plz see this problem
Copy link to clipboard
Copied
Illustrator scripting and photoshop scripting are different. You are unlikely to have a photoshop scripting problem solved on here.
Copy link to clipboard
Copied
thank to your reply... i need to photoshop script

