Copy link to clipboard
Copied
I need to find a script that will rename the imported artwork files.
I have many artwork files with different names, that I would like to rename to a project number, and then relink them into the document.
I would first "package" the document to get the files into one common folder. Once the files are in one link folder, I would like to rename and link them to that project.
The script must allow me to assign the base file name.
Hi Bill,
Here is the script. It renames linked image files on page by page basis. At start a dialog pops up asking you to enter a base name, then a confirmation dialog appears warning that all links in the current document are going to be renamed, and if you press Ok all links located on pages (not on master pages or pasteboard) are renamed like so :
WWAAU_001_a.jpg, WWAAU_001_b.jpg, WWAAU_001_c.jpg and so on.
Where: WWAAU — base name
001 —the number of the page where the image is placed
a, b, c …
...Copy link to clipboard
Copied
Hi Bill,
Some time ago I wrote together with other guys such a script. There was a discussion on this subject: http://forums.adobe.com/message/1106014#1106014 — see post #62 and above.
Warning! Use it with care and only on copies of your files — it renames linked images in the active indd file.
Kasyan
Copy link to clipboard
Copied
Kasyan,
Thank you for the reply.
I am a bit confused about the script or what portion I am to copy.
Is there anyway you could e-mail the script?
I would be vary greatful.
Then a couple questions to be sure?
It will allow me to add a beginning name of my choice? Like "WWAAU" folowed by #### - where # = a figure number. Next document uses "WWAAV" and so on.
If I package the document and run the script it will then rename the files in the packaged book? Then I can recopy the files to my working directory. Thus your warning.
Copy link to clipboard
Copied
Hi Bill,
Here is the script. It renames linked image files on page by page basis. At start a dialog pops up asking you to enter a base name, then a confirmation dialog appears warning that all links in the current document are going to be renamed, and if you press Ok all links located on pages (not on master pages or pasteboard) are renamed like so :
WWAAU_001_a.jpg, WWAAU_001_b.jpg, WWAAU_001_c.jpg and so on.
Where: WWAAU — base name
001 —the number of the page where the image is placed
a, b, c … — numbering of images located on the same page
If I package the document and run the script it will then rename the files in the packaged book?
It depends on the Update Graphic Links In Package option: If It’s on — script will rename packaged copies, if not — original files (I assume that you run the script on the packaged document).
Kasyan
Copy link to clipboard
Copied
Hi I have been trying to use your scripts as they sound perfect for what we want but
I keep getting errors can anyone help me to get them running?
Thanks
Amy
Copy link to clipboard
Copied
Hi, Amy.
You have to help us help you! Please tell us exactly what errors you get. Otherwise we're left guessing and that makes it really tough to assist.
Copy link to clipboard
Copied
Hi John,
Sorry I thought I'd put the feelers out in thread which were a bit cold before I went into too much depth!
here is an example of an error:
I have actually made my own thread, as we do have a working script which we've got some minor ish issues with.
Amy
Copy link to clipboard
Copied
Umm...which script are you running that produces this error? It can't be Kasyan's
RenameLinksWithConfirm_DuncDude2.jsx.zip since that's only 54 lines long and doesn't include a variable called myPagesArr.
More details are necessary.
p.s.: you can include inline images by clicking the camera icon (3rd grouping), instead of an external attachment.
Copy link to clipboard
Copied
oh ok thanks - I saw what I did didn't work, I'm not really very good on forums (or at anyhting it seems this week - my I am feeling sorry for myself!)
I've tried the script in this thread:
http://forums.adobe.com/thread/288979?tstart=0
an also this one:
http://avondale.typepad.com/indesignupdate/2005/09/renamerelink_sc.html
this second one literally does nothing at all on my computer, no error or anything - I'm a bit confused between JavaScript and AppleScript too which is making things worse - I really feel now this is beyond my brain to understand or fix!
Amy
Copy link to clipboard
Copied
Hi Amy,
I amended the script so it works with ID 6.0.4 (this update has broken the way links are handled by script) and I also attached sample files to demonstrate how it works.
The second script is for CS2 – try to place it inside "Scripts Panel > Version 4.0 Scripts" folder and run again
Kasyan
P.S. I don't see the attach file option any more, could you give me your e-mail so that I could send you the files
Copy link to clipboard
Copied
Hi Kasyan
I've put the scripts in the panel as you suggested and they seem to get a bit further along before producing errors now, bummer I actually thought they were running!
I've sent you a pm wit my email, I really appreciate everyone's help this forum is brilliant!
Amy
edit: well i ran this one and still got an error - bit different this time, I'm think it's something I'm doing wrong?
Copy link to clipboard
Copied
I meant that the 2nd script — written by Olav Kvern — should be placed inversion 4.0 scripts folder.
The one I sent you should be placed in Scripts Panel folder.
Copy link to clipboard
Copied
Oh sorry, yes this runs now, it's not quite what we need but it's great to have some success!
unforunately we're not changing a job name, we're replacing any random file names which are inconsistent with one job number
i.e. they could be called
image1.tif
picture45a.psd
artwork004b.ai
etc
and we want to rename and remove the exisitng file names
to
JOBNAME001a.tif
JOBNAME001b.psd
JOBNAME002a.ai
it doesn't seem unachievable now, with a code I can actually see and work out how it's working!
Copy link to clipboard
Copied
I remember that I wrote this script – it should be somewhere on the forum, I'll look for it in my archive and let you know if I find it.
Copy link to clipboard
Copied
great thanks!
Copy link to clipboard
Copied
There were several versions of the script — this one renames images placed on master pages as well.
Who knows why attachment of files is unavailable any more?
//====================================================
var myDoc = app.activeDocument;
var myAllLinks = myDoc.allGraphics;
var myPrepend = prompt("Example: thebook_08765", "Job description", "Please enter job description");
var response = confirm("Warning: You are about to rename all images linked to the foremost Indesign Document - proceed? Keep in mind - it is not reversible!");
if ( response == true )
{
for ( k = 0; k < myAllLinks.length; k++ )
{
var myLinkName = myAllLinks
}
crearLabels();
var myPages = myDoc.pages;
for ( p = 0; p < myPages.length; p++ )
{
var myPageNumber = pad000(myPages
.name);
var myLinks = myPages
.allGraphics;
var myASCII = 97;
for ( k = myLinks.length - 1; k >= 0; k-- )
{
var myLink = myLinks
if (myLink.extractLabel("relinked") != "yes") {
var myOldLinkName = myLink.name;
var myLinkUsage = LinkUsage( myLink );
var myExtension = myOldLinkName.substr(myOldLinkName.lastIndexOf( "." ));
if (myLinkUsage == 1)
{
var myNewLinkName = myPrepend + myPageNumber + String.fromCharCode( myASCII ) + myExtension;
var myOldImageHDfile = new File( myLink.filePath );
myOldImageHDfile.rename( myNewLinkName );
myLink.insertLabel("relinked", "yes");
myLink.relink( myOldImageHDfile );
try {
myLink = myLink.update();
}
catch(err) {}
myASCII++;
}
else
{
processMultiUsedLinks(myLink);
}
}
}
}
var myMasterSpreads = myDoc.masterSpreads;
for ( m = 0; m < myMasterSpreads.length; m++ )
{
var myMastSpr = myMasterSpreads
var myPageNumber = myMastSpr.name;
var myPrefix = myMastSpr.namePrefix;
var myLinks = myMastSpr.allGraphics;
var myASCII = 97;
for ( n = myLinks.length - 1; n >= 0; n-- )
{
var myLink = myLinks
if (myLink.extractLabel("relinked") != "yes") {
var myOldLinkName = myLink.name;
var myExtension = myOldLinkName.substr(myOldLinkName.lastIndexOf( "." ));
var myLinkLetter = (myLinks.length == 1) ? "" : String.fromCharCode( myASCII );
var myNewLinkName = myPrepend + '_master_' + myPrefix + myLinkLetter + myExtension;
var myOldImageHDfile = new File( myLink.filePath );
myOldImageHDfile.rename( myNewLinkName );
myLink.insertLabel("relinked", "yes");
myLink.relink( myOldImageHDfile );
try {
myLink.update();
}
catch(err) {}
myASCII++;
}
}
}
}
//------------------------- Functions -------------------------------------
// Check how many times the link was placed
function LinkUsage(myLink) {
var myLinkCounter = 0;
for (var c = 0; c < myDoc.links.length; c++) {
if (myLink.filePath == myDoc.links
myLinkCounter += 1;
}
}
return myLinkCounter
}
// Relink the links placed more than once
function processMultiUsedLinks(myLink) {
var myMultiUsedLink = new Array();
var myPagesArr = new Array();
var myAllLinks = myDoc.links;
for (var d = 0; d < myAllLinks.length; d++) {
if (myAllLinks
myMultiUsedLink.push(myAllLinks
}
}
for (e = myMultiUsedLink.length-1; e >= 0 ; e--) {
myPagesArr.push(myMultiUsedLink
}
var myPageNames = myPagesArr.sort().join("_");
var myNewLinkName = myPrepend + '_p' + myPageNames + myExtension;
var myOldImageHDfile = new File( myLink.filePath );
myOldImageHDfile.rename( myNewLinkName );
myLink.insertLabel("relinked", "yes");
myLink.relink( myOldImageHDfile );
try {
myLink = myLink.update();
}
catch(err) {}
for (f = myMultiUsedLink.length-2; f >= 0 ; f--)
{
var myCurrLink = myMultiUsedLink
myCurrLink.insertLabel("relinked", "yes");
myCurrLink.relink( myOldImageHDfile );
try {
myCurrLink = myLink.update();
}
catch(err) {}
}
UpdateAllOutdatedLinks();
}
// Clear labels in case this script has been already run on the current document
function crearLabels() {
for (var f = 0; f < myDoc.links.length; f++) {
if (myDoc.links
myDoc.links
}
}
}
//--------------------------------------------------------------------------------------------------------------
function UpdateAllOutdatedLinks() {
for(var myCounter = myDoc.links.length-1; myCounter >= 0; myCounter--){
var myLink = myDoc.links[myCounter];
if (myLink.status == LinkStatus.linkOutOfDate){
myLink.update();
}
}
}
//--------------------------------------------------------------------------------------------------------------
function pad000(myNumber) {
if (myNumber >= 1 && myNumber <= 9) {
x = "0" + "0" + myNumber;
} else if (myNumber >= 10 && myNumber <= 99) {
x = "0" + myNumber;
} else if (myNumber >= 100 && myNumber <= 999) {
x = myNumber;
}
return x;
}
Copy link to clipboard
Copied
File attachments are disabled because of a security problem; see the overview notice at the non-scripts forum. When you paste, could you use the Insert (>>) function with Syntax Highligting -> Java? It makes the code a lot easier to read.
As a practical point, I think pasting it in is much much better anyhow. It means people actually READ your code, which is always good!
Copy link to clipboard
Copied
Thank you, John.
I'll certainly use Syntax Highligting — I used XML Highligting once but it removed quotation marks from my code, so I thought it would be more reliable to just attach scripts.
Kasyan
Copy link to clipboard
Copied
thanks for this, it's really really close...
...if anyone can help with what could be added to allow the script to offer manual insertion of a 'generic name for multiple images' when an image is placed more than once, it would be perfect for our requirements
I've order myself a book on Javascript so I can begin to unpick and fathom out what on earth it all means!
but this has really saved my nacon so thanks very much!
Amy
Copy link to clipboard
Copied
Sorry for the delay, I had meant to get to this a bit earlier.
It's not helpful for you to post in multiple threads. I'll post a note in the other thread directing followups here, then maybe someone can lock it. Your request in the other thread makes no sense without this thread, since it's highly dependent on Kasyan's script.
Here's a version of Kasyan's script that seems to solve your problem. This also fixes what seem to have been some bugs in Kasyan's pre-existing handling of multiply-linked images. Specifically the script seemed to get try to handle that case but get confused when it tried to relink a link it had already relinked. It think it was depending on the relinked image to be the last image in myMultiUsedLink, and perhaps that is not always true.
So anyhow, changes:
Amy, if this doesn't work for you, it would probably be best for you to make a sample document available. As simple as possible that still replicates the problem.
// from http://forums.adobe.com/message/2632448#2632448
// renameArtwork.jsx. Originally by Kasyan Servetsky.
//====================================================
var debug = 0;
var myDoc = app.activeDocument;
var myAllLinks = myDoc.allGraphics;
var myPrepend = prompt("Example: thebook_08765", "Job description", "Please enter job description");
var response = confirm("Warning: You are about to rename all images linked to the foremost Indesign Document - proceed? Keep in mind - it is not reversible!");
if ( response == true )
{
for ( k = 0; k < myAllLinks.length; k++ )
{
var myLinkName = myAllLinks.itemLink.name;
}
crearLabels();
var myPages = myDoc.pages;
for ( p = 0; p < myPages.length; p++ )
{
var myPageNumber = pad000(myPages.name);
var myLinks = myPages.allGraphics;
var myASCII = 97;
for ( k = myLinks.length - 1; k >= 0; k-- )
{
if (!myLinks.isValid) continue;
var myLink = myLinks.itemLink;
if (myLink.extractLabel("relinked") != "yes") {
var myOldLinkName = myLink.name;
var myLinkUsage = LinkUsage( myLink );
var myExtension = myOldLinkName.substr(myOldLinkName.lastIndexOf( "." ));
if (myLinkUsage == 1)
{
var myNewLinkName = myPrepend + myPageNumber + String.fromCharCode( myASCII ) + myExtension;
var myOldImageHDfile = new File( myLink.filePath );
if (myOldImageHDfile.rename( myNewLinkName) != true) {
if (debug) $.writeln("Failed to rename "+myLink.filePath+" to "+myNewLinkName);
continue;
} else
if (debug) $.writeln("renamed "+myLink.filePath+" to "+myNewLinkName+" see?:"+myOldImageHDfile.name);
myLink.insertLabel("relinked", "yes");
myLink.relink( myOldImageHDfile );
try {
myLink = myLink.update();
}
catch(err) {}
myASCII++;
}
else
{
processMultiUsedLinks(myLink);
}
}
}
}
var myMasterSpreads = myDoc.masterSpreads;
for ( m = 0; m < myMasterSpreads.length; m++ )
{
var myMastSpr = myMasterSpreads;
var myPageNumber = myMastSpr.name;
var myPrefix = myMastSpr.namePrefix;
var myLinks = myMastSpr.allGraphics;
var myASCII = 97;
for ( n = myLinks.length - 1; n >= 0; n-- )
{
var myLink = myLinks.itemLink;
if (myLink.extractLabel("relinked") != "yes") {
var myOldLinkName = myLink.name;
var myExtension = myOldLinkName.substr(myOldLinkName.lastIndexOf( "." ));
var myLinkLetter = (myLinks.length == 1) ? "" : String.fromCharCode( myASCII );
var myNewLinkName = myPrepend + '_master_' + myPrefix + myLinkLetter + myExtension;
var myOldImageHDfile = new File( myLink.filePath );
myOldImageHDfile.rename( myNewLinkName );
myLink.insertLabel("relinked", "yes");
myLink.relink( myOldImageHDfile );
try {
myLink.update();
}
catch(err) {}
myASCII++;
}
}
}
}
//------------------------- Functions -------------------------------------
// Check how many times the link was placed
function LinkUsage(myLink) {
var myLinkCounter = 0;
for (var c = 0; c < myDoc.links.length; c++) {
if (myLink.filePath == myDoc.links.filePath) {
myLinkCounter += 1;
}
}
return myLinkCounter
}
// Relink the links placed more than once
function processMultiUsedLinks(myLink) {
var myMultiUsedLink = new Array();
var myPagesArr = new Array();
var myAllLinks = myDoc.links;
for (var d = 0; d < myAllLinks.length; d++) {
if (myAllLinks.filePath == myLink.filePath) {
myMultiUsedLink.push(myAllLinks);
}
}
for (e = myMultiUsedLink.length-1; e >= 0 ; e--) {
myPagesArr.push(myMultiUsedLink.parent.parent.parent.name);
}
var myPageNames = myPagesArr.sort().join("_");
var thislinkname = prompt("Example: LISICON", "LISICON", "Image "+myLink.filePath+" is multiply-linked. Specify a tag.");
var myNewLinkName = myPrepend + thislinkname + myExtension;
var myOldImageHDfile = new File( myLink.filePath );
if (myOldImageHDfile.rename( myNewLinkName) != true) {
if (debug) $.writeln("Failed to rename "+myLink.filePath+" to "+myNewLinkName);
} else
if (debug) $.writeln("renamed "+myLink.filePath+" to "+myNewLinkName+" see?:"+myOldImageHDfile.name);
myLink.insertLabel("relinked", "yes");
myLink.relink( myOldImageHDfile );
try {
myLink = myLink.update();
}
catch(err) {}
for (f = myMultiUsedLink.length-1; f >= 0 ; f--)
{
var myCurrLink = myMultiUsedLink;
if (debug) $.writeln("f["+f+"] extracted label is "+myCurrLink.extractLabel("relinked"));
if (myCurrLink.extractLabel("relinked") != "yes") {
myCurrLink.relink( myOldImageHDfile );
myCurrLink.insertLabel("relinked", "yes");
}
try {
myCurrLink = myLink.update();
}
catch(err) {}
}
UpdateAllOutdatedLinks();
}
// Clear labels in case this script has been already run on the current document
function crearLabels() {
for (var f = 0; f < myDoc.links.length; f++) {
if (myDoc.links.extractLabel("relinked") != "") {
myDoc.links.insertLabel("relinked", "");
}
}
}
//------------------------------------------------------------------------------ --------------------------------
function UpdateAllOutdatedLinks() {
for(var myCounter = myDoc.links.length-1; myCounter >= 0; myCounter--){
var myLink = myDoc.links[myCounter];
if (myLink.status == LinkStatus.linkOutOfDate){
myLink.update();
}
}
}
//------------------------------------------------------------------------------ --------------------------------
function pad000(myNumber) {
if (myNumber >= 1 && myNumber <= 9) {
x = "0" + "0" + myNumber;
} else if (myNumber >= 10 && myNumber <= 99) {
x = "0" + myNumber;
} else if (myNumber >= 100 && myNumber <= 999) {
x = myNumber;
}
return x;
}
p.s.: why do some of you like to name variables "myVariable"? What's wrong with "variable"?
Copy link to clipboard
Copied
Hi John,
I talked to Amy on private by e-mail and she sent me a sample file. I remade the script and it seems to be working (ID 6.0.4, Mac), but I hadn't enough time to totally test it yet since I was terribly busy that day. I had saved an inx copy of the file and run it on my home computer today (ID 6.0.4, Windows), but the result was different — a warning appeared: "The file such and such has different layouts than the imported version, the link will be updated with the source files current layer visibility settings" and some images changed their scaling. I haven't sorted out why this happens yet.
I'm posting the latest version of the script. I added a logging feature to it — it creates a text file on the desktop. And it shows a multiply-linked image while prompting the user to enter a new name for it.
p.s.: why do some of you like to name variables "myVariable"? What's wrong with "variable"?
I do this mostly from force of habit: when I was learning scripting I studied Ole's sample files — he always names variables starting with "my". I think this makes sense: this way an inexperienced scripter avoids unintentionally using some reserved JS word. I also name my functions starting with upper case letter, as recommended by Kris Coppieters, so that not to mess up JS built in functions.
Kasyan
var myDoc = app.activeDocument;
var myAllLinks = myDoc.allGraphics;
var myMultipleLinks = new Array();
var myLinksCounter = 1;
var myPrepend = prompt("Example: thebook_08765", "Job description", "Please enter job description");
if (!myPrepend) exit();
var response = confirm("Warning: You are about to rename all images linked to the foremost Indesign Document - proceed? Keep in mind - it is not reversible!", false, "Rename Links Script");
if ( response == true )
{
WriteToFile("\r--------------------- Script started -- " + GetDate() + " ---------------------\n\n");
for ( k = 0; k < myAllLinks.length; k++ )
{
var myLinkName = myAllLinks.itemLink.name;
}
crearLabels();
var myPages = myDoc.pages;
// Pages
for ( p = 0; p < myPages.length; p++ )
{
var myPageNumber = pad000(myPages.name);
var myLinks = myPages.allGraphics;
var myASCII = 97;
for ( k = myLinks.length - 1; k >= 0; k-- )
{
var myLink = myLinks.itemLink;
if (myLink.extractLabel("relinked") != "yes") {
var myOldLinkName = myLink.name;
var myLinkUsage = LinkUsage( myLink );
var myExtension = myOldLinkName.substr(myOldLinkName.lastIndexOf( "." ));
if (LinkUsage(myLink) == 1)
{
var myNewLinkName = myPrepend + myPageNumber + String.fromCharCode( myASCII ) + myExtension;
var myOldImageHDfile = new File( myLink.filePath );
var myRenameResult = myOldImageHDfile.rename( myNewLinkName );
if (myRenameResult) {
myLink.insertLabel("relinked", "yes");
myLink.relink( myOldImageHDfile );
try {
myLink = myLink.update();
}
catch(err) {}
myASCII++;
WriteToFile(((myLinksCounter < 10) ? (" " + myLinksCounter) : myLinksCounter) + " - " + myOldImageHDfile.name + " --> " + myNewLinkName + "\n");
myLinksCounter++;
}
else {
if (new File(myOldImageHDfile.parent + "/" + myNewLinkName + myExtension).exists) {
WriteToFile("CAN'T RENAME LINK -- " + myOldImageHDfile.name + " to " + myNewLinkName + " because the file already exists\n");
}
else {
WriteToFile("CAN'T RENAME LINK -- " + myOldImageHDfile.name + "\n");
}
}
}
else {
if (!IsObjInArray(myLink, myMultipleLinks)) {
myMultipleLinks.push(myLink);
}
}
}
}
}
var myMasterSpreads = myDoc.masterSpreads;
// Master spreads
for ( m = 0; m < myMasterSpreads.length; m++ )
{
var myMastSpr = myMasterSpreads;
var myPageNumber = myMastSpr.name;
var myPrefix = myMastSpr.namePrefix;
var myLinks = myMastSpr.allGraphics;
var myASCII = 97;
for ( n = myLinks.length - 1; n >= 0; n-- )
{
var myLink = myLinks.itemLink;
if (myLink.extractLabel("relinked") != "yes") {
var myOldLinkName = myLink.name;
var myExtension = myOldLinkName.substr(myOldLinkName.lastIndexOf( "." ));
if (LinkUsage(myLink) == 1)
{
var myLinkLetter = (myLinks.length == 1) ? "" : String.fromCharCode( myASCII );
var myNewLinkName = myPrepend + '_master_' + myPrefix + myLinkLetter + myExtension;
var myOldImageHDfile = new File( myLink.filePath );
var myRenameResult = myOldImageHDfile.rename( myNewLinkName );
if (myRenameResult) {
myLink.insertLabel("relinked", "yes");
myLink.relink( myOldImageHDfile );
try {
myLink.update();
}
catch(err) {}
myASCII++;
WriteToFile(((myLinksCounter < 10) ? (" " + myLinksCounter) : myLinksCounter) + " - " + myOldImageHDfile.name + " --> " + myNewLinkName + "\n");
myLinksCounter++;
}
else {
if (new File(myOldImageHDfile.parent + "/" + myNewLinkName + myExtension).exists) {
WriteToFile("CAN'T RENAME LINK -- " + myOldImageHDfile.name + " to " + myNewLinkName + " because the file already exists\n");
}
else {
WriteToFile("CAN'T RENAME LINK -- " + myOldImageHDfile.name + "\n");
}
}
}
else
{
if (!IsObjInArray(myLink, myMultipleLinks)) {
myMultipleLinks.push(myLink);
}
}
}
}
}
// Multiple images
if (myMultipleLinks.length > 0) {
for ( a = myMultipleLinks.length - 1; a >= 0; a-- )
{
processMultiUsedLinks(myMultipleLinks);
}
}
WriteToFile("\r--------------------- Script finished -- " + GetDate() + " --...
Copy link to clipboard
Copied
I realize this is very late to the party. I've installed this script (the one in message 20 in this thread) and it does just what it is supposed to.
But - How do I edit it to stop it from renaming images on the master page? I'm not a scripter, just hunting for something that does what I need it to!
Copy link to clipboard
Copied
@winterm Thank you for supporting my country in times of trouble.
@goldaochacademy We can simply remove the part of the code which loops through master pages: lines 69–118
However, there’s a possibility that an image is placed 2+ times both on a page and on a master page.
Copy link to clipboard
Copied
It seems like we have some mistype in line 212?
Otherwise, great script, Kasyan!
Слава Україні!
Copy link to clipboard
Copied
I made many versions of the script. Below is the latest variant in case someone is interested.
Rename links. Version 1.3
const gScriptName = "Rename links";
const gScriptVersion = "1.3";
var o = {};
CreateDialog();
//===================================== FUNCTIONS ======================================
function CreateDialog() {
var w = new Window("dialog", gScriptName + " - " + gScriptVersion);
w.alignChildren = "fill";
// ISBN
w.p = w.add("panel", undefined, "");
w.p.orientation = "column";
w.p.alignChildren = "left";
w.p.g = w.p.add("group");
w.p.g.orientation = "row";
w.p.g.st = w.p.g.add("statictext", undefined, "ISBN:");
w.p.g.et = w.p.g.add("edittext");
w.p.g.et.characters = 13;
if (app.documents.length == 0) {
w.p.g.et.text = "1234567890123";
}
else {
try {
w.p.g.et.text = app.activeDocument.name.match(/^\d{13,}/)[0];
}
catch(err) {
w.p.g.et.text = "1234567890123";
}
}
w.p.g.et.onChange = function() {
if (w.p.g.et.text.match(/\d{13,}/) == null) alert("\"" + w.p.g.et.text + "\" is not correct ISBN number. It should consist of 13 digits.");
}
w.p.cb = w.p.add("checkbox", undefined, "Create log file on the desktop");
w.p.cb.value = true;
w.p1 = w.add("panel", undefined, "Process:"); // Settings
w.p1.orientation = "column";
w.p1.alignChildren = "left";
w.p1.rb = w.p1.add("radiobutton", undefined, "Active document");
w.p1.rb1 = w.p1.add("radiobutton", undefined, "All documents in the selected folder");
if (app.documents.length == 0) {
w.p1.rb1.value = true;
w.p1.rb.enabled = false;
}
else {
w.p1.rb.value = true;
}
// Buttons
w.g2 = w.add("group");
w.g2.orientation = "row";
w.g2.alignment = "center";
w.g2.ok = w.g2.add("button", undefined, "OK", {name:"ok" });
w.g2.cancel = w.g2.add("button", undefined, "Cancel", {name:"cancel"});
var showDialog = w.show();
if (showDialog == 1) {
if (w.p1.rb.value) {
o.scope = 1; // Active document
}
else {
o.scope = 2; // All documents in the selected folder
}
o.isbn = w.p.g.et.text;
o.log = (w.p.cb.value) ? true : false;
Main();
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function Main() {
if (o.scope == 1) { // Active document
ProcessActiveDoc();
}
else if (o.scope == 2) { // All documents in the selected folder
BatchProcessDocs();
}
alert("Finished.", gScriptName);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function ProcessActiveDoc() {
var firstPageName, lastPageName, newFile;
var doc = app.activeDocument;
// Main
o.mainFolderPath = doc.filePath.parent.fullName + "/" + o.isbn + "/";
o.mainFolder = new Folder(o.mainFolderPath);
if (!o.mainFolder.exists) o.mainFolder.create();
// Images
o.imagesFolderPath = o.mainFolderPath + o.isbn + "_images/";
o.imagesFolder = new Folder(o.imagesFolderPath);
if (!o.imagesFolder.exists) o.imagesFolder.create();
if (doc.name.match(/^\d{13,}/) == null) {
firstPageName = doc.pages[0].name;
lastPageName = doc.pages[doc.pages.length-1].name;
newFile = new File(o.mainFolderPath + o.isbn + "_int_" + ZeroPad(firstPageName, 3) + "-" + ZeroPad(lastPageName, 3) + ".indd");
if (newFile.exists) ErrorExit("File \"" + newFile.fsName + "\" already exists."); // Don't overwrite any docs
doc = doc.save(newFile);
ProcessDoc(doc);
}
else {
ProcessDoc(doc);
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function BatchProcessDocs() {
var file, doc, firstPageName, lastPageName, newFile;
var folder = Folder.selectDialog("Select a folder with InDesign documents.");
if (folder == null) exit();
var files = GetFiles(folder);
if (files.length == 0) ErrorExit("No InDesign documents were found in the selected folder.");
// Create "main" and "images" folders
o.mainFolderPath = folder.fullName + "/" + o.isbn + "/";
o.mainFolder = new Folder(o.mainFolderPath);
if (!o.mainFolder.exists) o.mainFolder.create();
o.imagesFolderPath = o.mainFolderPath + o.isbn + "_images/";
o.imagesFolder = new Folder(o.imagesFolderPath);
if (!o.imagesFolder.exists) o.imagesFolder.create();
for (var i = 0; i < files.length; i++) {
file = files;
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
doc = app.open(file);
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
firstPageName = doc.pages[0].name;
lastPageName = doc.pages[doc.pages.length-1].name;
newFile = new File(o.mainFolderPath + o.isbn + "_int_" + ZeroPad(firstPageName, 3) + "-" + ZeroPad(lastPageName, 3) + ".indd");
if (newFile.exists) ErrorExit("File \"" + newFile.fsName + "\" already exists."); // Don't overwrite any docs
if (doc.name.match(/^\d{13,}/) == null) doc = doc.save(newFile);
ProcessDoc(doc);
doc.save(doc.fullName);
doc.close(SaveOptions.YES);
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function ProcessDoc(doc) {
var link, image, ext, threeDigits, newLinkFile, actualPpi, effectivePpi, space, pageName;
if (o.log) WriteToFile("===========================================================================\r" + GetDate() + "\r---------------------------------------------------------------------------\rFile name: " + doc.name + "\r");
var usedNumbers = GetUsedNumbers(o.imagesFolder);
var counter = 1;
var links = doc.links;
var linksCounter = 0;
for (var i = links.length-1; i >= 0; i--) {
link = links;
if (link.name.substr(0,13) == o.isbn) continue;
image = link.parent;
ext = GetExtension(link.name);
while (GetArrayIndex(usedNumbers, counter) != -1) {
counter++;
}
usedNumbers.push(counter);
threeDigits = ZeroPad(counter, 3);
newLinkFile = new File(o.imagesFolderPath + o.isbn + "_a" + threeDigits + "." + ext);
if (o.log) {
try {
actualPpi = image.actualPpi[0] + "x" + image.actualPpi[1];
effectivePpi = image.effectivePpi[0] + "x" + image.effectivePpi[1];
}
catch(err) {
actualPpi = "N/A";
effectivePpi = "N/A";
}
try {
space = image.space;
}
catch(err) {
space = "N/A";
}
try {
pageName = link.parent.parentPage.name;
}
catch(err) {
pageName = "N/A";
}
linksCounter++;
WriteToFile(linksCounter + " - " + link.name + " ==> " + newLinkFile.name + "\r\tActual ppi: " + actualPpi + "\r\tEffective ppi: " + effectivePpi + "\r\tColor space: " + space+ "\r\tPage: " + pageName);
}
try {
link.copyLink(newLinkFile);
}
catch(err) {
$.writeln(err.message + ", line: " + err.line);
}
}
if (o.log && linksCounter == 0) WriteToFile("No links were renamed.");
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function GetUsedNumbers(theFolder) {
var file, baseName, match;
var numList = [];
var fileList = theFolder.getFiles();
for (var i = 0; i < fileList.length; i++) {
file = fileList;
baseName = GetFileName(file.name);
match = baseName.match(/\d{3,}$/);
if (match != null) numList.push(Number(match[0]));
}
return numList;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function ZeroPad(num, digit) {
var tmp = num.toString();
while (tmp.length < digit) {
tmp = "0" + tmp;
}
return tmp;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function GetArrayIndex(arr, val) {
for (var i = 0; i < arr.length; i++) {
if (arr == val) {
return i;
}
}
return -1;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function GetFileName(fileName) {
var str = "";
var res = fileName.lastIndexOf(".");
if (res == -1) {
str = fileName;
}
else {
str = fileName.substr(0, res);
}
return str;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function GetExtension(name) {
var idx = name.lastIndexOf( "." );
var ext = "";
if (idx > -1) {
ext = name.substr((idx + 1));
}
return ext;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function WriteToFile(text) {
var file = new File("~/Desktop/" + gScriptName + ".txt");
if (file.exists) {
file.open("e");
file.seek(0, 2);
}
else {
file.open("w");
}
file.write(text + "\r");
file.close();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function ErrorExit(error, icon) {
alert(error, gScriptName + " - " + gScriptVersion, icon);
exit();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function GetDate() {
var date = new Date();
if ((date.getYear() - 100) < 10) {
var year = "0" + new String((date.getYear() - 100));
}
else {
var year = new String((date.getYear() - 100));
}
var dateString = (date.getMonth() + 1) + "/" + date.getDate() + "/" + year + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
return dateString;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function GetFiles(theFolder) {
var files = [],
fileList = theFolder.getFiles(),
i, file;
for (i = 0; i < fileList.length; i++) {
file = fileList;
if (file instanceof Folder) {
files = files.concat(GetFiles(file));
}
else if (file instanceof File && file.name.match(/\.indd$/i)) {
files.push(file);
}
}
return files;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
After line #152 you can add the following line to skip images on master pages:
if (link.parent.parent.parent.constructor.name == "MasterSpread") continue;
It will work only if the images are placed directly on master pages (not nested, in tables, anchored, etc.)