Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Script move active layer

Participant ,
Feb 10, 2023 Feb 10, 2023

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/
TOPICS
Scripting
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Feb 10, 2023 Feb 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 
...
Translate
Adobe
Community Expert ,
Feb 10, 2023 Feb 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 🙂 )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 10, 2023 Feb 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 + "+" 


-
www.instagram.com/mugen_labz/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 10, 2023 Feb 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...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 10, 2023 Feb 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();
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 10, 2023 Feb 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 😄 


-
www.instagram.com/mugen_labz/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 18, 2023 Feb 18, 2023
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Feb 18, 2023 Feb 18, 2023

Illustrator scripting and photoshop scripting are different.  You are unlikely to have a photoshop scripting problem solved on here. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 18, 2023 Feb 18, 2023
LATEST

thank to your reply... i need to photoshop script

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines