Search code examples
applescriptautomator

Adding suffix to duplicate file in Apple Script


I am attempting to write a script to allow me to keep my Downloads folder clean, this is great for uni/work as files get moved to their correct folders to allow linking in apps etc.

I have a script working for if a file does not exist, it will move the file to the selected folder (prompts user for folder) and I have added an option to delete the new file if it exists if the user wants, however I want to add an option to keep both files, with the new file being renamed with a "_2" added on the end of the name for the second "_3" for the third and so on.

As far as I have it, I am prompting for a manual name change, along with that not working, I can see it adding inefficiency. This is as far as I have it:

(* Version 1.2b
Move to Bin prompt added
Fixed activate Finder
add ability to keep a duplicate *)

on run {input, parameters}
    tell application "Finder"
        set fileToMove to item 1 of input
        set fileName to name of fileToMove
        set fileExtension to name extension of fileToMove ¬
            --gets extension incase file needs to be renamed
        activate
        set chosenFolder to POSIX file choose folder with prompt "Select a folder to move " & fileName & " "
        log chosenFolder
        
        try
            move fileToMove to folder chosenFolder
            -- Make sound play only on success
        on error
            -- Display notification with title "File Not Moved" subtitle "File with that name already exists at that location"
            
            display alert "Duplicate File Found" message "Would you like to move " & fileToMove & " to the Bin or Keep & Rename?" buttons {"Move Duplicate to Bin", "Keep & Rename", "Cancel"} default button "Keep Both" cancel button "Cancel"
            set buttonReturned to button returned of result
            
            if buttonReturned is "Keep Both" then
                display dialog "Set New File Name for: " & fileName & "  " default answer fileName with icon note
                set newName to result
                set the name of fileToMove to newName
                set the extension of fileToMove to fileExtension
                move fileToMove to folder chosenFolder
                do shell script "afplay /System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/Volume Mount.aif'"
            end if
            
            if buttonReturned is "Move Duplicate to Bin" then
                -- Move the file to the trash
                move fileToMove to trash
            end if
        end try
    end tell
end run

Any other suggestions, refinements welcome


Solution

  • Your script doesn't work anyway because the button names don't match and a single quote character is missing in the do shell script line.

    Never mind, you need a handler which checks the file name and adds an index.
    This is a bit cumbersome because the case no extension must be covered and if there is an extension you have to strip the extension from the file name.

    POSIX file choose folder is not needed.
    choose folder is sufficient and you can delete the folder keywords.

    The only way to move and rename a file simultaneously is with mv of the shell.

    (* Version 1.2b
    Move to Bin prompt added
    Fixed activate Finder
    add ability to keep a duplicate *)
    
    on run {input, parameters}
        tell application "Finder"
            set fileToMove to item 1 of input
            set fileName to name of fileToMove
            set fileExtension to name extension of fileToMove ¬
                --gets extension incase file needs to be renamed
            activate
            set chosenFolder to choose folder with prompt "Select a folder to move " & fileName & " "
            log POSIX path of chosenFolder
            
            try
                move fileToMove to chosenFolder
                -- Make sound play only on success
            on error
                -- Display notification with title "File Not Moved" subtitle "File with that name already exists at that location"
                
                display alert "Duplicate File Found" message "Would you like to move " & fileToMove & " to the Bin or Keep & Rename?" buttons {"Move Duplicate to Bin", "Keep & Rename", "Cancel"} default button "Keep & Rename"
                set buttonReturned to button returned of result
                
                if buttonReturned is "Keep & Rename" then
                    set newPath to my checkFileName(chosenFolder as text, fileName, fileExtension)
                    do shell script "mv " & quoted form of POSIX path of fileToMove & space & quoted form of POSIX path of newPath
                    do shell script "afplay '/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/Volume Mount.aif'"
                end if
                
                if buttonReturned is "Move Duplicate to Bin" then
                    -- Move the file to the trash
                    move fileToMove to trash
                end if
            end try
        end tell
    end run
    
    -- fDir: HFS path destination folder
    -- fName: full file name
    -- fExt: name extension
    
    on checkFileName(fDir, fName, fExt)
        if fExt is "" then
            set fullExt to fExt
            set baseName to fName
        else
            set fullExt to "." & fExt
            set endIndex to offset of fullExt in fName
            set baseName to text 1 thru (endIndex - 1) of fName
        end if
        set _index to 0
        repeat
            set _index to _index + 1
            set newPath to (fDir & baseName & "_" & (_index as text) & fullExt)
            try
                newPath as alias
                -- file exists – repeat again
            on error
                -- file does does not exist – return new path
                return newPath
                
            end try
        end repeat
    end checkFileName