Skip to main content
jeremy0013
Inspiring
October 29, 2015
Answered

Updating all multiple linked files

  • October 29, 2015
  • 3 replies
  • 1251 views

Hello all, again.  I am needing to update multiple linked files within one document.   The linked objects are on different layers. Can I get some suggestions on how to make this work?

This topic has been closed for replies.
Correct answer Qwertyfly___

in the code you posted above you declare plc to be an array of all placed items.

then you just alert plc which will always give you "[PlacedItems]"

try changing the alert to:

alert(plc);

this will give you "[PlacedItem ]" for each placed item

give this a go.

open a new document, and place 1 image in it.

run this code:

var doc = app.activeDocument;

var plc = doc.placedItems;

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

    try{

        alert (plc.file);

    }catch(e){

        alert(e);

    }

}

it should give you the file name of the linked image.

now go and rename the image, then re run the above code.

it should now say "Error: There is no file associated with this item"

this is because the link has been broken.

let me know how you go.

this should help in understanding the way files are linked.

3 replies

jeremy0013
Inspiring
November 2, 2015

When I put an alert on it, when looping Illustrator says [Placed File].  This may be why it can't find the file?

var orderNumber = "99999" 

var doc = app.activeDocument; 

var plc = doc.placedItems; 

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

    alert (plc);

}

Qwertyfly___
Qwertyfly___Correct answer
Legend
November 3, 2015

in the code you posted above you declare plc to be an array of all placed items.

then you just alert plc which will always give you "[PlacedItems]"

try changing the alert to:

alert(plc);

this will give you "[PlacedItem ]" for each placed item

give this a go.

open a new document, and place 1 image in it.

run this code:

var doc = app.activeDocument;

var plc = doc.placedItems;

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

    try{

        alert (plc.file);

    }catch(e){

        alert(e);

    }

}

it should give you the file name of the linked image.

now go and rename the image, then re run the above code.

it should now say "Error: There is no file associated with this item"

this is because the link has been broken.

let me know how you go.

this should help in understanding the way files are linked.

jeremy0013
Inspiring
October 29, 2015

I have 5 placed images in a illustrator pdf.  On open I want to update the path to where they are linked.  I already have a prompt in place for the orderNumber so when the script runs it does as follows.  As well all of the links will be previously named the same.  i.e. front.jpg - back.jpg

prompt - Order Number

open(orderNumber);

updateLinks to - Path (File ("file://User/Desktop/" + orderNumber + "/" "LinkedImages/")

And if it helps, since they are previously named we can access them by name for update.  Let me know.

Qwertyfly___
Legend
October 30, 2015

here is the basics

var orderNumber = "13445"

var doc = app.activeDocument;

var plc = doc.placedItems;

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

    var fileName = plc.file.name;

    plc.file = File("~/Desktop/" + orderNumber + "/LinkedImages/" + fileName);

}

jeremy0013
Inspiring
October 30, 2015

Keeps saying no file associated with this item.  I'm using a link that I know works. Am I doing something wrong?

Vinicius Baptista
Inspiring
October 29, 2015

Hello jeremy, I think it can help you,


To select all files with the same name on the list of this script, double-click it.

This script works only with selected links if you do not select anything it will work with all document links.

if (app.documents.length > 0){

var doc = app.activeDocument;

if (doc.placedItems.length > 0){

var winLinks = new Window ('dialog', 'Replace Links');

var listLinks = winLinks.add ('ListBox', undefined, '', {multiselect: true, numberOfColumns: 2, showHeaders: true, columnTitles: ['Index', 'Files']});

listLinks.maximumSize = [400, 400];

var btnRelink = winLinks.add ('button', undefined, 'Relink');

var Relink = function(){

if (doc.selection.length < 1){

  

for ( i = 0; i < app.activeDocument.placedItems.length; i++ ) {

placedArt = app.activeDocument.placedItems;

placedArt.selected = !(placedArt.selected);

};

app.redraw();

};

    var strLinkItem = new Array(new Array());

    for (i=0;i<doc.placedItems.length;i++){

if(doc.placedItems.selected){

      

    var sliceString = String(doc.placedItems.file);

    var sliceThisString = sliceString.slice(sliceString.lastIndexOf('/') + 1, sliceString.length);

  

    strLinkItem = [sliceThisString];

  

    for (x=0; x<1;x++){

        strLinkItem[1] = i;

        };

}else{

    strLinkItem = ["Empty"];

  

    for (x=0; x<1;x++){

        strLinkItem[1] = i;

        };

        };

      

    };

strLinkItem.sort();

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

if (strLinkItem[0] != "Empty"){

    var linkItem = listLinks.add('item', strLinkItem[1]);

    linkItem.subItems[0].text = strLinkItem[0];

};

};

listLinks.selection = [0];

};

btnRelink.onClick = function() {

  

    var newFile = File.openDialog ('Select the file that will replace the selected links.');

    if (newFile){

      

    var vetorSel = new Array();

    for(c=0; c < listLinks.items.length; c++){

        vetorSel.push(listLinks.items);

    };      

      

    for (z=0; z<listLinks.selection.length; z++){

        doc.placedItems[listLinks.selection].file = newFile;

        };

  

        for (k=0; k < vetorSel.length; k++){

            doc.placedItems[vetorSel].selected = true;

            };

      

        listLinks.removeAll();

        app.redraw();

        Relink();

    };

    };

listLinks.onDoubleClick = function (){

if (listLinks.selection != null){

  

    var totalList = listLinks.selection.length;

  

    var itemName = String(listLinks.selection[(totalList - 1)].subItems[0]);

  

    if (totalList = 1){

    var newSel = [];

    };

        for (a=0; a<listLinks.items.length; a++){

            if(listLinks.items.subItems[0] == itemName){

                newSel.push(listLinks.items.index);

                };

        };

    listLinks.selection = newSel;

    };

};

winLinks.show(Relink());

}else{alert("Any link in the current document.")};

}else{alert("No file is currently open.")};

Enjoy it.

Best regards...

jeremy0013
Inspiring
October 29, 2015

Thanks V.  I need it to select the linked files and relink them to a order specific folder.  I came up with this, and it almost works but it links all of them to the same file not all to their own.  So in short, I only need to change the link of where the placed files are located and update.

var orderNumber = prompt('Enter Order Number', '');

var theFile = File("file://Users/jeremydeboer/Desktop" + "/" + orderNumber  + "/" + orderNumber  + "_Proof.pdf"); //* Load Proof

var docRef = app.activeDocument;

var layers = docRef.layers;

var myLayer = layers["Placed Roster"];

docRef.selection = null;

for(var a=0;a<myLayer.pageItems.length;a++){

    var currentItem = myLayer.pageItems;

     currentItem.selected = true; 

}

sel = docRef.selection; 

if (sel.length>0) 

     { 

          var file = File("file://Users/jeremydeboer/Desktop" + "/" + orderNumber  + "/" + orderNumber  + "_ProofRoster.pdf");

          for (i=0 ; i<sel.length ; i++ ) 

               { 

                   if (sel.typename == "PlacedItem") 

                        { 

                            var iplaced = sel

                            iplaced.file = file; 

                         } 

               } 

     }

Qwertyfly___
Legend
October 29, 2015

have a look at file>package...

is this what you are trying to do?

compile all links to 1 folder.