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 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) {}
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
...
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
...
Copy link to clipboard
Copied
The variable "i" has not changed its value to 1. So only one image is processed.
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) {}
Copy link to clipboard
Copied
Femke,
You are the best! Thank you very much!
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:
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();
}
}
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.
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();
}
Copy link to clipboard
Copied
Femke,
Seriously you are the best! Thank you soooooo much this works perfeclty!
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?
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();
}
}
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.
Copy link to clipboard
Copied
Hi Femke,
I think I fixed it myself this way:
Copy link to clipboard
Copied
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) {}