Tuesday, June 18, 2019

#1 2019-04-12 12:16:42 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 149

Cleaning duplicate files

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

NOTE: Tested  on Mojave 10.14. On other systems you must replace some UI elements with appropriate UI elements (for example, on Captain checkbox 1 of group 1 of splitter group 1 is other UI element).

You can comment out or remove the lines with the confirmation dialog for deleting a duplicate, but it is better not to do this. This will give you the ability to control what you delete. Moreover, the script is "cooked" quickly, for personal use, and therefore bugs are possible.

In any case, I’m interested to know how the script worked for you and how many duplicates it found.
Feel free to specify bugs or undersights if you find them.

Applescript:



-- filename extension's list for files, which I want to protect from removing
set Protected_List to {"h"}
-- this tool is needed, as poor Applscript can't send real click events to windows
set mouseToolsPath to "/Users/123/Applications/MouseTools"

-- Opening "New Smart Folder window" and setting view options
tell application "System Events" to tell process "Finder" to click menu item "New Smart Folder" of menu 1 of menu bar item "File" of menu bar 1
tell application "Finder" to tell the front Finder window to set current view to list view
tell application "System Events" to tell process "Finder"
   click menu bar item "View" of menu bar 1
   tell menu item "Show View Options" of menu of menu bar item "View" of menu bar 1 to if (exists) then click
   tell window "New Smart Folder"
       tell checkbox "Browse in list view" to if (exists) and value is 0 then click
       click pop up button 1
       tell menu item "None" of menu 1 of pop up button 1 to if (exists) then click
       click pop up button 2
       tell menu item "Size" of menu 1 of pop up button 2 to if (exists) then click
       click UI element 8
       
       -- Creating query for files with size ≥ 1 Kbyte
       click radio button "This Mac" of radio group 1 of group 1 of splitter group 1
       click checkbox 1 of group 1 of splitter group 1
       tell splitter group 1
           tell pop up button 1 of group 2 to if (exists) then click
           click menu item "File Size" of menu 1 of pop up button 1 of group 2
           tell pop up button 2 of group 2 to if (exists) then click
           click menu item "is greater than" of menu 1 of pop up button 2 of group 2
           tell text field 1 of group 2 to if (exists) then click
           keystroke "1000"
           tell scroll area 1 of splitter group 1 to if (exists) then click
           do shell script mouseToolsPath & " -x 600 -y 400 -leftClick"
       end tell
   end tell
   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 of application "Finder")
tell application "Finder" to close windows

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"

Last edited by KniazidisR (2019-04-13 07:40:18 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#2 2019-04-13 07:35:02 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 149

Re: Cleaning duplicate files

Made improvements to the script above. Now you can choose which of the 2 compared files to delete, and which to leave. The speed, flexibility and practicality of the script is much better than its professional counterparts...

Last edited by KniazidisR (2019-04-13 07:48:03 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)