• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Illustrator script embed selected art in selected layer

Community Beginner ,
Oct 18, 2021 Oct 18, 2021

Copy link to clipboard

Copied

Hi there,

 

I have an Illustrator document with multiple layers and multiple layers with linked images/art in them. I would like to with a script select a specific layer and embed all images in the selecte layer only.

 

This is the script I have but as soon as I have multiple images in the layer it will only embed one of the images.

 

var i = 1
var doc = app.activeDocument;
var myLayers = doc.layers;

EmbedTestLayer();

function EmbedTestLayer()
{
//Layers visible and unlocked
for (a=0; a<myLayers.length; a++){
myLayer = myLayers[a];
myLayer.visible = true;
myLayer.locked = false;
}
}
try{
//Deselect all objects
app.activeDocument.selection = null;

//Embed objects in layer
myLayers.getByName ("Test Layer").hasSelectedArtwork = true;
placedArt = app.activeDocument.placedItems[i];
placedArt.embed("Test Layer");
}
catch (e) {}
 
Test Layer is the layer that I want to select as well as all elements/sublayers and then embed the Art.
 
Help will be greately appreciated.
TOPICS
Scripting

Views

1.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 3 Correct answers

Guide , Oct 18, 2021 Oct 18, 2021
var doc = app.activeDocument;
var myLayers = doc.layers;
EmbedTestLayer();
function EmbedTestLayer() {
    for (var a = 0; a < myLayers.length; a++){
        myLayer = myLayers[a];
        myLayer.visible = true;
        myLayer.locked = false;
    }
}
try {
    var targetLayer = myLayers["Test Layer"];
    var items = targetLayer.placedItems;
    for (var i = items.length - 1; i > -1; i--) {
        items[i].embed();
    }
}
catch (e) {}

Votes

Translate

Translate
Guide , Feb 26, 2022 Feb 26, 2022

That's because the above snippet was intended to traverse nested layers, as per your request.  But <Group> is a groupItem and not a layer.  With scripting one has to be specific.  (It's also my fault for not looking at the screenshot).  This will traverse nested groups, embedding placedItems:

