i'm sorry, i've posted it in my previous post, but it seems that after publishing it something went wrong
here it is:
#target Illustrator
// script.name = relinkAllSelected.jsx;
// script.description = relinks all selected placed images at once;
// script.required = select at least one linked image before running;
// script.parent = CarlosCanto // 7/12/11;
// script.elegant = false;
// https://forums.adobe.com/thread/2381744
// ---------------------------------------------------------------------------------
// changed: relink all selected placed items with ONE new file
// ---------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
//changed: relink all placed items
//----------------------------------------------------------------------------------
if (app.documents.length > 0) {
for (i = 0; i < app.activeDocument.placedItems.length; i++) {
var placedArt = app.activeDocument.placedItems;
placedArt.selected = !(placedArt.selected);
}
}
var idoc = app.activeDocument;
var sel = idoc.selection;
if (sel.length>0)
{
var file = File.openDialog ("Choose file");
for (i=0 ; i<sel.length ; i++ )
{
if (sel.typename == "PlacedItem")
{
var iplaced = sel;
//var file = File.openDialog ("open file " + iplaced.file );
iplaced.file = file;
}
}
}
else
{
alert("Select at least one placed item before running.");
}
(unfortunately i can't paste it as a "raw html", this may be a problem why it wasn't showing in my last post)
Ok, in this block:
if (app.documents.length > 0) {
for (i = 0; i < app.activeDocument.placedItems.length; i++) {
var placedArt = app.activeDocument.placedItems;
placedArt.selected = !(placedArt.selected);
}
}
you are selecting all placedItems in the document. Then later on you are looping each item in your selection and re-linking them.
There is no logic in this code that determines what layer the placed item is contained within.
Give this one a shot:
function relinkImagesOnActiveLayer()
{
if(!app.documents.length)
{
alert("Please open a document.");
return;
}
var newSourceFile = File.openDialog("Choose File");
if(!newSourceFile)
{
return;
}
var docRef = app.activeDocument;
var layers = docRef.layers;
var activeLayer = docRef.activeLayer;
var items = [];
//push all placed items in the document to an array
for(var x=0,len=docRef.placedItems.length;x<len;x++)
{
items.push(docRef.placedItems);
}
//no placed items. exit script.
if(!items.length)
{
alert("No placed items in the document.");
return;
}
var curItem;
for(var x=0,len=items.length;x<len;x++)
{
curItem = items;
if(curItem.layer == activeLayer)
{
curItem.file = newSourceFile;
}
}
}
relinkImagesOnActiveLayer();