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
- This topic has 6 replies, 2 voices, and was last updated 6 months, 2 weeks ago by
Andy Brown.
-
AuthorPosts
-
-
August 29, 2019 at 3:14 pm #1182763
Jort Haan
MemberHello 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!
-
August 29, 2019 at 3:52 pm #14324216
Theunis De Jong
MemberDidn’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.)
-
August 30, 2019 at 3:19 am #14324212
Jort Haan
MemberHello 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.
-
August 30, 2019 at 10:00 am #14324211
Jeremy Howard
ParticipantGive 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();
}-
April 2, 2024 at 6:33 am #14402200
Ruben Solér
ParticipantI’d like to give my thanks as well, the script worked beautifully!
-
-
September 23, 2019 at 4:45 am #14324084
Jort Haan
MemberDear 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!
-
July 31, 2024 at 7:26 am #14405417
Andy Brown
ParticipantI used this a few months ago and it worked great. Now I’m getting an error message. Not a coder here just copied and saved as jsx file.
Line:1
Source: {\rtf1\ansi\ansicpg1252\cocoartf2761
Offending text: \any advice?
-
-
AuthorPosts
- You must be logged in to reply to this topic.