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

Remove spaces and lowercase image link names

Explorer ,
Oct 30, 2013 Oct 30, 2013

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

TOPICS
Scripting
2.4K
Translate
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
Enthusiast ,
Oct 30, 2013 Oct 30, 2013
Translate
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
Explorer ,
Oct 31, 2013 Oct 31, 2013

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

Translate
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 ,
Nov 02, 2013 Nov 02, 2013

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

Translate
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
Engaged ,
Jan 21, 2019 Jan 21, 2019

So how do you add a loop to this code so that it goes through all the images in the open document?

Translate
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 ,
Jan 21, 2019 Jan 21, 2019

At first: this thread is more than 5 years old.

But anyway:

for ( i=0; i <= img.length-1; i++ ) {

    //…

    }

Translate
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
Engaged ,
Jan 21, 2019 Jan 21, 2019

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.

Translate
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 ,
Jan 21, 2019 Jan 21, 2019

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

Translate
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
Engaged ,
Jan 21, 2019 Jan 21, 2019

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.

Translate
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
Engaged ,
Jan 21, 2019 Jan 21, 2019

I'll keep hammering at it but unfortunatly it still doesn't work, it says there is Offending text "else".

Translate
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 ,
Jan 21, 2019 Jan 21, 2019

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

Translate
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
Enthusiast ,
Jan 26, 2019 Jan 26, 2019
LATEST

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

William Campbell
Translate
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 ,
Nov 02, 2013 Nov 02, 2013

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

Translate
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
Explorer ,
Nov 02, 2013 Nov 02, 2013

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.

Translate
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