• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Help combining scripts to relink to a new image

New Here ,
Mar 04, 2023 Mar 04, 2023

Copy link to clipboard

Copied

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

 }

 

TOPICS
Scripting

Views

684

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Mar 06, 2023 Mar 06, 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)

 

Votes

Translate

Translate
Adobe
Guide ,
Mar 04, 2023 Mar 04, 2023

Copy link to clipboard

Copied

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 05, 2023 Mar 05, 2023

Copy link to clipboard

Copied

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 );  */

 



Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Mar 06, 2023 Mar 06, 2023

Copy link to clipboard

Copied

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)

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 06, 2023 Mar 06, 2023

Copy link to clipboard

Copied

We keep getting closer. When I run the script it brings up the alert "Matching link not found" When I click Ok, it relinks to the new file whose name matches the layer name. If there is no matching file, I get the alert "Matching link not found" twice.
Where I seem to be lost is the ' if (imgNameNoExt == targetLayer.name)' part in the last section. I don't understand why the 'else' statement is activated if there is a match and then activated twice if there is no match. Has to due with the nested loops I suspect. I tried to clean up the script to make it a bit more legable.

// Define target layer

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

// Ask for name of file to relink and change selected layer to that name

    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");

        }
        
        
//Define where to look for matching files to relink
        var dirImages = new Folder("/Users/caldera/Desktop/Manual Job Flow/PPME");
	var imagesList = dirImages.getFiles();

        
//Run function to select all the <linked file> layers within the selected layer group
        
    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);

// Determine which file to relink

	var itemToPlace = {};
        for (var j = 0; j < imagesList.length; j++) {
        var imgName = imagesList[j].name;

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

      var imgNameNoExt = imgName.slice(0, imgName.indexOf("."));
      if (imgNameNoExt == targetLayer.name)  {

            var itemToPlace = _placedItems[i];

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

 		}
    }
    

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 13, 2023 Mar 13, 2023

Copy link to clipboard

Copied

LATEST
I'm down to just 1 error on this script. When it finds a match between the
file to be relinked and the renamed layer name it throws an error 1230
File/Folder Expected Line 70 itemToPlace.file=imagesList[j]. Why is this
not seeing it as a file?

//Automatically replaces links for each selected layer on
the active layer group //whenever the new link's file name matches the
renamed layer.

// Define target layer

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

// Ask for name of file to relink and change selected layer to that name

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");

}


// Define where to look for matching files to relink
var dirImages = new Folder("/Users/caldera/Desktop/Manual Job
Flow/PPME");
var imagesList = dirImages.getFiles();


//Run function to select all the layers within the selected
layer

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

// Define the folder to look in for the new file to relink



// Determine which file to relink

var itemToPlace = {};

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

var imgName = imagesList[j].name;
}

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

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


if (imgNameNoExt == targetLayer.name) {

var itemToPlace = _placedItems[i];
itemToPlace.file = imagesList[j];

var file = itemToPlace
}

else {
alert("Matching link not found")
}

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines