Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

Object Style to New Layer

Return to Member Forum

  • Author
    Posts
    • #70933
      Aaron Troia
      Participant

      I was wondering if there was a way to move only text boxes with a specific object style to a new layer through out a document, without, of course, doing it all by hand. Or maybe someone already has a script out there that does that and I’m just not running across it.

    • #71461
      Oleh Melnyk
      Member

      if you still need it – try this script https://www.dropbox.com/s/lrank3o5r5lykph/Move%20TextFrame%20to%20Layer.jsx?dl=0 – it should work

      I probably need to start posting my scripts in github))

    • #71498
      Aaron Troia
      Participant

      Thank you so much Oleh, I’m not working on the projects that I was working on when I first posted, but I know this script will come in handy on future eBook conversions that come my way!

    • #103492
      Aaron Gray
      Participant

      Oleh, this script is a great idea and just what I was looking for. However, I’m having some trouble with it. It works great sometimes, but other times I get a JavaScript Error:

      Error Number: 30477
      Error String: Invalid value for parameter ‘to’ of method ‘move’. Expected Array of 2 Units, Spread, Page or Layer, but received nothing.
      Line: 37
      Source: app.activeDocument.textFrames[i].move(app.activeDocument.layers.item(layer));

      Do you have any idea what could be causing that? I’m using InDesign CC 2014.

      Thanks!
      Aaron Gray

    • #103494

      Aaron, Olehs script is working for me. I get only an error, if a object is locked. Can you identify the situation when his script fails? Provide an example?

      Kai

    • #103503
      Aaron Gray
      Participant

      Thanks, Kai. I have an 11-page document with four layers in it: “DataFrame,” “Index,” “Layer 1,” and “Feature Boxes.” I have 10 different object styles in this document including one called “BB18 Index Frame.” I would like to move all the text frames in the document that have the “BB18 Index Frame” onto the “Index” layer, but when I try to do that with the script I get the JavaScript error. I’m also noticing that if I try to move all those objects to other layers, I get inconsistent results—some move and some don’t. I check to make sure that they all really do have the object style and they do, and there are no style overrides. Also, the layers are all unlocked.

    • #103508
      Oleh Melnyk
      Member

      Aaron, do all objects are text frames?
      It would be easier if you could share your .indd file

    • #103515

      You get inconsistent results, because the index per spread is messed up when the first frame is moved. Looping backwards will help here:

      for(i = app.activeDocument.textFrames.length-1; i >= 0 ; i–)

    • #103516
      Aaron Gray
      Participant

      Thank, Kai. Where/how would I insert that into the code?

      Oleh, is there an email address or other location I could send you a file?

      Thanks!

    • #103517

      Aaron, if you open the script with a Texteditor or the ESTK you will find a line something similar to the new line. Simply change it. But this will maybe not solve your error. Because Oleh write here too, I’m pretty sure, he will correct his script and provide a new download link :)

    • #103518
      Aaron Gray
      Participant

      Hi Kai. I tried what you suggested and it did still return the same error, but I will wait and see if Oleh has any ideas. Thanks!

    • #103519
      Aaron Gray
      Participant

      Oleh, I see now that your email is in the script. I will send you a file. To answer your other question, they are all text frames that have the object style applied.

    • #103520
      Oleh Melnyk
      Member

      Ok, this is weird! Looks like the problem is not with the script but with InDesign file, or to be more precise – with the layer name. Looks like “Index” is some sort of reserved word in InDesign, so all you need is to rename a layer, or at least – double-click on the layer name, so “Layer options” dialog opens – once it open, you can close this dialog and script should work ok… – Just try and let me know if it works!

      Meanwhile, I made some changes to initial script, so the new version should now work with all objects, not just text frames + now you can undo what script made in just one step.
      https://www.dropbox.com/s/uc8vmq878xwh4gf/Move%20Object%20with%20Selected%20ObjectStyle%20to%20Layer.jsx?dl=0

    • #103522
      Aaron Gray
      Participant

      Thanks, Oleh! I renamed the “Index” layer (to just “Layer 2”), and I even renamed the object style for good measure to not have “index” in the name of it, and now I’m no longer getting the JavaScript error.

      However, I’m still getting inconsistent results. Whenever I run the script, some frames successfully move to new layers and some don’t, and it’s not always the same ones. I can’t figure out what the pattern is! They are all definitely text frames with the same object style applied to them. One thing I do notice is that it seems to work more consistently when I’m moving objects up in the layer stacking order as opposed to down. When I move them up, they all seem to go up, but when I move them down, some get left behind.

    • #103523
      Oleh Melnyk
      Member

      Well, I have no idea what might cause this problem. In file you sent me – everything works once I rename, or just open and close “Layer options” dialog (all text frames moves to ‘Index’ layer).
      Maybe your InDesign file is corrupted?
      Have you tried to save it as .idml (for CS4 and later versions), then open this .idml file – this might help to fix some errors.
      Have you tried a new script version?
      Otherwise, it’s hard to guess – need try script with real InDesign document

    • #103525
      Aaron Gray
      Participant

      Thanks, Oleh. I will keep testing it out—in other documents as I have the opportunity. I did try saving it as an IDML but that didn’t resolve it. Thanks again for the help!

      Aaron

    • #103534

      Oleh, why didn’t you loop backwards as suggested?

    • #103535
      Oleh Melnyk
      Member

      Kai, when I loop backward, I got the same problem Aaron has – some object moves to a new layer and some don’t so I have to run the script multiple times… ?
      I don’t think loop backward can solve this problem since script didn’t store order, page number, or folder then move the object to new page/folder/whatever might matters for loop order – script just iterate over every element in opened document, check if object has applied object style, and if that object is not in the desired layer – script just moves object to new layer…

      New version should work with locked layers/objects https://www.dropbox.com/s/knh502mk9uxjipg/Move%20Object%20with%20Selected%20ObjectStyle%20to%20Layer%20v2.jsx?dl=0

    • #103536

      Aaron sent his testfile only to you. So I built a simple sample: 4 pages as spreads. Page 1 and 4 alone, page 2+3 on one spread. If there are more than one objects with applied oStyle per spread, only one object will be moved to the new layer, if you move forwards. If you move backwards all objects are moved.

      But you are right: I had sometimes problems with forward and backward loops, if I use collections. So sometimes it is helpful to deal with arrays instead. Maybe Aaron should built a example, that is available for everyone here.

    • #103538
      Oleh Melnyk
      Member

      Weird! I got an opposite issue while iterating backward.

      I added “proceed until you bleed” while-loop that tries to move objects to the desired layer (1000 tries) – now it should work, lol
      https://www.dropbox.com/s/r4dj5qktplewm7p/Move%20Object%20with%20Selected%20ObjectStyle%20to%20Layer%20v3.jsx?dl=0

    • #103540

      Oleh, weird indeed!

      Assume you have one page with 4 items.

      pageItems[0] > wrong style
      pageItems[1] > right style to move
      pageItems[2] > right style to move
      pageItems[3] > wrong style

      If you move forwards within a collection you should get the following result:

      pageItems[1] is moved to the new layer, but if you check the index of this object, the index is now [3]. That means, the previous [2] becomes [1] and will be ignored (!), and the previous [3] is now [2] and fails, because of the wrong style. But the previous [1] whos index is now [3] will be examined again.

      This will not happen (normally), if you loop backwards or if you deal with pI = app.activeDocument.pageItems.everyItem().getElements() as static array forwards.

      However: I had some issues with forwards or backwards in the past too ;-)

    • #103545
      Aaron Gray
      Participant

      I tried the latest version and I’m still getting inconsistent results, but now in a different way. I’m finding that the objects with the object style I’ve selected are consistently moving to whichever layer I select. However, I also find that some other objects that don’t have that style are also moving to the selected layer.

      Additionally, there seem to be certain layers where if I try to move objects to the layer with the script, I get an error with each object that says “Error: Cannot move locked items to a new layer,” even though none of the objects I’m trying to move are locked. However, then it goes ahead and moves the object to the layer anyway. (Here it seems like writing a line into the script to suppress error messages could be useful.)

      I’m sorry I’m not able to post a sample file online, but the policies of the company I work for wouldn’t permit that. This seems like it will be a very useful script once it’s debugged. I wish I had the programming skills to help more!

    • #103546

      Aaron, you could sent me a file directly to forum@ruebiarts.de. Otherwise you should built an example with bla bla text ;-)

      > However, I also find that some other objects that don’t have that style are also moving to the selected layer.

      Hm, I cannot believe that, cause in Olehs script there is a line, that should prevent that!

    • #103550
      Aaron Gray
      Participant

      Thanks, Kai! I didn’t get a chance today but I will send something your way next week.

    • #103551
      Oleh Melnyk
      Member

      check this script version with my InDesign test files – https://www.dropbox.com/s/nrb6me33sbtn9wg/MoveObjects2Layer.zip?dl=0

    • #103554

      Oleh, did you package the right files? Locked objects are not moved, if you choose move red to red, yellow is moved as well, but not on page 1. Objects with basic frame applied are moved on page 1 too ;-)

    • #103555

      Haha, I had in the meantime 3 different samples and get different results, if I move forwards or backwards, but not any solution give the right results ;-)

      First, you should remove your while loop. Second use a static array instead of your dynamic collection. This simplified example works for me in any solution:

      var curDoc = app.activeDocument;
      var objectStyleName = "Red Style";
      var layerName = "Red Layer";    
      
      var pItems = curDoc.pageItems.everyItem().getElements();
      var nItems = pItems.length;
      
      for (var i = 0; i < nItems; i++) {
        var curItem = pItems[i];
        if (curItem.appliedObjectStyle.name == objectStyleName && curItem.itemLayer.name != layerName) {             
          // move objects to layer
          curItem.itemLayer = curDoc.layers.itemByName(layerName);                     
        }
      }
      
    • #103559
      Oleh Melnyk
      Member

      looks like this is InDesign bug – sometimes it works, sometimes it doesn’t, and then – random results all the time

    • #103575
      Aaron Gray
      Participant

      Kai: Do you still want a test file? It sounds like you’re getting exactly the same results I was. However, I downloaded your simplified script, changed the object style and layer names to match the stuff I wanted to move, and it seems like it’s working fine. That may be all I need to solve my problem. Thanks!

    • #103584

      Aaron, if this is working for you, I don’t need additional testfiles.
      Oleh, I’m not sure, if this is a bug or simply the behavior of collections.

      Kai

    • #14364882
      lynx
      Participant

      This has been such great code to dissect and learn from, Currently it wont pull objects styles in the dropdown that are in folders, how would I tweak the language to do that?

      Secondly, If i change the top half to pull paragraph styles in the first list and objects styles in the second, how could I adjust the bottom portion to assign object styles from text frames that have the paragraph style chosen from the dropdown.

      thank you so much for any help!!

Viewing 30 reply threads
  • You must be logged in to reply to this topic.
Forum Ads