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

Layer change for text block and calculation script

Explorer ,
Jul 13, 2019 Jul 13, 2019

Hello,

I want to apologize in advance for my English, because I use a translator and do not know if it will be correct.

I would like to have two different scripts but I do not know how to do it.

The first will be a script that isolates the visible texts in the same layer and if possible that duplicates it and vectorizes the new layer.

I already managed to find this:

  1. var myDoc = app.activeDocument; 
  2.  
  3. if(!myDoc.layers.itemByName("textLayer").isValid){myDoc.layers.add({name:"textLayer"})};   
  4. var textLayer = myDoc.layers.itemByName("textLayer"); 
  5. textLayer.locked = false
  6.  
  7. myDoc.spreads.everyItem().textFrames.everyItem().locked = false
  8. myDoc.spreads.everyItem().textFrames.everyItem().itemLayer = textLayer; 

But it takes into account all the texts except the locked ones and creates an error if a text is locked.

For the second, I have unfortunately found nothing.

I would like a script that displays the result of the Width / Height and Height / Width calculation of the selected block.

I thank everyone in advance who will take the time to help me.

TOPICS
Scripting
1.4K
Translate
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 15, 2019 Jul 15, 2019

Try the following version

var myDoc = app.activeDocument

var layers = myDoc.layers.everyItem().getElements()

if(!myDoc.layers.itemByName("textLayer").isValid){myDoc.layers.add({name:"textLayer"})};

var textLayer = myDoc.layers.itemByName("textLayer");

var lockedLayers = []

for(var i = 0; i < layers.length; i++)

{

    if(layers.name == "textLayer" || layers.visible == false)

          continue;

    //Collect all the layers that are locked and unlock them

    if(layers.locked == true)

    {

        

...
Translate
Community Expert ,
Jul 13, 2019 Jul 13, 2019

Hi Kyvarit,

I used your script and it works fine for me with locked text frames as well. However it will give an error in case the textframes are placed on a locked layer. Can you share the error message that you get, also can you run your tests to see whether any layer in your document is locked. You could also share the document that gives you error by uploading it on Dropbox and pasting the public layer here

In order to unlock layers on which these text elements are placed you could use a code like below

var layers = myDoc.spreads.everyItem().textFrames.everyItem().itemLayer

Then you could iterate over all the elements in layers array, set their locked property to false and then try to change their layer to the new layer you are creating

-Manan

Translate
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 13, 2019 Jul 13, 2019

Erreur Script.jpg

In my case, whether it is the layer or the texts that are locked, I have this error.

There are two problems with my script.

1 / It blocks on blocks / layers locked.

2 / It does not ignore the invisible layers, so I can find myself with non-visible texts made visible during the execution of the script.

Translate
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 13, 2019 Jul 13, 2019

For me it works as i mentioned. Can you share a sample document? Upload it on Dropbox or any such cloud service and post the public URL here.

-Manan

Translate
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 13, 2019 Jul 13, 2019

Try the following code, it should work as per my description above. I have made provisions to avoid moving textframes from layers which are invisible. The logic is explained via comments in the code

var myDoc = app.activeDocument

var layers = myDoc.layers.everyItem().getElements()

if(!myDoc.layers.itemByName("textLayer").isValid){myDoc.layers.add({name:"textLayer"})};

var textLayer = myDoc.layers.itemByName("textLayer");

var lockedLayers = []

for(var i = 0; i < layers.length; i++)

{

     if(layers.name == "textLayer" || layers.visible == false)

          continue;

     //Collect all the layers that are locked and unlock them

     if(layers.locked == true)

     {

          lockedLayers.push(layers)

          layers.locked = false

     }

     var pi = layers.allPageItems //Pageitems present on the layer

     for(var j = 0; j < pi.length; j++)

     {

          if(pi.constructor.name != "TextFrame" || pi.parent.constructor.name == "Character")  //Avoid pageitems other than textframe or inline textframes

               continue;

          pi.locked = false

          pi.itemLayer = textLayer

     }

}

//Lock the layers that were originally locked

for(var i = 0; i < lockedLayers.length; i++)

     lockedLayers.locked = true

-Manan

Translate
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 13, 2019 Jul 13, 2019

Thank you it's great! I can read the script and comments, I do not understand much ^^,

If anyone has an idea for the second script.

Translate
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 13, 2019 Jul 13, 2019
Translate
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 13, 2019 Jul 13, 2019

I tried the script i gave above on the document and it works fine. Try it

-Manan

Translate
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 13, 2019 Jul 13, 2019

So I'm having a new problem.

There is an error number "30615" that occurs when there is a text block grouped with another block (text or not)

I changed the text file. with all the tests I did, it was no longer correct.

In any case already thank you, because it is already a very big progress for me this script.

Translate
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 14, 2019 Jul 14, 2019

What is required when the we have a textframe within a group, should the whole group move to the new layer or the group ungrouped and the textframe moved to the new layer. How about nested groups, how should they be handled.

-Manan

Translate
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 14, 2019 Jul 14, 2019

The goal for me and to have the isolated texts on the same layer in order to be able to vectorize them quickly while keeping a copy of the layer for modification.

So in any case, I would like visible text objects to be on the same layer.

Translate
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 15, 2019 Jul 15, 2019

Try the following version

var myDoc = app.activeDocument

var layers = myDoc.layers.everyItem().getElements()

if(!myDoc.layers.itemByName("textLayer").isValid){myDoc.layers.add({name:"textLayer"})};

var textLayer = myDoc.layers.itemByName("textLayer");

var lockedLayers = []

for(var i = 0; i < layers.length; i++)

{

    if(layers.name == "textLayer" || layers.visible == false)

          continue;

    //Collect all the layers that are locked and unlock them

    if(layers.locked == true)

    {

          lockedLayers.push(layers)

          layers.locked = false

    }

    var pi = layers.allPageItems //Pageitems present on the layer

    for(var j = 0; j < pi.length; j++)

    {

          if(pi.constructor.name != "TextFrame" || pi.parent.constructor.name == "Character")  //Avoid pageitems other than textframe or inline textframes

            continue;

          while(pi.parent.constructor.name == "Group")

              pi.parent.ungroup()

          pi.locked = false

          pi.itemLayer = textLayer

    }

}

//Lock the layers that were originally locked

for(var i = 0; i < lockedLayers.length; i++)

    lockedLayers.locked = true

-Manan

Translate
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 16, 2019 Jul 16, 2019
LATEST

Thank you very much Manan!

It works very well.

Translate
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