Skip to main content
Mike_Gondek10189183
Community Expert
Community Expert
September 11, 2017
Answered

relink all selected

  • September 11, 2017
  • 8 replies
  • 7486 views

CarlosCanto​ wrote this script for me years ago, but seems to not work anymore on

Mac OS 10.10.5

CC2015.3

Is supposed to relink all instance of an image, as for example on a carton in packaging design we make have the same picture of an apple pie on all 6 sides. Is annoying to run the link panel 6 times rather than one command. This was further aggravated by the Mac OS not remember ing the last location, which default folder used to correct, but that seems to work now.

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

var idoc = app.activeDocument;

sel = idoc.selection;

if (sel.length>0)

     {

       

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

     }

Correct answer CarlosCanto

Hi dimitri, sorry but it works fine here, I'm on Windows though.

 

are you using this script?

 

 

//#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; 05/12/19 updated for CC2019
// script.elegant = false;
 
var idoc = app.activeDocument;
sel = idoc.selection;
if (sel.length>0) {
      var file = File.openDialog ("open file");
      file = new File(file.fsName.replace("file://","")); // Mac OS Lion fix by John Hawkinson
      
      for (i=0 ; i<sel.length ; i++ ) {
           if (sel[i].typename == "PlacedItem") {
                var iplaced = sel[i];
                iplaced.file = file;
             }
           if (sel[i].typename == "RasterItem") {
                var iplaced = idoc.placedItems.add();
                iplaced.file = file;
                
                iplaced.move(sel[i], ElementPlacement.PLACEBEFORE);

                h = sel[i].height;
                w = sel[i].width;
                x2 = sel[i].left;
                y2 = sel[i].top;

                iplaced.position = [x2+w/2-iplaced.width/2, y2-h/2+iplaced.height/2];
                
                sel[i].remove();
             }
       }
}
else
     {
          alert("select at least one placed item before running");
     }

 

 

8 replies

Inspiring
July 15, 2021

For anyone who still wants to use this script, I have refined it a bit to be able to replace every selected image through a multi-selection of files (jpg/jpeg/png) and if there are more selected images than files, it will loop the last file through your entire selection (good when you want to replace multiple images with 1 file).

const idoc = app.activeDocument;
const sel = idoc.selection;
if (sel.length > 0) {
     sel.reverse();
     // Select files
     var files = null;
     if ($.os.search(/windows/i) != -1) {
          // Windows
          files = File.openDialog("please select files", "*.jpg;*.jpeg;*.png", true);
     } else {
          // MacOs
          files = File.openDialog("please select files", getFiles, true);
     }
     if (files) {
          // Loop through selection
          for (x = 0; x < sel.length; x++ ) {
               var i = x;
               // Replace all leftover images with last selected image
               if (x >= files.length) { i = files.length - 1; }
               if (sel[x].typename == "PlacedItem") {
                    sel[x].file = files[i];
               } else if (sel[x].typename == "RasterItem") {
                    var iplaced = idoc.placedItems.add();
                    iplaced.file = files[i];
                    iplaced.move(sel[x], ElementPlacement.PLACEBEFORE);
                    iplaced.left = sel[x].left;
                    iplaced.top = sel[x].top;
                    iplaced.height = sel[x].height;
                    iplaced.width = sel[x].width;
                    sel[x].remove();
               }
          }
     } else { alert("select at least one file"); }
} else { alert("select at least one placed item before running"); }

// Get jpg, jpeg and png from files (macOs)
function getFiles(f) {
     return f.name.match(/\.(jpg|jpeg|png)$/i) != null || f.constructor.name == "Folder";
}
Paolo Civiero
Participant
September 7, 2023

a litlte bit change for replace pdf ai psd and tif linked files


#target Illustrator

const idoc = app.activeDocument;
const sel = idoc.selection;
if (sel.length > 0) {
     sel.reverse();
     // Select files
     var files = null;
     if ($.os.search(/windows/i) != -1) {
          // Windows
          files = File.openDialog("please select files", "*.ai;*.pdf;*.psd;*.tif*.jpg;*.jpeg;*.png", true);
     } else {
          // MacOs
          files = File.openDialog("please select files", getFiles, true);
     }
     if (files) {
          // Loop through selection
          for (x = 0; x < sel.length; x++) {
               var i = x;
               // Replace all leftover images with last selected image
               if (x >= files.length) { i = files.length - 1; }
               if (sel[x].typename == "PlacedItem") {
                    sel[x].file = files[i];
               } else if (sel[x].typename == "RasterItem") {
                    var iplaced = idoc.placedItems.add();
                    iplaced.file = files[i];
                    iplaced.move(sel[x], ElementPlacement.PLACEBEFORE);
                    iplaced.left = sel[x].left;
                    iplaced.top = sel[x].top;
                    iplaced.height = sel[x].height;
                    iplaced.width = sel[x].width;
                    sel[x].remove();
               }
          }
     } else { alert("select at least one file"); }
} else { alert("select at least one placed item before running"); }

