Help combining scripts to relink to a new image
I’ll trying to combine two working scripts I found on here. One relinks a new file after asking you to find that file. The other compares the document name to the image name in a folder and if there is a match places that document in Illustrator.
I’d like the script to select the hightlighted <linked file> layer and relink to the new matching file automatically. But instead of matching the new file to the document name, it should match the name of the highlighted or selected layer that host the linked file.
I’m currently stuck on an error on line58, 'var iplaced = placedArt iplaced is undefined'. I think there is something wrong with the way the matching file is defined. Thank you.
//Finds the replacement links for each selected <linked file> layer on the active layer.
var doc = app.activeDocument;
var targetLayer = app.activeDocument.activeLayer;
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--) {
var placedArt = _placedItems[i]
placedArt.selected = !(placedArt.selected);
var dirImages = new Folder("/Users/areldar/Desktop/Auto Flow/PPME");
var imagesList = dirImages.getFiles();
var itemToPlace = {};
for (var i = 0; i < imagesList.length; i++) {
var imgName = imagesList[i].name;
var documentName = doc.name;
//compare image filename to current document name (both without extensions)
var imgNameNoExt = imgName.slice(0, imgName.indexOf("."));
var docuNameNoExt = documentName.slice(0, documentName.indexOf("."));
// check identical names
if( imgNameNoExt == docuNameNoExt ) {
var itemToPlace = doc.placedItems();
itemToPlace.file = imagesList[i];
}
var file = itemToPlace
alert("file "+file)
//var file = File.openDialog ("open file");
// file = new File(file.fsName.replace("file://",""));
var iplaced = placedArt;
iplaced.file = file;
}
}
