Script to move all images(graphics) to a new layer

Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Script to move all images(graphics) to a new layer

Viewing 4 reply threads
  • Author
    Posts
    • #1182763
      Jort Haan
      Member

      Hello All,

      I’m trying to build a script to move all graphics to a new separate layer. I got this working but the images on the new layer don’t have their original frame size. It’s like only the image in the graphic frame is moved to the new layer. I can’t figure out how to fix this. So a little help would be appreciated.

      Script code:
      var scriptName = “Images to layers”;
      if (app.documents.length == 0) ErrorExit(“Please open a document and try again.”, true);

      Main();

      function Main() {
      var myDoc = app.activeDocument;
      var imgs = app.activeDocument.allGraphics;
      var foundObjects = Array();

      for(var i = 0; i < imgs.length; i++){
      if(imgs[i].parent.move){foundObjects.push(imgs[i]);}

      doc = app.activeDocument;

      if(foundObjects.length > 0){
      try{var myDestLayer = myDoc.layers.add({name:”Images”});}
      catch(_){myDestLayer = myDoc.layers.item(“Images”);}
      currentItem = foundObjects[i].duplicate(myDestLayer);
      for (i = 0; i < doc.pageItems.length; i++) {
      currentItem.move(myDoc.layers.item(“Images”));
      try{foundObjects[i].parent.remove(); newDuplicate.graphics[0].geometricBounds = foundObjects[i].parent.graphics[0].geometricBounds;}catch(_){}

      }
      }

      alert(“Finished.”, scriptName);
      }

      function ErrorExit(error, icon) {
      alert(error, scriptName, icon);
      exit();
      }
      }

      Thanks!

    • #14324216

      Didn’t try, but I think I see what’s happening. You indeed are moving the image – to be exact, the contents of each graphic frame. It’s as if you have selected the image with the open arrow, not its containing frame with the black arrow.

      Try “if(imgs[i].parent.move){foundObjects.push(imgs[i].parent);}” instead.

      (Whence the “imgs[i].parent.move”? This seems to checks if its parent has a ‘move’ method, and all of your images [parents] should. I believe you don’t need the “if” at all, unless you found some images that cannot be moved. Then just move each but wrap a try .. catch around this.)

    • #14324212
      Jort Haan
      Member

      Hello Theunis, thank you for your reply.

      I tried this but somehow the page is deleted. I can seem to figure out how. Any ideas?

      The ‘if’ is inherited of another script.

      Thanks in advance.

    • #14324211

      Give this a go and let us know how it works!

      var scriptName = “Images to layers”;
      if (app.documents.length == 0) ErrorExit(“Please open a document and try again.”, true);

      Main();

      function Main() {
      var myDoc = app.activeDocument;
      var myPageItems = app.activeDocument.allPageItems;
      var imageFrameArray = new Array();

      try{
      var myDestLayer = myDoc.layers.add({name:”Images”});
      }catch(foo){
      myDestLayer = myDoc.layers.item(“Images”);
      }

      for(var i = 0; i < myPageItems.length; i++){
      var aFrame = myPageItems[i];

      if(aFrame == “[object Group]”){
      var myGroupFrames = aFrame.allPageItems;
      for(g = 0; g < myGroupFrames.length; g++){
      var gFrame = myGroupFrames[g];
      //if the current frame is an image frame
      if(gFrame instanceof Rectangle || gFrame instanceof Polygon || gFrame instanceof Oval){
      imageFrameArray.push(gFrame);
      }
      }//end for g < myGroupFrames.length
      }else if(aFrame instanceof Rectangle || aFrame instanceof Polygon || aFrame instanceof Oval){
      imageFrameArray.push(aFrame);
      }
      }

      for(x=0; x<imageFrameArray.length;x++){
      var thisFrame = imageFrameArray[x];

      try{
      var myLinkedItem = thisFrame.pageItems[0];
      var myLinkName = myLinkedItem.itemLink.name;
      thisFrame.itemLayer = “Images”;
      }catch(foo){}
      }

      alert(“Finished.”, scriptName);
      }

      function ErrorExit(error, icon) {
      alert(error, scriptName, icon);
      exit();
      }

    • #14324084
      Jort Haan
      Member

      Dear Jeremy,

      Thank you for your reply and my excuse for my late reply. Sometimes real life and major events get in the way of our working life.

      The script you posted works like a charm. Thank you very much. I learned a lot from it.

      Cheers!

Viewing 4 reply threads
  • You must be logged in to reply to this topic.
>