apple events and Finder

I am writing a lightweight file manager and being the lazy programmer that I am, the program is using aesend to send an apple event to the Finder to do file copies and deletions. The problem I have is that I need to know when the copy finishes but I also do not want to wait for it finish either, i.e. I want the file operations to execute in the background. I am using Thomas Tempelmann’s aeSend code, shown below. I’ve tried using MBS’ Carbon Events manager to look for a reply back from Finder but I get nothing.

Anyone have any ideas on how to do this? Am I missing something here?



#if TargetMacOS
if items.Ubound >= 0 then
dim ae as AppleEvent = NewAppleEvent (“core”, opcode, “MACS”)

  dim list as new AppleEventDescList
  for each f as FolderItem in items
    if f <> nil then
      list.AppendFolderItem (f)
    end if
  ae.DescListParam("----") = list
  if dest <> nil then
    ae.FolderItemParam("insh") = dest
  if allowInteraction then
    declare function AESend lib "Carbon" (ae as Integer, reply as Ptr, sendMode as Integer, prio as Integer, timeoutTicks as Integer, idle as Ptr, filter as Ptr) as Integer
    const kAENoReply = &h00000001 ' sender doesn't want a reply to event
    const kAEQueueReply = &h00000002 ' sender wants a reply but won't wait
    const kAEWaitReply = &h00000003 ' sender wants a reply and will wait
    const kAEDontReconnect = &h00000080 ' don't reconnect if there is a sessClosedErr from PPCToolbox
    const kAEWantReceipt = &h00000200 ' (nReturnReceipt) sender wants a receipt of message
    const kAENeverInteract = &h00000010 ' server should not interact with user
    const kAECanInteract = &h00000020 ' server may try to interact with user
    const kAEAlwaysInteract = &h00000030 ' server should always interact with user where appropriate
    const kAECanSwitchLayer = &h00000040 ' interaction may switch layer
    const kAEDontRecord = &h00001000 ' don't record this event - available only in vers 1.0.1 and greater
    const kAEDontExecute = &h00002000 ' don't send the event for recording - available only in vers 1.0.1 and greater
    const kAEProcessNonReplyEvents = &h00008000 ' allow processing of non-reply events while awaiting synchronous AppleEvent reply
    //dim sendMode as Integer = kAEWaitReply+kAEAlwaysInteract+kAECanSwitchLayer -> original code I modified to use different flags
    dim sendMode as integer=kAEAlwaysInteract+kAEQueueReply
    // Issue the command to the Finder
    dim err as Integer = AESend (ae.Ptr, nil, sendMode, 0, 0, nil, nil)
    if err <> 0 then
      return "Could not communicate with Finder (error "+Str(err)+")"

I would suggest you use API instead of AppleEvents. You might have to get some jiggery pokery on for non-thread blocking copy operations.

Or use a shell class and the ditto command.

You won’t be able to sell an app on the Apple Mac App Store that can control the “Finder”, it’s prohibited by Apple.

Thanks for the suggestions. I will look into using shell and ditto.

Not planning on putting this on the App Store. I’ve heard of problems with file managers and the app store and this was for just personal use. But you never know. :slight_smile:

Personally I would take the App Store limitations as a sign of things to come, I suspect over time as the macOS becomes more restrictive, the App Store may be the only way to distribute macOS applications. Microsoft have just gone this route with Windows 10 S, it can only run apps from the MS App Store!

The day Apple implements that requirement is the day I move to Linux. Tired of Apple and MS trying to dictate to us how to run our computers.

The AppleScript works better in NSAppleScriptMBS than the old AppleScriptMBS class.
Especially NSAppleEventDescriptorMBS class is excellent to work with the data structures. I added a lot of helper functions to make live easier, e.g. to build lists and objects from various data types.

For the file operations, you should look into NSFileManagerMBS where most operations are available nowadays.
The asynchron file copying is done by MacFileOperationMBS class just like in the Finder. You can get progress events and update status.

See Proper way to execute an AppleScript? . No plugin needed here.

I doubt very much Windows S has a great future. This looks like a very bad idea, to sell a crippled machine, just to have the opportunity to charge for the full version later. Microsoft may have been just one mesquine thing too far…