Skip to main content
Known Participant
February 23, 2024
Answered

Create a script to insert . AI within a document.AI.

  • February 23, 2024
  • 1 reply
  • 495 views

Hello, I already work creating some scripts, but very simple things. I have several .AI files in a folder. Within these files there is only one Group. I need a way to insert these files into a document open in Illustrator with its editing capabilities intact. It would be like opening a file, copying and pasting it into my document. But it is not feasible to do this with multiple files. I need ideas.

This topic has been closed for replies.
Correct answer m1b

Hi @MarcosAdriano1994, you can try this script I just wrote. When you run it, it will duplicate all the page items into the active document. Let me know if it helps.

- Mark

 

/**
 * Gather page items from all documents into active document.
 * @author m1b
 * @discussion https://community.adobe.com/t5/illustrator-discussions/create-a-script-to-insert-ai-within-a-document-ai/m-p/14442488
 */
(function () {

    var docs = app.documents;

    if (docs.length < 2)
        return alert('Please open two or more documents and try again.');

    var sourceDocNames = [],
        targetDoc = docs[0];

    for (var i = 1; i < docs.length; i++)
        sourceDocNames.push(docs[i].name);

    for (var i = 0, sourceDoc, items, layer; i < sourceDocNames.length; i++) {

        sourceDoc = app.documents.getByName(sourceDocNames[i]);

        for (var j = sourceDoc.layers.length -1; j >= 0 ; j--) {

            items = sourceDoc.layers[j].pageItems;
            layer = makeLayer(targetDoc, sourceDoc.layers[j]);

            for (var k = 0; k < items.length; k++)
                items[k].duplicate(layer, ElementPlacement.PLACEATEND);

        }

    }


})();




/**
 * Makes, or gets, a layer.
 * @author m1b
 * @version 2022-07-24
 * @param {Document} [doc] - an Illustrator Document.
 * @param {String|Layer} [nameOrLayer] - the layer name, or a Layer.
 * @returns {Layer}
 */
function makeLayer(doc, nameOrLayer) {

    doc = doc || app.activeDocument;
    var newLayer,
        name = nameOrLayer.hasOwnProperty('name') ? nameOrLayer.name : nameOrLayer;

    try {
        newLayer = doc.layers.getByName(name);
    } catch (e) {
        newLayer = doc.layers.add();
        if (undefined != nameOrLayer) {
            newLayer.name = name;
            if (nameOrLayer.hasOwnProperty('color'))
                newLayer.color = nameOrLayer.color;
        }
    }

    return newLayer;

};

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
February 23, 2024

Hi @MarcosAdriano1994, you can try this script I just wrote. When you run it, it will duplicate all the page items into the active document. Let me know if it helps.

- Mark

 

/**
 * Gather page items from all documents into active document.
 * @author m1b
 * @discussion https://community.adobe.com/t5/illustrator-discussions/create-a-script-to-insert-ai-within-a-document-ai/m-p/14442488
 */
(function () {

    var docs = app.documents;

    if (docs.length < 2)
        return alert('Please open two or more documents and try again.');

    var sourceDocNames = [],
        targetDoc = docs[0];

    for (var i = 1; i < docs.length; i++)
        sourceDocNames.push(docs[i].name);

    for (var i = 0, sourceDoc, items, layer; i < sourceDocNames.length; i++) {

        sourceDoc = app.documents.getByName(sourceDocNames[i]);

        for (var j = sourceDoc.layers.length -1; j >= 0 ; j--) {

            items = sourceDoc.layers[j].pageItems;
            layer = makeLayer(targetDoc, sourceDoc.layers[j]);

            for (var k = 0; k < items.length; k++)
                items[k].duplicate(layer, ElementPlacement.PLACEATEND);

        }

    }


})();




/**
 * Makes, or gets, a layer.
 * @author m1b
 * @version 2022-07-24
 * @param {Document} [doc] - an Illustrator Document.
 * @param {String|Layer} [nameOrLayer] - the layer name, or a Layer.
 * @returns {Layer}
 */
function makeLayer(doc, nameOrLayer) {

    doc = doc || app.activeDocument;
    var newLayer,
        name = nameOrLayer.hasOwnProperty('name') ? nameOrLayer.name : nameOrLayer;

    try {
        newLayer = doc.layers.getByName(name);
    } catch (e) {
        newLayer = doc.layers.add();
        if (undefined != nameOrLayer) {
            newLayer.name = name;
            if (nameOrLayer.hasOwnProperty('color'))
                newLayer.color = nameOrLayer.color;
        }
    }

    return newLayer;

};
Known Participant
March 8, 2024

Apologies for the delay. I adapted your code and it worked perfectly. Thank you very much.

m1b
Community Expert
Community Expert
March 8, 2024

Excellent! Well done! 😊