I'm trying to make a script in JavaScript, but I'm not very experienced in this field, so I'm here to seek help.
Basically where I work I have a ton of .indd files with a lot of different images with different names and numbers every day. When someone want to know in which document is a certain image I need to search manually for every image.
I'm trying to put together a working script, but without any luck. What I'm looking for is a script that let you select a folder, let you type the name (or part of the name) of the filename to search for, search through all the documents in the folder without opening all the files at once (for performance reason as the files are a lot), and give you an alert if the file with that name is found.
I have merged together various scripts found on various sites, but it doesn't work for some unknown reason.
The code is the following
app.scriptPreferences.enableRedraw = false;
var Directory = Folder.selectDialog ("Seleziona una cartella");
if (!Directory) {
exit();
}
var Documents = Directory.getFiles();
var Dialwindow = new Window("dialog");
Dialwindow.text = "Search";
Dialwindow.orientation = "column";
var Textbar = Dialwindow.add('edittext');
Textbar.preferredSize.width = 150;
Textbar.active = true
var Buttons = Dialwindow.add("group", undefined);
Buttons.orientation = "row";
var Search = Buttons.add("button", undefined, undefined);
Search.text = "Search";
Search.onClick = function() {
Dialwindow.close();
}
var Exit = Buttons.add("button", undefined, undefined);
Exit.text = "Exit";
Exit.onClick = function() {
// So far, so good, now I'm looking for a code that stops the script completely, as at the moment clicking on Search or Exit gives the same result
Dialwindow.close();
}
Dialwindow.show();
for (var i = 0; i < Documents.length; i++) {
// Now I think it begins the part that gives me problem
app.open(Documents[i]);
// As far as I understand this opens every indd files one by one
Collegamenti = app.activeDocument.links;
Result = Textbar.text;
// This script was found on a site. Normally it works, but merging it doesn't
var Array = new Array();
for(var i=0; i < Collegamenti.length; i++) {
var Nome = Collegamenti[i].name;
Nome = Nome.toLowerCase();
if(Nome.indexOf(Result.toLowerCase()) != -1)Array.push(Collegamenti[i]);
}
// I'm not sure if this is written correctly
if (Array.length= 1) {
alert("You can find the image " + Result + " in this file " + app.activeDocument.name.replace(/.[^.]+$/,''));
app.activeDocument.close(SaveOptions.NO);
}
else {
app.activeDocument.close(SaveOptions.NO);
}
}
So this script should let you select a folder, create a dialog window with a text bar, search through the documents in the folder, if an image with that name is found trigger an alert, else close the document.
I thank anyone who can explain what is wrong or can suggest improvements/corrections. If something is not clear due to my bad English, please ask, I'll try to be more clear.
EDIT
Thanks Yuri for your quick answer, I have learned a few new things from your code (like the "while() {}" and the ".shift" parts that I didn't know about). Unfortunately, where I work, it's not very practical to scroll all the txt file to find what I was searching (as there are a lot of indd files), even if your script saves me from wasting a lot of time. I apologize but I have probably not been very clear, as my intent was to understand what is wrong with the original code, so I could learn instead of having some ready-to-go code. The parts I don't understand are:
for (var i = 0; i < Documents.length; i++) {
app.open(Documents[i]);
This opens all the documents, one by one which is essentially correct, but I can't use app.activeDocument.links; because there is no active document. There is a way to use a variable instead of app.activeDocument.links with the code above? I've tried to use the "while (Documents.length) {var Shift = Documents.shift();var Doc = app.open(Shift);" but for the same reason I don't know how to continue with the next code. Another thing is this:
var Array = new Array();
var Links = app.activeDocument.links
for(var i=0; i < Links.length; i++) {
var Name = Links[i].name;
Name = Name.toLowerCase();
if(Name.indexOf(Textbar.text.toLowerCase()) != -1)Array.push(Links[i]);
}
It works with all the documents opened, but I don't know how use it with the "app.open(Documents[i]);" as there is no active document opened. And there is a way to stop the script with the Button.onClick = function() {}? I've tried exit(); but it doesn't work.
EDIT2
Thanks to Yuri, this is the final code:
app.scriptPreferences.enableRedraw = false;
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
var folder = Folder.selectDialog ("Select a folder");
if (!folder) exit();
var files = folder.getFiles('*.indd');
if (files.length == 0) { alert("The folder doesn\'t contain any indd files"); exit() }
var name = prompt("Write a file name:");
if (name == null) { alert("Empty textbar"); exit() }
name = name.toLowerCase();
var total = [];
while (files.length) {
var doc = app.open(files.shift());
var links = doc.links.everyItem().getElements();
if (links.length == 0) { alert("No links are present in document\r''" + doc.name.replace(/\.[^\.]+$/, '') + "''");}
var find = [];
while (links.length) {
var link = links.shift();
if (link.name.toLowerCase().indexOf(name) > -1) {find.push(link); total.push(link);}}
if (find.length > 0) { alert("The link ''" + name + "'' is\ron ''" + doc.name.replace(/\.[^\.]+$/, '') + "''");}
doc.close();}
alert("Done\r" + total.length + " links found");
Since your code contains too much errors it's hard to explain every single one of them. I've tried fix the errors and keep your algorithm more or less intact. Here is the working code:
var directory = Folder.selectDialog("Seleziona una cartella");
if (!directory) exit();
// dialog window -------------------------------------------------------
var dialwindow = new Window("dialog", "Search");
var textbar = dialwindow.add('edittext');
textbar.preferredSize.width = 150;
textbar.active = true
var buttons = dialwindow.add("group", undefined);
buttons.orientation = "row";
var search = buttons.add("button", undefined, "Search");
var cancel = buttons.add("button", undefined, "Exit");
dialwindow.defaultElement = search;
dialwindow.cancelElement = cancel;
if (dialwindow.show() != 1) exit();
// main code -----------------------------------------------------------
var result = textbar.text;
var documents = directory.getFiles('*.indd');
for (var i = 0; i < documents.length; i++) {
var doc = app.open(documents[i]);
var links = doc.links;
var arr = [];
for (var j = 0; j < links.length; j++) {
var link_name = links[j].name.toLowerCase();
if (link_name.indexOf(result.toLowerCase()) > -1) {
arr.push(links[j]);
break;
}
}
if (arr.length > 0) {
alert("You can find the image " + result + " in this file " + doc.name.replace(/\.[^\.]+$/, ''));
}
doc.close(SaveOptions.NO);
}