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
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
...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.
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.
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.
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?
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();
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:
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.
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.
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.
Copy link to clipboard
Copied
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.