Remove Duplicate Files

Summary: Keeps only the latest record among each group of identical ones.
Requires: EagleFiler
Install Location: ~/Library/Scripts/Applications/EagleFiler/
Last Modified: 2019-10-02

Description

This script scans the selected records for multiple files with the same contents. For each group of identical files, it keeps the newest one and moves the older ones to the trash. In order for this to work, the checksums of all the selected files must be up to date. (You can also prevent duplicate files from being added to the library in the first place by unchecking Allow duplicate files in library in the preferences.)

Installation Instructions · Download in Compiled Format · Download in Text Format

Script

tell application "EagleFiler"
    
set _checksums to {}
    
set _recordsSeen to {}
    
set _records to selected records of browser window 1
    
set _trash to trash of document of browser window 1
    
repeat with _record in _records
        
set _checksum to _record's checksum
        
set _matches to my findMatch(_checksum, _checksums, _recordsSeen)
        
if _matches is {} then
            
set _checksums to {_checksum} & _checksums
            
set _recordsSeen to {_record} & _recordsSeen
        
else
            
set _otherRecord to item 1 of _matches
            
if _otherRecord's modification date > _record's modification date then
                
set _record's container to _trash
            
else
                
set _otherRecord's container to _trash
                
set _checksums to {_checksum} & _checksums
                
set _recordsSeen to {_record} & _recordsSeen
            
end if
        
end if
    
end repeat
end tell

on findMatch(_checksum, _checksums, _recordsSeen)
    
tell application "EagleFiler"
        
if _checksum is "" then return {}
        
if _checksums contains _checksum then
            
repeat with i from 1 to length of _checksums
                
if item i of _checksums is _checksum then
                    
return item i of _recordsSeen
                
end if
            
end repeat
        
end if
        
return {}
    
end tell
end findMatch