Skip to main content
Known Participant
July 5, 2020
Answered

Script to move selected items to a named layer?

  • July 5, 2020
  • 4 replies
  • 3975 views

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

This topic has been closed for replies.
Correct answer Charu Rajput

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.

4 replies

Mike Witherell
Community Expert
Community Expert
September 24, 2022

What if you wanted to select objects and send to a new layer that doesn't yet exist and you need to give the layer a name as you go?

Mike Witherell
femkeblanco
Legend
September 25, 2022
var doc = app.activeDocument;
var layerName = prompt("");
var _layer = doc.layers.add();
_layer.name = 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();
alexgsrubio_
Known Participant
October 13, 2023

Hello. I've tried to compile this in the Apple Script editor and I always get this Syntax error: A identifier can’t go after this identifier, referring to the first var doc. Any ideas on how to solve this? See attached image:

Known Participant
July 5, 2020

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.

Charu Rajput
Community Expert
Charu RajputCommunity ExpertCorrect answer
Community Expert
July 5, 2020

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
Charu Rajput
Community Expert
Community Expert
July 5, 2020

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