Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy items via Finder and Applescript: how to get "item exists - replace?" dialog in Finder?

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

(Note: I have good reasons for using the Finder instead of using shell cmds, NSWorkspace or other means, so there's no need for suggestions in that direction.)

I am currently relying on Applescript for asking the Finder to perform the copy operation. Here's 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 item already exists, the script throws an error and cancels the copy operation.

However, I would rather have the Finder display the "item exists" dialog that it shows when doing such a copy operation in the Finder interactively, like this one:

Finder "item already exists" warning

Now, here's the thing: If I run this script from the 3rd party app Script Debugger, then this dialog appears!

So, obviously there is a way to get the Finder to display the dialog instead of throwing an exception. But how? Who knows the secret to this?

like image 664
Thomas Tempelmann Avatar asked Nov 24 '22 23:11

Thomas Tempelmann


1 Answers

Well, for my own needs I've found a solution: Instead of using an AppleScript, I need to use the AppleEvents API directly.

There, I can supply the values kAEAlwaysInteract | kAECanSwitchLayer to AESend's SendMode parameter. This makes the dialogs appear in the Finder (and also bring the Finder to front in this case).

For someone relying on AppleScript, though, this is not a solution, unfortunately.

like image 127
Thomas Tempelmann Avatar answered Nov 27 '22 12:11

Thomas Tempelmann