Content is user-generated and unverified.

macOS Shortcut: Duplicate File Cleaner

Step-by-Step Setup Instructions

1. Create New Shortcut

  • Open the Shortcuts app on your Mac
  • Click the "+" button to create a new shortcut
  • Name it "Clean Duplicate Files"

2. Add Actions (in this order):

Action 1: Get Selected Items in Finder

  • Search for and add: "Get Selected Items in Finder"
  • This gets the files you've selected in Finder

Action 2: Get Details of Files

  • Search for and add: "Get Details of Files"
  • Set the detail to get: "Name"
  • This extracts just the filenames

Action 3: Run JavaScript on Mac

  • Search for and add: "Run JavaScript on Mac"
  • Paste the JavaScript code below into the script field:
javascript
function run(input, parameters) {
    // Get the filenames from the input
    const filenames = input[0];
    
    // Regex pattern to match duplicate file suffixes (-1, -2, -3, etc.)
    const duplicatePattern = /^(.+?)(-\d+)(\.[^.]+)?$/;
    
    // Function to extract base name from filename
    function getBaseName(filename) {
        const match = filename.match(duplicatePattern);
        if (match) {
            const baseName = match[1];
            const extension = match[3] || '';
            return baseName + extension;
        } else {
            return filename;
        }
    }
    
    // Function to check if a file is a duplicate
    function isDuplicate(filename) {
        return duplicatePattern.test(filename);
    }
    
    // Function to group files by their base names
    function groupFilesByBaseName(filenames) {
        const groups = {};
        
        filenames.forEach(filename => {
            const baseName = getBaseName(filename);
            
            if (!groups[baseName]) {
                groups[baseName] = {
                    original: null,
                    duplicates: []
                };
            }
            
            if (isDuplicate(filename)) {
                groups[baseName].duplicates.push(filename);
            } else {
                groups[baseName].original = filename;
            }
        });
        
        return groups;
    }
    
    // Function to identify files to delete
    function getFilesToDelete(filenames) {
        const groups = groupFilesByBaseName(filenames);
        const filesToDelete = [];
        
        Object.keys(groups).forEach(baseName => {
            const group = groups[baseName];
            
            if (group.original && group.duplicates.length > 0) {
                filesToDelete.push(...group.duplicates);
            } else if (group.duplicates.length > 1) {
                filesToDelete.push(...group.duplicates.slice(1));
            }
        });
        
        return filesToDelete;
    }
    
    // Get files to delete
    const filesToDelete = getFilesToDelete(filenames);
    
    // Return the result
    return filesToDelete;
}

Action 4: Filter Files

  • Search for and add: "Filter Files"
  • Set the condition: "Name" "is" "[Magic Variable from JavaScript]"
  • This will filter the original selected files to only those marked for deletion

Action 5: Show Notification (Optional Preview)

  • Search for and add: "Show Notification"
  • Set title: "Files to Delete"
  • Set body: Use magic variable from Filter Files to show count
  • This gives you a preview before deletion

Action 6: Choose from Menu (Confirmation)

  • Search for and add: "Choose from Menu"
  • Set prompt: "Delete these duplicate files?"
  • Add two options:
    • "Yes, Delete"
    • "Cancel"

Action 7: Move to Trash (under "Yes, Delete")

  • Search for and add: "Move to Trash"
  • Use the filtered files from Action 4 as input

Action 8: Show Notification (Success)

  • Search for and add: "Show Notification"
  • Set title: "Cleanup Complete"
  • Set body: "Duplicate files moved to trash"

3. Set Shortcut Options

  • Click the settings icon in the top right
  • Enable "Use with Finder"
  • Optionally add a keyboard shortcut

How to Use

  1. Select files in Finder that include duplicates (like document.pdf, document-1.pdf, etc.)
  2. Right-click and choose ServicesClean Duplicate Files
    • OR use your keyboard shortcut
    • OR run from Shortcuts app
  3. Review the preview notification showing what will be deleted
  4. Choose "Yes, Delete" to confirm or "Cancel" to abort
  5. Duplicate files moved to trash - originals remain!

What Gets Deleted

  • document-1.pdf (if document.pdf exists)
  • photo-2.jpg (if photo.jpg exists)
  • report-3.docx (keeps first duplicate if no original)
  • document.pdf (original files are kept)

Safety Features

  • Preview before deletion
  • Confirmation dialog
  • Files moved to Trash (not permanently deleted)
  • Only affects files with specific duplicate patterns
Content is user-generated and unverified.
    macOS Shortcut: Duplicate File Cleaner Setup Guide | Claude