Copy elements through Finder and Applescript: how to get "element exists - replace?" dialogue in Finder?

In my application, I like the OSX Finder to copy a file or folder.

(Note: I have good reasons for using Finder instead of using cmds, NSWorkspace, or other means, so there is no need for suggestions in this direction.)

I am currently relying on Applescript to request the Finder to perform a copy operation. Here is a sample script for testing:

tell application "Finder"
    try
        copy file POSIX file "/etc/bashrc" to folder POSIX file "/private/tmp"
        -- alternative with same behavior:
        --duplicate POSIX file "/etc/bashrc" to POSIX file "/private/tmp"
    on error errmesg number errn
        display dialog "Error thrown: " & errmesg
    end try
end tell

Now the problem is this: If the destination element already exists, the script throws an error and cancels the copy operation.

, , Finder " " , Finder , :

Finder "item already exists" warning

, : script script Debugger, !

, , Finder , . ? ?

+5
3

, : AppleScript API AppleEvents.

kAEAlwaysInteract | kAECanSwitchLayer AESend SendMode. Finder ( Finder ).

-, AppleScript, , , .

+1

, Finder AppleScript - , script . , Finders, System Events move , , , Finder replacing true, , ask (. MacScripter ).

, Script -, , . , script script. , - - Finder move - Lower Finder, .

- , Paul R, AppleScript-ObjectiveC ( , ).

0

This may be belated, but if you delete the file before copying it, it will be the same as replacing:

tell application "System Events"
    if exists file ("NewFilePath") then
        delete file ("NewFilePath")
            -- NewFilePath Is where the file will end up or already is
    end if
end tell
tell application "Finder"
    try
        copy file POSIX file "FilePath" to folder POSIX file "FolderPath"
        -- FilePath is where it is now
        -- FolderPath is the path of the folder you want the file in
    on error errmesg number errn
        display dialog "Error thrown: " & errmesg
    end try
end tell

and since the choice between replacing or stopping is not possible, there is no need to display such a window

-1
source

All Articles