Copy link to clipboard
Copied
I have a lot of illustrator files that are organized by a template size. The layers are setup so that the top level layer is always Template. However the sublayer I need to move to the top level could be called 42 Pg Border or 30 Pg Border or 24 Pg Border.
Is there a way for a script to search through the layers and if it finds "42 Pg Border" or "30 Pg Border" or "24 Pg Border" in the sublayer Template it would move it to the top level but if it doesn't see one of those it doesn't give an error?
I'm still new to scripting so the extent of my knowledge on this is to scan all the layers and unlock them. Then look for a specific layer name. It's the sublayer and moving it to the top level that I am not sure of.
Here is my current code:
var doc = app.activeDocument;
var allLayers = doc.layers;
for (var i = allLayers.length-1; i >= 0; i--){
allLayers.locked = false;
if (allLayers.name == "42 Pg Border" || allLayers.name == "30 Pg Border" || allLayers.name == "24 Pg Border") {
alert("I found the " + allLayers.name + " layer")
} else {
alert("Not the right layer")
}
}
Any help would be greatly appreciated!
I am using CS4 on a Windows 64 Bit PC with JavaScript
give this a shot.
its still early and my brain is not fully functioning yet.
bringing all layers to top will (may) mess up stacking order alot.
...var doc = app.activeDocument;
var allLayers = doc.layers;
for (var i = allLayers.length-1; i >= 0; i--){
allLayers.locked = false;
deeper(allLayers);
}
function deeper(parent){
var subLayers = parent.layers;
if (subLayers.length > 0){
for (var i = subLayers.length-1; i >= 0; i--){
subLayers.locked
Copy link to clipboard
Copied
Try This...
var doc = app.activeDocument;
var allLayers = doc.layers;
var count = 0;
for (var i = allLayers.length-1; i >= 0; i--){
allLayers.locked = false;
if (allLayers.name == "42 Pg Border" || allLayers.name == "30 Pg Border" || allLayers.name == "24 Pg Border") {
allLayers.zOrder(ZOrderMethod.BRINGTOFRONT);
++count;
}
}
if (count == 0){
alert("No Border Layer Found");
}
Copy link to clipboard
Copied
I get the alert for No Border Layer Found because it is in a sublayer. The code above only looks through all the layers at the top level.
Copy link to clipboard
Copied
Sorry, I missed the mention of sublayers. even though it was the whole question... OOPS.
Try this.
var doc = app.activeDocument;
var allLayers = doc.layers;
var count = 0;
for (var i = allLayers.length-1; i >= 0; i--){
deeper(allLayers);
}
if (count == 0){
alert("No Border Layer Found");
}
function checkLayer(parent,layer){
layer.locked = false;
if (layer.name == "42 Pg Border" || layer.name == "30 Pg Border" || layer.name == "24 Pg Border") {
layer.move(allLayers[0],ElementPlacement.PLACEBEFORE);
++count;
}
}
function deeper(parent){
var subLayers = parent.layers;
if (subLayers.length > 0){
for (var i = subLayers.length-1; i >= 0; i--){
checkLayer(parent,subLayers);
if (count>0){return}
deeper(subLayers);
}
} return
}
Copy link to clipboard
Copied
OK so that code is really close! I tweaked it a bit. I kept getting an error that said cannot move a locked layer. So I just made a loop to unlock ALL the layers first thing. Then to get rid of some of the "extra" code I wrote I changed this line:
if (layer.name == "42 Pg Border" || layer.name == "30 Pg Border" || layer.name == "24 Pg Border") {
to this.....
if ((layer.name).substr(-6) == "Border") {
So now instead of looking for 42 30 24 (and other border templates I may have to add) I just told it to look at the 6 characters at the end of the string (which will always be the word Border).
OK so now the code runs and does what it is supposed to but I am getting an undefined in my javascript console in extendscript. Any ideas?
Current updated code:
var doc = app.activeDocument;
var allLayers = doc.layers;
var count = 0;
for (var z = allLayers.length-1; z >= 0; z--){
allLayers
.locked = false; }
for (var i = allLayers.length-1; i >= 0; i--){
deeper(allLayers);
}
if (count == 0){
alert("No Border Layer Found");
}
function checkLayer(parent,layer){
if ((layer.name).substr(-6) == "Border") {
layer.move(allLayers[0],ElementPlacement.PLACEBEFORE);
++count;
}
}
function deeper(parent){
var subLayers = parent.layers;
if (subLayers.length > 0){
for (var i = subLayers.length-1; i >= 0; i--){
checkLayer(parent,subLayers);
if (count>0){return}
deeper(subLayers);
}
} return
}
Copy link to clipboard
Copied
Is there a way to script something that will just move ANY/ALL sublayers to a top level layer?
That may work more efficiently for me as the more jobs I get the less consistency I am noticing.
One file has the following layer:
30 Page
> 30 Pg Border
Other files have been formatted:
Template Layers
> 42 Page
> 42 Pg Border
Copy link to clipboard
Copied
give this a shot.
its still early and my brain is not fully functioning yet.
bringing all layers to top will (may) mess up stacking order alot.
var doc = app.activeDocument;
var allLayers = doc.layers;
for (var i = allLayers.length-1; i >= 0; i--){
allLayers.locked = false;
deeper(allLayers);
}
function deeper(parent){
var subLayers = parent.layers;
if (subLayers.length > 0){
for (var i = subLayers.length-1; i >= 0; i--){
subLayers.locked = false;
deeper(subLayers);
if ((subLayers.name).substr(-6) == "Border") {
subLayers.move(allLayers[0],ElementPlacement.PLACEBEFORE);
}
}
}
}
Copy link to clipboard
Copied
That works perfect! Thank you again for your help!
You helped me with this script and my other one. As a thank you I would like to offer something to you. Nothing big but just something to show my gratitude....
I'll message you.
Copy link to clipboard
Copied
I don't require anything, the thanks is good for me.
I'm glad I could help you out.
Let me know if the scripts need any refinement.
especially the save script.
there should be some safe guard like:
if folder does not exist alert.
in case the file name has a typo etc...
I enjoy nutting out simple programming tasks.
and doing it for others gives me ideas on speeding up my own workflow and improving my own scripts.
Thanks for the offer though...
Copy link to clipboard
Copied
Hi QwertyFly,
I've a requirement something similar to this.
I've some text frames inside sublayers.
Main Layer 1
Sublayer 1
Sublayer 2
Sub sub layer 1
Sub sub layer 2
Sublayer 3
Sub sub layer 1
Sub sub layer 2
Sub sub sub layer 1
Sub sub sub layer 2
Sublayer 4
.......
I should traverse into sub and all the sublayers inside sublayers and find the text frames available in those and get those.
I could able to get only to one level of sublayer.
Please see code
****
var doc = activeDocument;
var totalLayers = doc.layers.length;
$.writeln("Print the total layers available in the document:::",totalLayers);
for(var i = 0; i < app.documents.length; i++)
{
for ( var i = 0 ; i < totalLayers ; i++)
{
var currentLayer = doc.layers;
$.writeln("currentLayer.name :::PRINT:::",currentLayer.name);
var subLayers = doc.layers.layers.length;
$.writeln("Print the total subLayers available in the document:::",subLayers);
for(var k = 0; k < doc.layers.layers.length;k++)
{
$.writeln("Total Text Frames available in the sublayer::::",doc.layers.layers
for(var j = 0; j < doc.layers.layers
{
var str = doc.layers.layers
$.writeln("Text :::",str," ::Size :::",doc.layers.layers
doc.layers.layers
var haystack = doc.layers.layers
var textSize= doc.layers.layers
$.writeln("doc.layers.layers
$.writeln("doc.layers.layers
}
}
****
Please direct me in the right way....
Copy link to clipboard
Copied
do you need to go through each layer and sublayer?
just access the textFrames array.
var doc = app.activeDocument;
var texts = doc.textFrames;
var textsQty = texts.length;
var text = [], position = [];
for(var i = 0; i < textsQty; i++){
text = texts.contents;
position = [];
position[0] = texts.top;
position[1] = texts.left;
alert("Frame" + i + "\nPosition (" + position[0] + "," + position[1] + ")\nContents:\n" + text);
}
ps. cant use anchor on most text frames...
Copy link to clipboard
Copied
Vow!!!! thats amazing...Lines of code are very less....
Yeah I have used the code above and able to traverse through each layer and sublayer.
Many thanks for ur timely help Qwertyfly.
Copy link to clipboard
Copied
‌glad I could help