Skip to main content
Inspiring
June 26, 2019
Question

• Javascript to embed ALL linked images into the AI file via script (bug?)

  • June 26, 2019
  • 4 replies
  • 3167 views

Hello -

I "copy-paste" a couple of different snippet of code to embed all linked images into the AI file.

Apparently it works for a couple of them but for some other, they jumps/moves of position before they are embed…

What happen?

Is that a know "bug"?

I found this on the net ->  https://graphicdesign.stackexchange.com/questions/119214/illustrator-javascript-to-embed-linked-images

Hereunder some snippet of code I have tried:

if ( app.documents.length > 0 ) {
  
while ( app.activeDocument.placedItems.length > 0 ) {
  placedArt
= app.activeDocument.placedItems[0];
  placedArt
.embed();
  
}

}

Another

  1. #target illustrator 
  2.  
  3. embedAllImages(); 
  4.  
  5. function embedAllImages(){ 
  6.     var placedArt = app.activeDocument.placedItems; 
  7.     if ( placedArt.length == 0 ) { return; } 
  8.     for ( var i = placedArt.length-1; i >= 0; i-- ) { 
  9.         placedArt.embed(); 
  10.     }; 
  11. }; 

Thank you

- Dimitri

This topic has been closed for replies.

4 replies

RobOctopus
Inspiring
January 22, 2024

Old topic, but maybe this will help someone else

 

I found that putting an app.redraw() into my embed loop solved my problem. I was randomly having images change size/positions when embeding via scripting. Doing the same thing manually would embed as expected, but something about the script was messing with some of them. The redraw seemed to fix the few that I tested this with.

for (var e = app.activeDocument.placedItems.length - 1; e >= 0; e--) {
  var PlacedLink = app.activeDocument.placedItems[e]
  if (!PlacedLink.hidden) {
    try {
      PlacedLink.embed();
    } catch (e) {
      /*nothing*/
    }
    app.redraw()
  }
}

 

Inspiring
June 27, 2019

Hello -

Could a "Timeout" function help me in this particular case?

Something like:

setTimeout(myFunction, 3000);

Launch the first loop, and after a small timeout,

launch the second loop.

Thank you...

Inspiring
June 26, 2019

What's really strange to me…

Is that when I run the follow piece of code at once some of "links" are moved from their orignal place  (two appear in the middle of the active artboard. Some of them are even resized randomly.

But when I run these two "loops" separately it works!

try {

if ( app.documents.length > 0 ) {

// while ( app.activeDocument.placedItems.length > 0 ) {

for (var  p = 0; p <= app.activeDocument.placedItems.length; p++) {

placedArt = app.activeDocument.placedItems

;

placedArt.embed();

}

}

} catch(err) {

while ( app.activeDocument.placedItems.length > 0 ) {

placedArt = app.activeDocument.placedItems[0];

placedArt.embed();

}

}

Hereunder is a screenshot of the "LINKS" palette:

- Dimitri

Inspiring
June 26, 2019