Every so often, I have to update "Real World Adobe InDesign," (I wrote the first edition, and have collaborated with David Blatner on three other editions). Because we suffer from various personality flaws, we've used a different scheme for each edition for naming the multitudinous graphics files that make up the illustration in the book.
During production, these different naming "systems" (I use the term very loosely) become a headache. Does "XXtabs palette.01.tif" go in the same illustration with "set tabs_a.tif"? And what do either of them have to do with "02 tab set.tif"?
We could, of course, go to the Finder/Explorer and change the file names, then return to InDesign and relink each graphic to the corresponding renamed file. But that kind of back-and-forth, painstaking operation takes lots of time (which we never have). And it's an error-prone process, as well.
For this production cycle, I decided to do something about it. The following script renames the file linked to the selected graphic, then relinks the graphic to the renamed file. As you can see from the comments, this script isn't perfect--but it's good enough to save me a lot of time and trouble.
To use the script, copy it out of the message and paste it into a text editor (BBEdit or Notepad will work just fine). Save the file as plain text, giving it the file extension ".jsx". Put the file in the Scripts folder inside the Presets folder in your InDesign folder. The script will now appear in the Scripts palette (Window>Automation>Scripts). To run the script, select a graphic (or its frame), then double-click the script name in the Scripts palette.
//RenameRelink.jsx
//An InDesign CS2 JavaScript
//
//Renames the file of the selected linked graphic and relinks to the renamed graphic.
//Used in the production of "Real World Adobe InDesign CS2"
//
if(app.documents.length != 0){
if((app.documents.item(0).allGraphics.length != 0)&&(app.selection.length != 0)){
//A production version of this script would probably include
//a more complete list of graphic/page item types.
switch(app.selection[0].constructor.name){
//If a frame is selected with the Selection tool...
case "Rectangle":
case "Oval":
case "Polygon":
if(app.selection[0].graphics.length != 0){
myDisplayDialog(app.selection[0].graphics.item(0));
}
break;
//If a graphic is selected with the Direct Selection tool...
case "Image":
case "EPS":
case "PDF":
myDisplayDialog(app.selection[0]);
break;
}
}
}
function myDisplayDialog(myGraphic){
//A production version of this script would probably check the link
//status before proceeding to get the link file.
var myLink = myGraphic.itemLink;
var myLinkName = myLink.name;
var myLinkFile = File(myLink.filePath);
//A production version of this script would check to see if
//the link file is present, and whether it's an alias/shortcut
//rather than a file.
var myDialog = app.dialogs.add({name:"Rename Graphic"});
with(myDialog.dialogColumns.add()){
with(dialogRows.add()){
with(dialogColumns.add()){
staticTexts.add({staticLabel:"File name:"});
}
with(dialogColumns.add()){
var myFileNameField = textEditboxes.add({editContents:myLinkName, minWidth:200});
}
}
}
var myResult = myDialog.show();
if(myResult == true){
var myFileName = myFileNameField.editContents;
myDialog.destroy();
myRenameGraphic(myLink, myLinkFile, myFileName);
}
else{
myDialog.destroy();
}
}
function myRenameGraphic(myLink, myLinkFile, myFileName){
//A production version of the script would put up an error if the
//file cannot be renamed (for whatever reason).
myLinkFile.rename(myFileName);
myLink.relink(myLinkFile);
myLink.update();
}