Copy link to clipboard
Copied
Hey Script forum,
I was wondering if anyone knows of a script that could remove spaces and lowercase the link names in a document, and relink the image. I'm getting some errors wen exporting ePubs about spaces and uppercase letters. Obviously I can change them manually, but it would be so nice if it could be done automatically.
I tried to run a search for such a great tool/script, but so far I've come up flat.
Thanks,
Tara
Copy link to clipboard
Copied
Have you tried the below link:
http://carijansen.com/2011/11/10/indesign-epub-support-tool-renaming-image-links/
Vandy
Copy link to clipboard
Copied
Thanks Vandy,
It's close to what I'm looking for, but still leaves a lot of manual changing. But thank you so much for forewarding it to me. I'll definitely have use for it in other projects.
Tara
Copy link to clipboard
Copied
You can try something like this:
// relink_ImgWithCleanedName.jsx
// http://forums.adobe.com/thread/1325315?tstart=0
// Remove spaces and lowercase image link names (and relink)
// required: an opened document with placed image
// regards pixxxelschubser
var imgs = app.activeDocument.allGraphics;
if (imgs.length != 0) {
//var imgs = app.activeDocument.links;
var img = imgs[0].itemLink;
if (img.status == LinkStatus.NORMAL ) {
var old_imgName = img.name;
var old_imgPath = img.filePath;
var old_imgFile = File (old_imgPath);
//var new_imgName = old_imgName.replace(/ /g,'').toLowerCase();
// --> does what you want - but I prefer this:
var new_imgName = old_imgName.replace(/ /g,'_').toLowerCase();
old_imgFile.rename(new_imgName);
var new_namedFile = File (old_imgFile.parent.fsName+"//"+new_imgName);
img.relink(new_namedFile);
img.update();
} else {alert("LinkStatus von: "+img.name + " --> " +img.status)}
} else {alert("no linked graphics")}
All you have to do is to add a loop through all graphics linked items.
Have fun
![]()
Copy link to clipboard
Copied
So how do you add a loop to this code so that it goes through all the images in the open document?
Copy link to clipboard
Copied
At first: this thread is more than 5 years old.
But anyway:
for ( i=0; i <= img.length-1; i++ ) {
//…
}
Copy link to clipboard
Copied
Sorry yes, it is 5 years old but the code still works so thank you for the reply. When I wrap this code it throws an error. Is a specific section of the code that needs to be wrapped.
Copy link to clipboard
Copied
Do you have scripting experience?
untestet:
var imgs = app.activeDocument.allGraphics;
if (imgs.length != 0) {
for ( i=0; i <= img.length-1; i++ ) {
//var imgs = app.activeDocument.links;
var img = imgs.itemLink;
if (img.status == LinkStatus.NORMAL ) {
var old_imgName = img.name;
var old_imgPath = img.filePath;
var old_imgFile = File (old_imgPath);
//var new_imgName = old_imgName.replace(/ /g,'').toLowerCase();
// --> does what you want - but I prefer this:
var new_imgName = old_imgName.replace(/ /g,'_').toLowerCase();
old_imgFile.rename(new_imgName);
var new_namedFile = File (old_imgFile.parent.fsName+"//"+new_imgName);
img.relink(new_namedFile);
img.update();
}
} else {alert("LinkStatus von: "+img.name + " --> " +img.status)}
} else {alert("no linked graphics")}
Copy link to clipboard
Copied
A little. I can frankenstein scripts together but can't write them from scratch. I'm just about to start a javascript course to learn the basics and hopefully will be able to start writing them with more understanding. Thank you for your help.
Copy link to clipboard
Copied
I'll keep hammering at it but unfortunatly it still doesn't work, it says there is Offending text "else".
Copy link to clipboard
Copied
You are right.
There was two typos.
Try
var imgs = app.activeDocument.allGraphics;
if (imgs.length != 0) {
for ( i=0; i <= imgs.length-1; i++ ) {
//var imgs = app.activeDocument.links;
var img = imgs.itemLink;
if (img.status == LinkStatus.NORMAL ) {
var old_imgName = img.name;
var old_imgPath = img.filePath;
var old_imgFile = File (old_imgPath);
//var new_imgName = old_imgName.replace(/ /g,'').toLowerCase();
// --> does what you want - but I prefer this:
var new_imgName = old_imgName.replace(/ /g,'_').toLowerCase();
old_imgFile.rename(new_imgName);
var new_namedFile = File (old_imgFile.parent.fsName+"//"+new_imgName);
img.relink(new_namedFile);
img.update();
} else {alert("LinkStatus von: "+img.name + " --> " +img.status)}
}
} else {alert("no linked graphics")}
Copy link to clipboard
Copied
What I use for e-books and web content. Personally, I like all lowercase and all punctuation replaced with dashes. Can be modified as needed.
var doc;
var ext;
var file;
var i;
var link;
var name;
doc = app.activeDocument;
for (i = 0; i < doc.links.length; i++) {
link = doc.links;
file = new File(link.filePath);
name = link.name.replace(/.[^.]+$/, "");
ext = link.name.replace(/^.*\./, "");
name = name.toLowerCase();
ext = ext.toLowerCase();
// Replace punctuation with dash.
name = name.replace(/[ ~`@#$%\^&\*()_=:;,\."'_~+<>\/\?\{\}\[\]\|\\]/g, "-");
// Delete any leading dashes.
name = name.replace(/^-/g, "");
// Reduce double dashes to one.
name = name.replace(/--/g, "-");
if (file.exists) {
// Change file name by adding 'tmp-' and then back to
// new name so that change to lowercase takes effect.
file.rename("tmp-" + name + "." + ext);
file.rename(name + "." + ext);
// Then relink and update renamed file.
link.relink(file);
link.update();
}
}
alert("Links successfully renamed and relinked.");
Copy link to clipboard
Copied
Is it possible that removing spaces may cause a duplicate filename? (Of course it *can*, but does your workflow prevent it right from the start?)
Copy link to clipboard
Copied
Hi!
Maybe this script will be helpful:
https://sites.google.com/site/dtpscripting/indesign-scripts/standardlinksnames-jsx
Although it does not lowercase links names, but it could be modified for that purpose.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more