Copy link to clipboard
Copied
Hey gang,
Is there a script or method that can look in a Links folder and see which files are NOT being used in the InDesign layout? Having identified them, those extra graphics could be moved or deleted. That is what I am trying to accomplish. Any good ideas?
Here‘s a version with a dialog that let‘s you name the new folder. It’s working for me, but if you get an error post a capture:
cleanLinks()
/**
* Moves the document links to a named folder
*/
function cleanLinks(){
var fn;
var d = app.dialogs.add({name:"Enter a New Folder Name", canCancel:true});
with(d.dialogColumns.add()){
fn = textEditboxes.add({editContents:" ", minWidth:250});
}
if(d.show() == true){
fn = fn.editContents
d.destroy();
}
...
Copy link to clipboard
Copied
Hi Mike:
I tend to put unused files on the pasteboard. From the Links panel you can see these files indicated by PB in the far right column. Easy to delete or move from there. Does this help?
Copy link to clipboard
Copied
Hi Scott,
This is more a matter of files that were planned to be used, got put in the Links folder, but then either never got used, or were later deleted from the layout. Now the Links subfolder has extra stuff. I want to hunt for all files not being used in the InDesign file.
I suppose I could check links, package, then get rid of old folders, but I thought someone might have a clever script or method.
I hope you are well!
Copy link to clipboard
Copied
How about package the document. This will pull all the used files into the new Links folder, then you can delete the original folder.
-Manan
Copy link to clipboard
Copied
How about: Select all images in the Links panel, embed them, delete all images in the Links folder, then unembed back into the Links panel...
Copy link to clipboard
Copied
Clever!
I kinda wish Bridge had the answer. In Bridge, you can right-click the INDD file and have it show all links.
I sorta want the opposite of that, too.
Copy link to clipboard
Copied
Actually, come to think of it: Bridge can show all Links. Then I could set them to 5 Stars rating. Then sort by Rating and I would be able to tease out which files were not starred.
I wonder if that is Bridge scriptable?
Copy link to clipboard
Copied
If script is something that you are after. The following InDesign script should work. Prerequisties for the script to run are
var linksNames = app.documents[0].links.everyItem().name
var linksFldr = File(app.documents[0].links[0].filePath).parent
var flCol = linksFldr.getFiles()
for(var i = 0; i < flCol.length; i++){
var fn = File(flCol[i]).displayName
var rg = new RegExp("\\b" + fn + "\\b")
if(!rg.test(linksNames))
File(flCol[i]).remove()
}
-Manan
Copy link to clipboard
Copied
In addition to the good information above, some links may have their own links (an Illustrator file for example) these "linked links" are only needed if the placed file needs to be edited, but in my experience they may be saved in the InDesign links folder and would not be collected with the packaged files. Here is an old link to a related discussion: https://community.adobe.com/t5/indesign-discussions/is-there-a-system-for-sorting-graphics-that-are-...
Copy link to clipboard
Copied
Manan, thank you. Could you please describe what your script does and does not do? for example, does it delete any files?
Copy link to clipboard
Copied
Hi @Mike Witherell,
The script checks in the folder where the asset of the first link it finds in the document resides. This is set as the folder that is to be cleaned up. Then it searches every filename in this folder in the filename collection of the links placed in the indesign file. If the filname is found in the collection the file is skipped, if the name is not found that means the file is not placed in InDesign and henc it is deleted.
So yes the script does delete files. Give it a try with a sample document.
-Manan
Copy link to clipboard
Copied
Hi Manan and Mike, Another option might be to use the link’s copyTo() method, which is the equivalent of the Link Panel’s Utilities>Copy Link(s) to... menu item. In this case the document links could be in any folder, and files that are not linked don’t get trashed. You could also do this manually via the UI, but in that case the links get copied—the script moves the use files without trashing the unused files:
var doc = app.activeDocument;
var dp = doc.filePath
var dl = doc.links.everyItem().getElements()
var nlf = new Folder(dp + "/UsedLinks");
nlf.create();
var lp
for (var i = 0; i < dl.length; i++){
lp = dl[i].filePath
dl[i].copyLink(nlf);
File(lp).remove();
};
Copy link to clipboard
Copied
Nice idea Rob. I see I missed most of the action and the thread progressed well. As far as I know the undo actions are for the changes we make on the document so any external changes done would be permanent, so adding an undo for file delete would not be possible.
-Manan
Copy link to clipboard
Copied
Dear Manan and Rob Day,
Thank you both for posting these scripts. Both work. Manan's delete files instantly. Rob's moves them to a new folder and adjusts the Links and does not delete files. Both work well.
Question 1: Could this be made Undo-able? (as in that code method the Theunis de Jong posted about a couple years ago?)
Question 2: Could these scripts be made to announce an onscreen alert so as to indicate that they have completed something? (They both do what they do without any fanfare or user alert interaction. I had to look in the subfolders to see that something had been changed.)
Copy link to clipboard
Copied
There is the doScript method that lets you undo a script, but I don’t think it will let you undo Finder/Explorer operations—Manan might know more about that. An alert is easy:
app.doScript(cleanLinks,ScriptLanguage.JAVASCRIPT,[],UndoModes.ENTIRE_SCRIPT,'cleanLinks');
/**
* Moves the document links to a folder named UsedLinks
*/
function cleanLinks(){
var fn = "UsedLinks"
var doc = app.activeDocument;
var dp = doc.filePath
var dl = doc.links.everyItem().getElements()
var nlf = new Folder(dp + "/" + fn);
nlf.create();
var lp
for (var i = 0; i < dl.length; i++){
lp = dl[i].filePath
dl[i].copyLink(nlf);
File(lp).remove();
};
alert("Links Have Been Moved to " + fn)
}
Copy link to clipboard
Copied
Thanks Rob!
Yes, I doubt there is an undo possible here.
Just playing around on my own, I added an alert, but I am probably over-elaborating the code:
var doc = app.activeDocument;
var dp = doc.filePath
var dl = doc.links.everyItem().getElements()
var nlf = new Folder(dp + "/UsedLinks");
nlf.create();
var lp
for (var i = 0; i < dl.length; i++){
lp = dl[i].filePath
dl[i].copyLink(nlf);
File(lp).remove();
};
var doc = app.activeDocument;
alert ("Links gathered into the new UsedLinks subfolder");
Copy link to clipboard
Copied
I haven’t test mine very much—not sure if this is a problem, but if you run the script multiple times on the same document, the links are going to get renamed and appended with _1. It would be possible to add a dialog that asks you to name the new folder.
Copy link to clipboard
Copied
True, and in fact I couldn't get your latest version of script to work. I don't have enough experience to know why, but the one like it that I hacked does work. Thanks for helping me experiment and learn a bit!
Copy link to clipboard
Copied
Here‘s a version with a dialog that let‘s you name the new folder. It’s working for me, but if you get an error post a capture:
cleanLinks()
/**
* Moves the document links to a named folder
*/
function cleanLinks(){
var fn;
var d = app.dialogs.add({name:"Enter a New Folder Name", canCancel:true});
with(d.dialogColumns.add()){
fn = textEditboxes.add({editContents:" ", minWidth:250});
}
if(d.show() == true){
fn = fn.editContents
d.destroy();
}
var doc = app.activeDocument;
var dp = doc.filePath
var dl = doc.links.everyItem().getElements()
var nlf = new Folder(dp + "/" + fn);
nlf.create();
var lp
for (var i = 0; i < dl.length; i++){
lp = dl[i].filePath
dl[i].copyLink(nlf);
File(lp).remove();
};
alert("Links Have Been Moved to " + fn)
}
Copy link to clipboard
Copied
Rob, thank you. I very much like your last effort here. It makes me feel like I am in control of it, and it sums up at the end with a message. Very elegant, and just what I need to clean up after messy projects! Thanks again!