Highlighted

Script to move selected items to a named layer?

Explorer ,
Jul 05, 2020

Copy link to clipboard

Copied

I have a programmable keyboard that I am using to assign shortcuts and macros to for various programs. One of the things I want to do is move selected objects to a named layer.

I always have four layers consistent throughout most of my documents

I have scripts that select one of these layers when I want to work on them and I've applied a shortcut to it through an action.

var aDoc = app.activeDocument;aDoc.activeLayer = aDoc.layers.getByName("Artwork");

Sometimes while working I forget to change layers and on big projects, this can be problematic and I need a quick way to move the selected objects to a named layer by pressing one key.

I'm sure this can be done with a script but I do not know anything about Javascript, I have read the documentation, but I cannot understand it.

Any help on this would be appreciated

Adobe Community Professional
Correct answer by Charu Rajput | Adobe Community Professional

Another simplified solution : If you know layername will always be "Artwork", then there is no need for dialog that will ask for layername. You can just use simplified code

var doc = app.activeDocument;
var layerName = 'Artwork';
var _layer = doc.layers.getByName(layerName);
var _selectedItems = app.selection
for (var i = _selectedItems.length - 1; i >= 0; i--) {
    _selectedItems[i].move(_layer, ElementPlacement.PLACEATEND);
    _selectedItems[i].selected = false;
}
app.redraw();

 

I hope any of the solution works for you.

Topics

Scripting

Views

90

Likes

Translate

Translate

Report

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

Script to move selected items to a named layer?

Explorer ,
Jul 05, 2020

Copy link to clipboard

Copied

I have a programmable keyboard that I am using to assign shortcuts and macros to for various programs. One of the things I want to do is move selected objects to a named layer.

I always have four layers consistent throughout most of my documents

I have scripts that select one of these layers when I want to work on them and I've applied a shortcut to it through an action.

var aDoc = app.activeDocument;aDoc.activeLayer = aDoc.layers.getByName("Artwork");

Sometimes while working I forget to change layers and on big projects, this can be problematic and I need a quick way to move the selected objects to a named layer by pressing one key.

I'm sure this can be done with a script but I do not know anything about Javascript, I have read the documentation, but I cannot understand it.

Any help on this would be appreciated

Adobe Community Professional
Correct answer by Charu Rajput | Adobe Community Professional

Another simplified solution : If you know layername will always be "Artwork", then there is no need for dialog that will ask for layername. You can just use simplified code

var doc = app.activeDocument;
var layerName = 'Artwork';
var _layer = doc.layers.getByName(layerName);
var _selectedItems = app.selection
for (var i = _selectedItems.length - 1; i >= 0; i--) {
    _selectedItems[i].move(_layer, ElementPlacement.PLACEATEND);
    _selectedItems[i].selected = false;
}
app.redraw();

 

I hope any of the solution works for you.

Topics

Scripting

Views

91

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 05, 2020 0
Adobe Community Professional ,
Jul 05, 2020

Copy link to clipboard

Copied

Hi,

You can try following snippet.

function main() {
    dlg = new Window('dialog', 'Move Selected Items');
    dlg.margin = [10, 10, 10, 10];

    row = dlg.add('group', undefined, '');
    var dirSt = row.add('statictext', undefined, "Enter layer name in which you want to move");
    //dirSt.size = [110, 20];
    var layerNameField = row.add("edittext", [0, 0, 200, 20], "");
    
    row = dlg.add('group', undefined, '');
    row.orientation = 'row';
    row.alignment = 'Right'

    //Cancel Button
    var cancel = row.add('button', undefined, 'Cancel', {
        name: 'Cancel'
    });
    cancel.onClick = function () {
        dlg.close();
    };

    //Move Button
    var moveButton = row.add('button', undefined, 'Move', {
        name: 'Move'
    });

    layerNameField.onChange = function(){
        if(layerNameField.text == ''){
            moveButton.enabled = false;
        }
    }

    moveButton.onClick = function () {
        var doc = app.activeDocument;
        var layerName = layerNameField.text;
        var _layer = doc.layers.getByName(layerName);
        var _selectedItems = app.selection
        for (var i = _selectedItems.length - 1; i >=0; i--) {
            _selectedItems[i].move(_layer, ElementPlacement.PLACEATEND);
            _selectedItems[i].selected = false;
        }
        app.redraw();
        dlg.close();
    };

    dlg.show()
}

main();

 

Above script will always ask you to enter name of the layer in which you want to move all selected items. Enter name of tha layer as in "Layer" panel and click on "Move" button.

 

Let us know if this works for you.

Best regards

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 05, 2020 0
Adobe Community Professional ,
Jul 05, 2020

Copy link to clipboard

Copied

Another simplified solution : If you know layername will always be "Artwork", then there is no need for dialog that will ask for layername. You can just use simplified code

var doc = app.activeDocument;
var layerName = 'Artwork';
var _layer = doc.layers.getByName(layerName);
var _selectedItems = app.selection
for (var i = _selectedItems.length - 1; i >= 0; i--) {
    _selectedItems[i].move(_layer, ElementPlacement.PLACEATEND);
    _selectedItems[i].selected = false;
}
app.redraw();

 

I hope any of the solution works for you.

Best regards

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 05, 2020 2
Explorer ,
Jul 05, 2020

Copy link to clipboard

Copied

Thank you! Yes, I will always know my layer names and I can set up the document ahead of time with the relevant script, especially if its a big project.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 05, 2020 1