Copy Images instead of Moving them | InDesign Javascript



I have written an InDesign javascript script that moves me all images (respectively the frames of the images) into a new layer “Webbilder” created by the script (if not existing).

How can I customize it so that the images are not moved but copied?

This is my current script:

if (app.documents.length == 0) {
    alert("Es ist kein Dokument geöffnet, das Skript kann nicht ausgeführt werden.");
    console.log("Es ist kein Dokument geöffnet, das Skript kann nicht ausgeführt werden.")
    exit;
}
copyImages();

function copyImages() {
    var myDoc = app.activeDocument;
    var myImageFrameArray = new Array();
    var myPageItems = myDoc.allPageItems;
    var myNewLayer;
    try {
        myNewLayer = myDoc.layers.add({name:"Webbilder"})
    } catch (e) {
        myNewLayer = myDoc.layers.item("Webbilder");
    }

    for (i = 0; i < myPageItems.length; i++) {
        var myFrames = myPageItems[i];
        if (myFrames == "[object Group]") {
            var myGroupFrames = myFrames.allPageItems;
            for (j = 0; j < myGroupFrames.length; j++) {
                var myArrayGroupFrames = myGroupFrames[j];
                if (myArrayGroupFrames instanceof Rectangle || myArrayGroupFrames instanceof Polygon || myArrayGroupFrames instanceof Oval) {
                    myImageFrameArray.push(myArrayGroupFrames);
                } 
            }
        }
        else if (myFrames instanceof Rectangle || myFrames instanceof Polygon || myFrames instanceof Oval) {
            myImageFrameArray.push(myFrames);
        }
    }

    for (g = 0; g < myImageFrameArray.length; g++) {
        var myArrayImageFrameArray = myImageFrameArray[g];
        try {
            var myLinkedItem = myArrayImageFrameArray.pageItems[0];
            var myLinkName = myLinkedItem.itemLink.name;
            myArrayImageFrameArray.itemLayer = "Webbilder";
        } catch (e) {}
    }
    alert("Skript erfolgreich ausgefuehrt.")
}

Does anyone have any ideas, or do I need to change my approach completely?

Thanks already in advance for the feedback.

Noel

Answer

Try to change the line:

myArrayImageFrameArray.itemLayer = "Webbilder";

with this:

myArrayImageFrameArray.duplicate().itemLayer = "Webbilder";


Source: stackoverflow