This script will replace all links with the same name with your chosen file.
/**
* This script replaces all linked files with the same name at one time in Adobe Illustrator.
* It allows the user to select the link to replace from a list of current links using a drop-down menu
* and then choose the new file from a dialog.
*/
function replaceAllLinksWithSameName() {
var doc = app.activeDocument;
var placedItems = doc.placedItems;
var linkNames = [];
// Collect all unique link names
for (var i = 0; i < placedItems.length; i++) {
var placedItem = placedItems[i];
if (placedItem.file) {
var fileName = placedItem.file.name;
// Check if the fileName is already in linkNames
var isUnique = true;
for (var j = 0; j < linkNames.length; j++) {
if (linkNames[j] === fileName) {
isUnique = false;
break;
}
}
if (isUnique) {
linkNames.push(fileName);
}
}
}
if (linkNames.length === 0) {
alert("No linked files found in the document.");
return;
}
// Create a dialog for link selection
var dialog = new Window("dialog", "Select Link to Replace");
dialog.orientation = "column";
var dropdown = dialog.add("dropdownlist", undefined, linkNames);
dropdown.selection = 0;
var okButton = dialog.add("button", undefined, "OK");
okButton.onClick = function() {
dialog.close(1);
};
if (dialog.show() != 1) {
return;
}
var oldFileName = dropdown.selection.text;
var newFile = File.openDialog("Select the new file to replace " + oldFileName);
if (newFile) {
var count = 0;
for (var k = 0; k < placedItems.length; k++) {
var placedItem = placedItems[k];
if (placedItem.file && placedItem.file.name === oldFileName) {
placedItem.file = newFile;
count++;
}
}
alert(count + " links have been replaced.");
} else {
alert("Operation cancelled or no file selected.");
}
}
replaceAllLinksWithSameName();