I am trying to tell Finder to move files with AppleScript. But I don't know how to deal with duplication/replacement/skipping

StackOverflow https://stackoverflow.com//questions/12708195

Question

The script is as below:

tell application "Finder"

    set destinFolder to (choose folder)

    try
        move selection to destinFolder
    on error vMessage number -15267
        display dialog "" & vMessage
    end try

end tell

Basically,if a file with the same name has already existed in the destination folder, I want users have the choice to duplicate,replace or just skip that file and continue the next file movement using AppleScript. Just like the pic below.

enter image description here

Update: I know how to display a dialog like the one above,the problem is I don't know how to implement the "duplicate,replace or just skip" logic in AppleScript. And besides, I want to keep this line:

move selection to destinFolder

Because this line of code show a proper moving progress percentage in a single dialog, using repeat will lose that benefit.

Was it helpful?

Solution

You can try something like this:

set destinFolder to (choose folder)
set destItems to every paragraph of (do shell script "ls " & quoted form of (POSIX path of destinFolder))

tell application "Finder"
    set mySelection to selection
    repeat with anItem in mySelection
        set itemName to anItem's name
        if itemName is in destItems then
            display alert "An item named " & itemName & " already exists in this location. Do you want to replace it with the one you're moving?" buttons {"Skip", "Replace"} default button "Replace"
            if button returned of the result = "Replace" then move anItem to destinFolder with replacing
        else
            try
                move anItem to destinFolder
            end try
        end if
    end repeat
end tell

or this:

    set destinFolder to (choose folder)

tell application "Finder"
    set mySelection to selection
    repeat with anItem in mySelection

        try
            move anItem to destinFolder
        on error errMsg number errNum
            if errNum = -15267 then
                display alert "An item named " & itemName & " already exists in this location. Do you want to replace it with the one you're moving?" buttons {"Skip", "Replace"} default button "Replace"
                if button returned of the result = "Replace" then move anItem to destinFolder with replacing
            else
                tell me
                    activate
                    display alert errMsg & return & return & "Error number" & errNum buttons "Cancel"
                end tell
            end if
        end try

    end repeat
end tell

EDIT This script will not give you a choice for each item that exists in the destination folder

set destinFolder to (choose folder)

tell application "Finder"
    set mySelection to selection
    try
        move mySelection to destinFolder
    on error errMsg number errNum
        if errNum = -15267 then
            display alert "One or more items already exist in this location. Do you want to replace them with the ones you're moving?" buttons {"Skip", "Replace"} default button "Replace"
            if button returned of the result = "Replace" then move mySelection to destinFolder with replacing
        else
            tell me
                activate
                display alert errMsg & return & return & "Error number" & errNum buttons "Cancel"
            end tell
        end if
    end try

end tell

OTHER TIPS

Its not posible to display finder copy UI using applescript. You should implement your own UI.
your script will display this.
enter image description here

Take a look at Mac Finder native copy dialog article.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top