Skip to main content
Inspiring
July 27, 2018
Answered

Relink all files on active layer

  • July 27, 2018
  • 1 reply
  • 1116 views

Hey everyone,

i've found this awesome script by CarlosCanto​ and it works perfectly if files are just placed.

BUT in my case it's not working because all my files are cropped with Clipping Mask and of course selecting objects is not the same as selecting files...

I thought that maybe it's possible to change the script so it would change all Links on active layer??

So would someone help me with that or maybe show me the right way to fix this issue?

This topic has been closed for replies.
Correct answer Disposition_Dev

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

1 reply

Silly-V
Legend
July 27, 2018

The way to do would be still rely on selection (start out by selecting all on the active layer), except do not iterate the selection but rather iterate the document placedItems (on the document level) and see if they have the .selected property as 'true'. Then you can be sure you have gotten a hold of the right placed item at which point you're free to edit their .file property which links them to wherever.

To this day I'm not sure if there's a difference between the changing of the .file property and the PlacedItem.relink() function..

MCzaAuthor
Inspiring
July 30, 2018

TBH I had no idea how to do this that way you suggested, but after some searching i've found this scripting guide and in example there is (not) exactly what i've needed.

with that code i changed original script and it looks like this:

#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 now it relinks ALL files, not just these on active layer.

I have a feeling that the solution is very close and very easy

Disposition_Dev
Legend
July 30, 2018

you said "it looks like this:" and then there's nothing there.. did something get left out?

based on your description it sounds like you're missing the conditional statement that Silly-V talked about. you need to check whether or not the placedItem in question is actually on the correct layer before re-linking it.

Can you share your code so we can test it and see exactly what the problem might be? Without being able to look at the code it's difficult to diagnose the problem. All we can do is speculate.