Cleaning duplicate files

The following script finds and cleans all my Mac from duplicates in few seconds.

I have created Smart Folder named LargeFiles.savedSearch, which I have saved in folder Saved Searches of folder Library of my home directory. This smart folder is created with “Search Mac” and using criteria “File Size greater than 1000 Kbyte”.


set Protected_List to {"h"} -- protected files list (extensions)
set theSmartFolder to alias "Untitled:Users:123:Library:Saved Searches:LargeFiles.savedSearch"

tell application "Finder"
	activate
	open theSmartFolder
	tell application "System Events" to tell process "Finder"
		click menu bar item "Edit" of menu bar 1
		delay 0.5
		tell menu item "Select All" of menu 1 of menu bar item "Edit" of menu bar 1 to if (exists) then click
	end tell
	-- Getting Finder paths list for files, matching to criteria above
	set theSelection to selection
	close windows
end tell

set duplicate_Files to 0
set saved_Space to 0
set all_Files to count theSelection

tell application "Finder"
	repeat with n from 1 to all_Files - 1
		if exists (item n of theSelection) then
			set item_1 to item n of theSelection
			-- skip comparing, if item_1 is protected by filename extension's list above
			if not (name extension of item_1 is in Protected_List) then
				set type_1 to file type of item_1
				-- skip comparing, if item_1 is application
				if type_1 ≠ "APPL" then
					set posix_Path_1 to quoted form of (POSIX path of (item_1 as alias))
					set size_1 to size of item_1
					
					repeat with m from n + 1 to all_Files
						set item_2 to item m of theSelection
						set size_2 to size of item_2
						-- skip byte by byte comparing if file's sizes isn't same 
						if size_1 = size_2 then
							set type_2 to file type of item_2
							-- byte by byte comparing ONLY if is same file's types 
							if type_1 = type_2 then
								set posix_Path_2 to quoted form of (POSIX path of (item_2 as alias))
								set shell_Command_String to "cmp -s" & posix_Path_1 & space & posix_Path_2
								set exitStatus to 0 -- setting initial status of shell operation
								try
									set exitStatus to do shell script shell_Command_String
									return exitStatus
								end try
								if exitStatus = 0 then -- if exitStatus remains= 0, that indicates "files is same"					
									display dialog "Founded duplicate files!!!" & return & return & posix_Path_1 & return & return & posix_Path_2 & return & return & "REMOVE DUPLICATE FILE ?" buttons {"SKIP", "REMOVE 1st FILE", "REMOVE 2nd FILE"}
									set button_Returned to button returned of result
									
									if button_Returned is "REMOVE 2nd FILE" then
										set duplicate_Files to duplicate_Files + 1
										set locked of item_2 to false
										move item_2 to trash -- removing duplicate file
										set saved_Space to saved_Space + size_2
									end if
									if button_Returned is "REMOVE 1st FILE" then
										set duplicate_Files to duplicate_Files + 1
										set locked of item_1 to false
										move item_1 to trash -- removing duplicate file
										set saved_Space to saved_Space + size_1
										exit repeat
									end if
								end if
							end if
						else
							exit repeat
						end if
					end repeat
				end if
			end if
		end if
	end repeat
end tell

tell me to activate
-- Creating report message
display alert "Total files maching to criteria: " & (all_Files as string) & return & return & "Removed duplicate files: " & (duplicate_Files as string) & return & return & "Cleaned Disk Space: " & (saved_Space / 1024 as string) & " Kbytes"

The speed, flexibility and practicality of the script is much better than its professional counterparts…