• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Rename (with suffix) and relink an selection images

Community Beginner ,
Apr 21, 2021 Apr 21, 2021

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)

TOPICS
Scripting

Views

517

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 21, 2021 Apr 21, 2021

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

Votes

Translate

Translate
Community Expert ,
Apr 21, 2021 Apr 21, 2021

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) {}
}

 

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 22, 2021 Apr 22, 2021

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 22, 2021 Apr 22, 2021

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)?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 22, 2021 Apr 22, 2021

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 22, 2021 Apr 22, 2021

Copy link to clipboard

Copied

Ok! Thanks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 22, 2021 Apr 22, 2021

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) {}
} 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 22, 2021 Apr 22, 2021

Copy link to clipboard

Copied

LATEST

Whoww... hat is a bit too complicated for me

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines