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

Find a specific Sublayer and move to top level layer

Engaged ,
Oct 21, 2014 Oct 21, 2014

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

TOPICS
Scripting

Views

2.0K

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

Guide , Oct 23, 2014 Oct 23, 2014

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

...

Votes

Translate

Translate
Adobe
Guide ,
Oct 21, 2014 Oct 21, 2014

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

  }

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
Engaged ,
Oct 22, 2014 Oct 22, 2014

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.

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 22, 2014 Oct 22, 2014

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

}

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
Engaged ,
Oct 23, 2014 Oct 23, 2014

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 

}

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
Engaged ,
Oct 23, 2014 Oct 23, 2014

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

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 23, 2014 Oct 23, 2014

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

  }

  }   

  }

}

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
Engaged ,
Oct 24, 2014 Oct 24, 2014

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.

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 24, 2014 Oct 24, 2014

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...

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 ,
Jun 09, 2015 Jun 09, 2015

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.textFrames.length);

                                         for(var j = 0; j < doc.layers.layers.textFrames.length; j++)

                                         {

                                        var str = doc.layers.layers.textFrames.contents;   

                                        $.writeln("Text :::",str," ::Size :::",doc.layers.layers.textFrames.textRange.characterAttributes.size," ::Text Font :::",

                                        doc.layers.layers.textFrames.textRange.characterAttributes.textFont);

                                        var haystack = doc.layers.layers.textFrames.textRange.characterAttributes.textFont; 

                                        var textSize= doc.layers.layers.textFrames.textRange.characterAttributes.size;

                                        $.writeln("doc.layers.layers.textFrames.anchor[0] :::",doc.layers.layers.textFrames.anchor[0]);

                                        $.writeln("doc.layers.layers.textFrames.anchor[1] :::",doc.layers.layers.textFrames.anchor[1]);

                                }          

                            }

                           

****

Please direct me in the right way....



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 ,
Jun 09, 2015 Jun 09, 2015

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...

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 ,
Jun 10, 2015 Jun 10, 2015

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.

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 ,
Jun 10, 2015 Jun 10, 2015

Copy link to clipboard

Copied

LATEST

‌glad I could help

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