Skip to main content
Participating Frequently
March 4, 2023
Answered

Help combining scripts to relink to a new image

  • March 4, 2023
  • 1 reply
  • 1062 views

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;
 }
    

 }

 

This topic has been closed for replies.
Correct answer femkeblanco

Sorry, I don't follow you.  But, if you're trying to test the image file name against the layer name, it should be

if (imgNameNoExt == targetLayer.name) 

or

if (imgNameNoExt == newName)

 

1 reply

femkeblanco
Legend
March 5, 2023

At a glance, there are three problems:


(1) You have two nested loops with the same index. Change the inner loop

for (var i = 0; i < imagesList.length; i++) {

to

for (var j = 0; j < imagesList.length; j++) {

Also. change the two instances of imagesList[i] in this loop to imagesList[j].

 

(2) This statement is incorrect

var itemToPlace = doc.placedItems();

Change it to

var itemToPlace = _placedItems[i];

 

(3) The expression in the last line returns an item, not a file, hence the error.  If you want that line, it should be

iplaced.file = file.file;

However, if I understand what you're trying to do, this line would be superfluous and should be deleted anyway. 

DB5708Author
Participating Frequently
March 5, 2023

femkeblano, thank you so much. That got it working. I just need a couple more tweaks. The script should ask the operator to input the name of the new link that changes the layer name of the selected link. Then the script looks in the folder for a file whose name matches the new layer name. I managed to insert the code to ask and change the layer name. I then found a post from you on how to retrieve the layer name, but no matter where I insert it, it breaks the script. I'm trying to retrieve the new layer name and have it compare that to var imgName and if it doesn't match create an alert but my else statement on line 75 gets caught in an infinite loop. If there is a better way to do all this, I'm all ears. Thanks again for your help.

 

var doc = app.activeDocument;
var targetLayer = app.activeDocument.activeLayer;
targetLayer.visible = true;
targetLayer.locked = false;

// var aDoc = activeDocument;

//var actLay = aDoc.activeLayer;

if (targetLayer.typename == "Layer") {

    var newName = prompt ("Would you like to rename this layer group?", targetLayer.name, "Rename");

    try {targetLayer.name = newName;} catch (e) {};

    } else {

        alert("active Layer is not a LayerSet");

        }
        
        
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/caldera/Desktop/Manual Job Flow/PPME");
var imagesList = dirImages.getFiles();



var itemToPlace = {};

  
      for (var j = 0; j < imagesList.length; j++) {
      
      


          var imgName = imagesList[j].name;
     //    var documentName = doc.name;
            var layerName = list

        //compare image filename to current selected layer name (both without extensions)

    var imgNameNoExt = imgName.slice(0, imgName.indexOf("."));
    var docuNameNoExt = documentName.slice(0, documentName.indexOf("."));

        // check identical names
        if( imgNameNoExt == docuNameNoExt ) {
   //     if( imgNameNoExt == layerName ) {


            var itemToPlace = _placedItems[i];

     		 itemToPlace.file = imagesList[j]; 
     		 
     		 } else
     		 alert("Matching link not found")
     		
        }
        
       var file = itemToPlace
	

 }
    
    
    
    
    
//----------------------------------    
       
 /*  Get name of layer to compare against imgName  
    var list = "";
for (var i = 0; i < app.selection.length; i++) {
    list = list + app.selection[i].name + " - " + app.selection[i].layer.name + "\r";
}
alert( list );  */

 



femkeblanco
femkeblancoCorrect answer
Legend
March 6, 2023

Sorry, I don't follow you.  But, if you're trying to test the image file name against the layer name, it should be

if (imgNameNoExt == targetLayer.name) 

or

if (imgNameNoExt == newName)