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

Script to move selected items to a named layer?

Explorer ,
Jul 05, 2020 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

TOPICS
Scripting

Views

2.5K

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
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 05, 2020 Jul 05, 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

...

Votes

Translate

Translate
Adobe
Community Expert ,
Jul 05, 2020 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

Votes

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
community guidelines
Community Expert ,
Jul 05, 2020 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

Votes

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
community guidelines
Explorer ,
Jul 05, 2020 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.

Votes

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
community guidelines
Community Expert ,
Sep 24, 2022 Sep 24, 2022

Copy link to clipboard

Copied

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

Votes

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
community guidelines
Guide ,
Sep 24, 2022 Sep 24, 2022

Copy link to clipboard

Copied

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

Votes

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
community guidelines
Community Beginner ,
Oct 12, 2023 Oct 12, 2023

Copy link to clipboard

Copied

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:Screen Shot 2023-10-12 at 18.07.53.png

Votes

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
community guidelines
Community Expert ,
Oct 12, 2023 Oct 12, 2023

Copy link to clipboard

Copied

Looks like a jsx file, not Apple script. Try placing it in a text editor and save it as plain text (not RTF) with a .jsx extension.

Votes

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
community guidelines
Guide ,
Oct 14, 2023 Oct 14, 2023

Copy link to clipboard

Copied

The error implies that the script is either not saved in the right format or not being run in the right way.

 

  1. The script should be saved with a .jsx extension. Copy the script to a plain text file and save with a .jsx extension. I do not use a Mac, but I understand that Apple's TextEdit, by default, will save as rich text; this is not what you want; you want plain text...
  2. The script should be run from illustrator, by going through File > Scripts. 

Votes

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
community guidelines
Community Expert ,
Oct 14, 2023 Oct 14, 2023

Copy link to clipboard

Copied

(macOS) TextEdit > TextEdit menu > Settings > New Document > and choose Plain Text. Then close out the Settings dialog box. Now you can use TextEdit to make plain text scripts.

Mike Witherell

Votes

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
community guidelines
Community Expert ,
Oct 14, 2023 Oct 14, 2023

Copy link to clipboard

Copied

LATEST

Mike is correctly showing the way to change the default for TextEdit to plain text.

If you only need that occasionally, you can create a new file and choose Format > Make Plain Text before pasting the script.

Votes

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
community guidelines