var doc = app.activeDocument;
var targetLayer = doc.layers["Test Layer"];
targetLayer.visible = true;
targetLayer.locked = false;
var _placedItems = [];
function recurse(items) {
    for (var i = 0; i < item
...

Votes

Translate

Translate
Guide , Mar 01, 2022 Mar 01, 2022

Yes, that is fine. 

 

Edit:  Or you could use this:

var doc = app.activeDocument;
try {
    var targetLayer = doc.layers["Test Layer"];
    targetLayer.visible = true;
    targetLayer.locked = false;
    var _placedItems = [];
    function recurse(items) {
        for (var i = 0; i < items.length; i++) {
            if (items[i].typename == "PlacedItem") {
                _placedItems.push(items[i]);
            } else if (items[i].typename == "GroupItem") {
                recurse(items[i].pageIte
...

Votes

Translate

Translate
Adobe
Community Expert ,
Oct 18, 2021 Oct 18, 2021

Copy link to clipboard

Copied

The variable "i" has not changed its value to 1. So only one image is processed.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 18, 2021 Oct 18, 2021

Copy link to clipboard

Copied

var doc = app.activeDocument;
var myLayers = doc.layers;
EmbedTestLayer();
function EmbedTestLayer() {
    for (var a = 0; a < myLayers.length; a++){
        myLayer = myLayers[a];
        myLayer.visible = true;
        myLayer.locked = false;
    }
}
try {
    var targetLayer = myLayers["Test Layer"];
    var items = targetLayer.placedItems;
    for (var i = items.length - 1; i > -1; i--) {
        items[i].embed();
    }
}
catch (e) {}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 19, 2021 Oct 19, 2021

Copy link to clipboard

Copied

Femke, 

 

You are the best! Thank you very much!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 25, 2022 Feb 25, 2022

Copy link to clipboard

Copied

Hi there,

 

Sorry for asking again but I am running into another issue. 

Same script but then I need to not only in the picture embed all images in the "Test Layer" but also in all sub layers. So need to embed Images 1 through 4. 

 

This is the script that Femke helped with and it actually does embed Image 1 but nothing else.

 

Any help will be immensily appreciated.

 

Script:

 

var doc = app.activeDocument;
var layers = doc.layers;
var myLayers = doc.layers;
var placedImages = layers["Test Layer"];
var mayLayers = layers["Test Layer"];
myLayers.layers = true;

var myLayers = doc.layers;
for (a=0; a<myLayers.length; a++){
myLayer = myLayers[a];
myLayer.visible = true;
myLayer.locked = false;
}
//Embed Cloud Marks
try {
var targetLayer = myLayers.getByName("Test Layer");
var items = targetLayer.placedItems;
for (var i = 0, len = items.length; i < len; i--) {
items[i].embed();
}
}
catch (e) {}
 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Feb 25, 2022 Feb 25, 2022

Copy link to clipboard

Copied

var doc = app.activeDocument;
var targetLayer = doc.layers["Test Layer"];
targetLayer.visible = true;
targetLayer.locked = false;
targetLayer.prop1 = true;
function recurse(_layers) {
    for (var i = 0; i < _layers.length; i++) {
        _layers[i].prop1 = true;
        recurse(_layers[i].layers);
    }
}
recurse(targetLayer.layers);
var items = doc.placedItems;
for (var i = items.length - 1; i > -1; i--) {
    if (items[i].parent.prop1 == true) {
        items[i].embed();
    }
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 25, 2022 Feb 25, 2022

Copy link to clipboard

Copied

Hi Femke,

 

Thanks for the fast reply. Sorry but this also does not work. Anything outside the <Group> layer is embedded but nothing inside this layer. Not even if I change the name of the sublayer to Layer 1 or something else.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Feb 26, 2022 Feb 26, 2022

Copy link to clipboard

Copied

That's because the above snippet was intended to traverse nested layers, as per your request.  But <Group> is a groupItem and not a layer.  With scripting one has to be specific.  (It's also my fault for not looking at the screenshot).  This will traverse nested groups, embedding placedItems:

var doc = app.activeDocument;
var targetLayer = doc.layers["Test Layer"];
targetLayer.visible = true;
targetLayer.locked = false;
var _placedItems = [];
function recurse(items) {
    for (var i = 0; i < items.length; i++) {
        if (items[i].typename == "PlacedItem") {
            _placedItems.push(items[i]);
        } else if (items[i].typename == "GroupItem") {
            recurse(items[i].pageItems);
        }
    }
}
recurse(targetLayer.pageItems);
for (var i = _placedItems.length - 1; i > -1; i--) {
    _placedItems[i].embed();
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 26, 2022 Feb 26, 2022

Copy link to clipboard

Copied

Femke,

 

Seriously you are the best! Thank you soooooo much this works perfeclty!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 01, 2022 Mar 01, 2022

Copy link to clipboard

Copied

Hi Femke,

 

Sorry to be such a hassle but when I run this script and the Test Layer is not present it gives me an error. How can I get around that?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Mar 01, 2022 Mar 01, 2022

Copy link to clipboard

Copied

The following prompts you to enter the name of a specific layer to target.  (Is this what you want?)  Alternatively, you can leave the text area empty to target all layers. 

var doc = app.activeDocument;
var _placedItems;
var _name = prompt("Enter target layer name or leave empty to target all layers", "Test Layer");
if (_name == "") {
    _placedItems = doc.placedItems;
    _embed()
} else {
    try {
        var targetLayer = doc.layers[_name];
        targetLayer.visible = true;
        targetLayer.locked = false;
        _placedItems = [];
        function recurse(items) {
            for (var i = 0; i < items.length; i++) {
                if (items[i].typename == "PlacedItem") {
                    _placedItems.push(items[i]);
                } else if (items[i].typename == "GroupItem") {
                    recurse(items[i].pageItems);
                }
            }
        }
        recurse(targetLayer.pageItems);
        _embed()
    } catch(e) {
        alert(e.message);
    }
}
function _embed() {
    for (var i = _placedItems.length - 1; i > -1; i--) {
        _placedItems[i].embed();
    }
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 01, 2022 Mar 01, 2022

Copy link to clipboard

Copied

Hi Femke,

 

This is actually used in an automated script. What needs to happen is the script needs to check if the "Test Layer" is available and if yes embed all images in the "Test Layer" and if the "Test Layer" not continue without doing anything.

 

I hope it is clear.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 01, 2022 Mar 01, 2022

Copy link to clipboard

Copied

Hi Femke,

 

I think I fixed it myself this way:

 

var doc = app.activeDocument;
var _placedItems;
var _name = doc.layers[""];
if (_name == "") {
_placedItems = doc.placedItems;
_embed()
} else {
try {
var targetLayer = doc.layers["Test Layer"];
targetLayer.visible = true;
targetLayer.locked = false;
_placedItems = [];
function recurse(items) {
for (var i = 0; i < items.length; i++) {
if (items[i].typename == "PlacedItem") {
_placedItems.push(items[i]);
} else if (items[i].typename == "GroupItem") {
recurse(items[i].pageItems);
}
}
}
recurse(targetLayer.pageItems);
_embed()
} catch(e) {}
}
function _embed() {
for (var i = _placedItems.length - 1; i > -1; i--) {
_placedItems[i].embed();
}
}
If you dont mind having a quick look, I will be forever greatfull!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Mar 01, 2022 Mar 01, 2022

Copy link to clipboard

Copied

LATEST

Yes, that is fine. 

 

Edit:  Or you could use this:

var doc = app.activeDocument;
try {
    var targetLayer = doc.layers["Test Layer"];
    targetLayer.visible = true;
    targetLayer.locked = false;
    var _placedItems = [];
    function recurse(items) {
        for (var i = 0; i < items.length; i++) {
            if (items[i].typename == "PlacedItem") {
                _placedItems.push(items[i]);
            } else if (items[i].typename == "GroupItem") {
                recurse(items[i].pageItems);
            }
        }
    }
    recurse(targetLayer.pageItems);
    for (var i = _placedItems.length - 1; i > -1; i--) {
        _placedItems[i].embed();
    }
} catch(e) {}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines