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
Scripting Bridge
RSS for tagAutomate scriptable apps by sending and receiving Apple events using Scripting Bridge.
Posts under Scripting Bridge tag
3 Posts
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
l’m trying to automate Apple Music on macOS Tahoe 26 using ScriptingBridge. Scripts that previously worked for controlling playback, fetching track info, or manipulating playlists no longer function.
For example, code like this used to work:
`import ScriptingBridge
let music = SBApplication(bundleIdentifier: "com.apple.Music") as! MusicApplication
print(music.currentTrack?.name ?? "No track playing")`
But now it fails, returning nil for track info and failing to send playback commands.
Questions:
Has ScriptingBridge been deprecated or broken in Tahoe 26 for Apple Music?
Any guidance or example code would be appreciated.
I have created swift command line project and i have added logic to executing apple script using NSAppleScript. That will launch Microsoft Excel file
I am launching this swift command line executable from java using process launch.
3)This is not prompting me. It is throwing exception "Not authorized to send Apple events to Microsoft Excel."
I have already tried out this option
Added info.plist with NSAppleEventsUsageDescription
Added entitlement with com.apple.security.automation.apple-events to true
In packages i have selected this entitlement
i have select the bundle identifier , team and signing certificate "Development" and automatically manage signing.
can you please suggest what could i missed ?