Copy link to clipboard
Copied
Hello, community.
For a while now, I've been using ChatGPT to help me do something I wasn't able before - write the Illustrator scripts I need to improve the efficiency of my workflows. That being said, I always run across one problem that I wasn't able to fix with my limited coding knowledge.
I cannot figure out how to reference top-level layers in illustrator. For example, I've been trying to create a script that renames all selected layers, based on a user's input string. However, when I run the script, it doesn't actually rename the selected layers, but the first item that is held within each of those layers. It's driving me crazy.
Looking forward to hearing your thoughts on this. Thank you very much for your time!
Copy link to clipboard
Copied
Without seeing your code nobody can tell you where you go wrong, but it simply sounds liek you don't have any actual object type checks in your script and then things go *kerplang* because a wrong data type is encountered.
Mylenium
Copy link to clipboard
Copied
You're right. My apologies for not including code. Here is such a quick code snippet, that's supposed to change the name of selected top-level layers based on a string the user inputs:
// Define variables
var doc = app.activeDocument;
var selection = doc.selection;
// Check if there is a selection
if (selection.length == 0) {
// If no selection, show an error message
alert("No selection was made. Please select one or more items to rename.");
} else {
// If there is a selection, show a dialog window to input the new name
var newName = prompt("Enter the new name:", "");
if (newName != null) {
// Loop through the selection and rename each item
for (var i = 0; i < selection.length; i++) {
selection[i].name = newName;
}
}
}
Let me know if you spot any mistake that could help me out in my case. Thank you very much!
Copy link to clipboard
Copied
Layers are not selectable, as evidenced by the fact that they do not have a "selected" property and are not added to the "selection" collection. (What you do when you "select" a layer is select the items on the layer.) This should loop through the layers and name those that contain selected items:
var doc = app.activeDocument;
var newName = prompt("Enter the new name:", "");
if (newName != null) {
for (var i = 0; i < doc.layers.length; i++) {
if (doc.layers[i].hasSelectedArtwork) {
doc.layers[i].name = newName;
}
}
}
Copy link to clipboard
Copied
So in this case, there's absolutely no way to reference these layers? For example, I was thinking of creating another script that basically reorders all my layers (in the layers panel) based on their corresponding artboard number. Would something like that be doable?
Copy link to clipboard
Copied
I was thinking of creating another script that basically reorders all my layers (in the layers panel) based on their corresponding artboard number.
By @Eduard Buta
You could check out whether Alexander Aldygin's scripts can do what you need: https://github.com/alexander-ladygin/illustrator-scripts
Copy link to clipboard
Copied
What you ask is a problem because JSX’s `app.activeDocument.selection` gives you the currently selected PageItem objects, not the Layer objects highlighted in the Layers palette. If you can think of some other way for a script to identify the layers to be renamed, that may be achievable via JSX.
Copy link to clipboard
Copied
When retrieving the selected layers in the layer panel via script, I did the following steps.
Copy link to clipboard
Copied
Any chance you could paste a code snippet that does what you mentioned here? It's hard for a non-coder to visualize how this would look like in code.
Copy link to clipboard
Copied
That is almost all of the scripts for sale. And it is not for beginners, because it is like a miracle that makes the impossible possible.
You can buy it here, and if you do, the source code can be read.
Illustrator script to select contents of multiple layers selected in a panel | note
Copy link to clipboard
Copied
I was thinking of creating another script that basically reorders all my layers (in the layers panel) based on their corresponding artboard number.
By @Eduard Buta
Can you provide more details on what you are trying to do, preferably with screenshots of before and after?
Copy link to clipboard
Copied
I'll try to explain it a little bit better.
For example, when I'm working with logo files, I create a whole lot of variations. Each logo option is added to a document, and each one is wrapped in a group.
Then, I run a script that creates artboards around every group in the document, so now I have a bunch of artboards around every one of my logo options. Problem is, all those options are still on one layer in the layers panel, and I'd prefer them to be separate, each with its own layer, so I can correctly categorize them and rename them.
That's when I run a script that loops through all artboards, detects the items within each artboard, and moves them to their own layers. So now I have a bunch of artboards and a bunch of corresponding layers, right?
I now finally run another script that sorts artboards based on their viewport position, from top left to bottom. So I suddenly have my artboards ordered the way I want them to, based on the position I see on the screen.
However, this script doesn't also sort their corresponding layers. So while my artboards are neatly sorted from 1 to X, my layers are all pretty much randomly ordered, based on how the script initially created them. I'd like a way to sort these layers as well, based on their corresponding artboard number.
Hope this made better sense? Let me know!