I need my application to copy some files, but using Finder. Now, I know all different methods and options to programmatically copy files using various APIs, but that's not the point here. I specifically need to use Finder for the purpose, so please, let's avoid eventual suggestions mentioning other ways to copy files.
My first thought was to use the most simple approach, execute an AppleScript script using NSUserAppleScriptTask
, but that turned out not to be ideal. It works fine, unless there already are files with same names at the copying destination. In such case, either the script execution ends with an error, reporting already existing files at the destination, or the existing files can be simply overridden by adding with overwrite
option to duplicate
command in the script.
What I need is behaviour just like when Finder is used from the UI (drag'n'drop, copy/paste…); if there are existing files with same names at the destination, Finder should offer a "resolution panel", asking the user to "stop", "replace", "don't replace", "keep both" or "merge" (the latter in case of conflicting folders). So, I came to suspect that I could achieve such bahaviour by using Apple Events directly and passing kAEAlwaysInteract | kAECanSwitchLayer
options to AESendMessage()
. However, I can't figure out how to construct appropriate NSAppleEventDescriptor
(nor old-style Carbon AppleEvent
) objects and instruct Finder to copy files.
This is where I came so far, providing srcFiles
are source files (to be copied) URLs and dstFolder
destination folder (to be copied into) URL:
NSRunningApplication *finder = [[NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.finder"] firstObject];
if (!finder)
{
NSLog(@"Finder is not running.");
return;
}
NSAppleEventDescriptor *finderDescriptor = [NSAppleEventDescriptor descriptorWithBundleIdentifier:[finder bundleIdentifier]];
NSAppleEventDescriptor *dstDescriptor = [NSAppleEventDescriptor descriptorWithString:[dstFolder path]];
NSAppleEventDescriptor *srcDescriptor = [NSAppleEventDescriptor listDescriptor];
for (NSURL *url in srcFiles)
{
NSAppleEventDescriptor *fileDescriptor = [NSAppleEventDescriptor descriptorWithString:[url path]];
[srcDescriptor insertDescriptor:fileDescriptor atIndex:([srcDescriptor numberOfItems] + 1)];
}
NSAppleEventDescriptor *event = [NSAppleEventDescriptor appleEventWithEventClass:kAECoreSuite
eventID:kAEClone
targetDescriptor:finderDescriptor
returnID:kAutoGenerateReturnID
transactionID:kAnyTransactionID];
[event setParamDescriptor:srcDescriptor forKeyword:keyDirectObject];
[event setParamDescriptor:dstDescriptor forKeyword:keyAETarget];
NSError *error;
NSAppleEventDescriptor *result = [event sendEventWithOptions:(NSAppleEventSendAlwaysInteract | NSAppleEventSendCanSwitchLayer) timeout:10.0 error:&error];
The code above executes without any error. The final result
descriptor is a NULL descriptor ([NSAppleEventDescriptor nullDescriptor]
) and there's no error returned (by reference). However, nothing happens, Finder remains silent and the application doesn't make macOS/TCC prompt for a permission to "automate Finder".
I wonder if the approach above is correct and if I use correct parameters as arguments for all calling method/messages. I'm specially interested if passing keyAETarget
is the right value in [event setParamDescriptor:dstDescriptor forKeyword:keyAETarget]
, since that one looks most suspicious to me. I'd really appreciate if anyone can help me with this.
I'd also like to point out that I tried the same approach outlined above with old-style Carbon AppleEvent API, using AECreateDesc()
, AECreateAppleEvent()
, AEPutParamDesc()
and AESendMessage()
… All API calls succeeded, returning noErr
, but again, nothing happened, Finder remained silent and no macOS/TCC prompt for a permission to "automate Finder".
Any help is highly appreciated, thanks!
-- Dragan