// Get jpg, jpeg and png from files (macOs)
function getFiles(f) {
     return f.name.match(/\.(pdf|ai|psd|tif|jpg|jpeg|png)$/i) != null || f.constructor.name == "Folder";
}
Participating Frequently
July 28, 2020

Is there a version of this script that allows for "show import options"?  Would like to change the artboard within an ai file that's being linked to. 

 

Thanks

K

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
November 23, 2019

Hi dimitri, sorry but it works fine here, I'm on Windows though.

 

are you using this script?

 

 

//#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; 05/12/19 updated for CC2019
// script.elegant = false;
 
var idoc = app.activeDocument;
sel = idoc.selection;
if (sel.length>0) {
      var file = File.openDialog ("open file");
      file = new File(file.fsName.replace("file://","")); // Mac OS Lion fix by John Hawkinson
      
      for (i=0 ; i<sel.length ; i++ ) {
           if (sel[i].typename == "PlacedItem") {
                var iplaced = sel[i];
                iplaced.file = file;
             }
           if (sel[i].typename == "RasterItem") {
                var iplaced = idoc.placedItems.add();
                iplaced.file = file;
                
                iplaced.move(sel[i], ElementPlacement.PLACEBEFORE);

                h = sel[i].height;
                w = sel[i].width;
                x2 = sel[i].left;
                y2 = sel[i].top;

                iplaced.position = [x2+w/2-iplaced.width/2, y2-h/2+iplaced.height/2];
                
                sel[i].remove();
             }
       }
}
else
     {
          alert("select at least one placed item before running");
     }

 

 

Inspiring
November 24, 2019

Good morning Carlos -

 

Apparently not,

but this one his working like a charm.   😄

 

Thank you sooooo much.

 

 

- Dimitri

 

Participant
February 20, 2020

Here is a slightly more simple version

 

 

#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

// ---------------------------------------------------------------------------------

var idoc = app.activeDocument;

sel = idoc.selection;

if (sel.length>0)

{

var file = File.openDialog ("open file");

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

{

if (sel[i].typename == "PlacedItem")

{

var iplaced = sel[i];

//var file = File.openDialog ("open file " + iplaced.file );

iplaced.file = file;

}

}

}

else

{

alert("select at least one placed item before running");

}

CarlosCanto
Community Expert
Community Expert
November 19, 2019

Hi dimitri, can you share a sample file to play with?

Inspiring
November 23, 2019

Hello Carlos -

 

Please, find here a rogh AI file with a couple of pictures —>   https://file.io/IxePDv

I run under Mac OS X Mojave 10.14.6 + AI CC2019

 

Big thanks for your lihghts and time.

 

 

 

- Dimitri

 

CarlosCanto
Community Expert
Community Expert
November 15, 2019

your images are probably rasterItems (embedded) instead of placed, can you confirm?

if that's the case, there's another version of the script that deals with both types of images

Inspiring
November 16, 2019

Hello Carlos -

 

I double checked and they are not.

So they are NOT embedded.

 

 

- Dimitri

 

CarlosCanto
Community Expert
Community Expert
November 14, 2019

did you select your images on the artboard first?

Inspiring
November 15, 2019

Hey Carlos -

 

Yes, I do, otherwise I'd get the "alert" message…

Any idea?

 

 

- Dimitri

 

Inspiring
November 14, 2019

Good evening -

 

I'm running under Illustrator CC 23.1.1

And when I launch the script, well AI asks me to open a file (the new one), I click on "open"…

But after that nothing happen.

 

Any idea why?

Thank you

 

 

 

- Dimitri

 

Silly-V
Legend
September 11, 2017

Have you thought of using the Illustrator variable data to help with this workflow?

Mike_Gondek10189183
Community Expert
Community Expert
September 11, 2017

Would that help here? All I want to do is update 6 links of:

\Volumes\Kaleidoscope Server\1 Jobs\ABCD\AWG\79786 AWG Best Choice Ice Cream Range Design\5 Implementation\1 Images\Vanilla1.psd

to

\Volumes\Kaleidoscope Server\1 Jobs\ABCD\AWG\79786 AWG Best Choice Ice Cream Range Design\5 Implementation\1 Images\Vanilla2.psd

Silly-V
Legend
September 12, 2017

In your case, probably not. Or, probably may, depending if you find yourself needing to replace multiple images with different images!

You see, in a less-than-ideal workaround, you can save a spreadsheet .txt or .csv file with just these contents:

@my-image
newImage.png

The document's images you want to relink would need to be named "my-image" so they can be bound by name via VariableImporter.jsx

The thing is, you will need to change "newImage.png" to your new image name every time - so you'd have to keep this text file open and make edits to it throughout the day. The downsides are the spreadsheet, the clicks to open it inside VariableImporter dialog, but the upside is that your csv can be a nice spot to keep your latest used file path (instead of getting it from links panel), your placed files are all replaced at the same time, and if needed, you can actually relink multiple sets of placed files, using multiple @my-image-x columns.

Another thing is, I need to fix up the VariableImporter to remember name of last csv location used, so that you don't have to navigate to it every time. And, I have a bug which messes up server paths on Windows. So, I'll get back to you with the changes