Skip to main content
Mike Witherell
Community Expert
Community Expert
August 15, 2022
Answered

Clean up InDesign project folders

  • August 15, 2022
  • 7 replies
  • 1685 views

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?

This topic has been closed for replies.
Correct answer rob day

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!


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)  
}

 

 

 

7 replies

Mike Witherell
Community Expert
Community Expert
August 17, 2022

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!

Mike Witherell
Mike Witherell
Community Expert
Community Expert
August 17, 2022

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");

 

 

Mike Witherell
rob day
Community Expert
Community Expert
August 17, 2022

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.

Mike Witherell
Community Expert
Community Expert
August 17, 2022

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!

Mike Witherell
Mike Witherell
Community Expert
Community Expert
August 17, 2022

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.)

Mike Witherell
rob day
Community Expert
Community Expert
August 17, 2022

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)  
}

 

Mike Witherell
Community Expert
Community Expert
August 17, 2022

Manan, thank you. Could you please describe what your script does and does not do? for example, does it delete any files?

Mike Witherell
Community Expert
August 17, 2022

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

-Manan
rob day
Community Expert
Community Expert
August 17, 2022

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();
};   

 

 

 

 

 

 

 

Mike Witherell
Community Expert
Community Expert
August 16, 2022

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?

Mike Witherell
Community Expert
August 16, 2022

If script is something that you are after. The following InDesign script should work. Prerequisties for the script to run are

  • The document should be open
  • The script will search for link files in the folder where the first link it finds in the document is present. Ideally this means that all the placed files should be present in the same folder

 

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

-Manan
Luke Jennings3
Community Expert
Community Expert
August 16, 2022

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-active-in-an-indesign-document/m-p/9073554

 

TᴀW
Legend
August 16, 2022

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...

Visit www.id-extras.com for powerful InDesign scripts that save hours of work — automation, batch tools, and workflow boosters for serious designers.
Mike Witherell
Community Expert
Community Expert
August 16, 2022

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.

Mike Witherell
Scott Citron
Legend
August 15, 2022

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?

Mike Witherell
Community Expert
Community Expert
August 15, 2022

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!

Mike Witherell
Community Expert
August 16, 2022

Hi @Mike Witherell 

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

-Manan