Copy link to clipboard
Copied
Ive been at this for days and I've had no luck with this. I'm trying to highlight text that contains an anchored image within it and if the image I'm looking for exists I'd like to replace it. The problem i think I'm having is with the formats—the extensions are different.
The existing image is a raster .tif and the one I'd like to swap it with is an .ai vector file. The code below will get me the filenames for both images. The issue is that the file is not getting replaced. Does anyone know what I may be doing wrong?
Like i said I've been at this for a few days and maybe I'm overthinking this so far I've gotten myself lost. What's the best way to handle replacing the one image with another?
The script is just using the first index of the image I want to replace for now, Ill adjust it later to include more. The .ai needs to be selected as different users will have the replacement file in different locations.
// grab the path from the image contained within the selection
var my_Tif_Image = app.selection[0].allGraphics[0].properties.itemLink.filePath;
//create file object to get its path
var new_Ai_Image = File.openDialog ('select image');
//set path A to path B
my_Tif_Image = new_Ai_Image;
Thanks in advance for your time.
Try this (not at computer nor have Indesign and am pretty rusty, so can't verify):
I don't think you need my_Tif_Image. Instead, leave your "var new_Ai_image" line then try:
var my_Tif_Image = app.selection[0].allGraphics[0].parent.place(new_Ai_image);
Copy link to clipboard
Copied
Try this (not at computer nor have Indesign and am pretty rusty, so can't verify):
I don't think you need my_Tif_Image. Instead, leave your "var new_Ai_image" line then try:
var my_Tif_Image = app.selection[0].allGraphics[0].parent.place(new_Ai_image);
Copy link to clipboard
Copied
Hi spicyDoge ,
if you need an openDialog to fetch the ai graphic you could also use the relink command in the Links panel 😉
That aside, are the names the same or similar enough* so that you can automate relinking without having the dialog?
*Similar enough means you can do the name of the ai file using a RegExp by looking at the TIF file's name.
To relink a link you have to use method relink() on the Link object.
Also check property preserveBounds of app.imagePreferences.
// graphic frame selected holding the tif image:
// ai file has the same name
// Don't know if that is useful with your sample application:
app.imagePreferences.preserveBounds = true ;
var graphicFrame = app.selection[0];
var placedFile = File( graphicFrame.graphics[0].itemLink.filePath );
var folderOfPlacedGraphic = placedFile.parent;
// Maybe we need a more sophisticated RegExp to get the name of the ai file:
var nameOfPlacedFileWithoutSuffix = placedFile.name.replace(/\.+$/,"");
var aiFile = File( folderOfPlacedGraphic +"/"+ nameOfPlacedFileWithoutSuffix +".ai" );
if(aiFile.exists){ graphicFrame.graphics[0].itemLink.relink( aiFile ) };
Regards,
Uwe