Skip to main content
Max Mugen
Inspiring
February 10, 2023
Answered

Script move active layer

  • February 10, 2023
  • 2 replies
  • 1973 views

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? 

This topic has been closed for replies.
Correct answer BryanPagenkopf

@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();

2 replies

BryanPagenkopfCorrect answer
Inspiring
February 10, 2023

@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();
Max Mugen
Max MugenAuthor
Inspiring
February 10, 2023

Works like a charm ! 
thank you very much 🙂 

Thank you also for commenting the code, so I can learn, tweak an modify 
very appreciated 😊 

-- maxmugen.com
Known Participant
February 18, 2023
Imaginerie
Community Expert
Community Expert
February 10, 2023

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 🙂 )

Max Mugen
Max MugenAuthor
Inspiring
February 10, 2023

oh, that would be nice indeed 
just tried it, didn't work for me, 
I've tried every combination with ctrl, shift, alt + "+" 

-- maxmugen.com
Imaginerie
Community Expert
Community Expert
February 10, 2023

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...