Copy link to clipboard
Copied
Is it possible by means of a javascript to rename a selection of images of a document in a bulk with a suffix and relink. (For example: filename.jpg to filename_C.jpg)
Yes, you should familiarize yourself with the File object and Link object. Link.copyLink(File) is going to be your primary mode of doing so. Edited with a demonstration (you'll want graphic containers (ie rectangles and/or ovals) selected):
var sels = app.selection;
var suffix = "_C";
var extRegEx = /\.[a-zA-Z]+$/gi;
var aLink, linkName, linkExt, linkNameNoExt;
for (var i = 0; i < sels.length; i++) {
try {
aLink = sels[i].graphics[0].itemLink;
linkName = aLink.filePath;
...
Copy link to clipboard
Copied
Yes, you should familiarize yourself with the File object and Link object. Link.copyLink(File) is going to be your primary mode of doing so. Edited with a demonstration (you'll want graphic containers (ie rectangles and/or ovals) selected):
var sels = app.selection;
var suffix = "_C";
var extRegEx = /\.[a-zA-Z]+$/gi;
var aLink, linkName, linkExt, linkNameNoExt;
for (var i = 0; i < sels.length; i++) {
try {
aLink = sels[i].graphics[0].itemLink;
linkName = aLink.filePath;
linkExt = linkName.match(extRegEx)[0];
linkNameNoExt = linkName.replace(extRegEx, "");
aLink.copyLink(File(linkNameNoExt + suffix + linkExt));
} catch(e) {}
}
Copy link to clipboard
Copied
Note that this creates a copy of the image in its original location, rather than renames it. But it's the easiest way to do it.
Copy link to clipboard
Copied
Thanks Brain311 it works!!!
And is it also possible with a selection in the 'links' panel (selection of images on different pages)?
Copy link to clipboard
Copied
No. Those selections are not accessible. Selections only work on a single spread. If you want it to operate on images across pages, you'll need some kind of logic in the code to grab the right ones.
Copy link to clipboard
Copied
Ok! Thanks!
Copy link to clipboard
Copied
A fine to place to start is to get allGraphics of a Document object, then iterate through them.
var ags = app.activeDocument.allGraphics;
var aLink;
for (var i = 0; i < ags.length; i++) {
try {
aLink = ags[i].itemLink;
//your if (someCondition) code here
//look at the Link object API here:
//https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Link.html
} catch(e) {}
}
Copy link to clipboard
Copied
Whoww... hat is a bit too complicated for me