Hi Friends, I am run the below script for renaming the links it works. But, the problem was when renumbering the number missing link also counted. For e.g. in My document 5 linked images, 3 missing images, total count. In linked images output file name was 001, 004, 006, 007, 008. My requirement is missing link do not count for renaming the file name. Thanks in advance
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 + "_" + threeDigits + "T" + "." + 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;
}
//-----------------------------------------------------------------------------------------------------------------------------------
... View more