3 Replies
      Latest reply on Apr 5, 2019 4:10 AM by eskimo
      wiristeam Level 1 Level 1 (0 points)

        I have an app that handles different kinds of apple events. I am developing an auxiliary app to test this first app (all in swift).

        In my main app I have a handler function such as:

        func handleAppleEvent(event : UnsafePointer?, reply : UnsafeMutablePointer?, refcon : Optional) -> OSErr {
             //read apple event 
             ....
             return error
        }

         

        Where error is a variable that can be 0 if no error occured or any code depending on what has happened.

        In my aux app I create an apple event pointing to the main app, an empty reply event and send it using the method AESendMessage

        AESendMessage( &appleEvent,
                       &reply,
                       AESendMode(kAEWaitReply),
                       0)

        I want to test it with correctly set appleEvents and wrongly set AppleEvents. No matter what kind of event I send, the result of AESendMessage is 0. I am sure that the return is non zero on the main app, since by debugginb goth apps at the same time this variable is correctly set to other values.

        Therefore, either:

        • I am not understanding how to retreive this error code
        • There is somewhere where the information is lost

        EDIT: When I send the events from AppleScripts the result codes are shown as I expect. (for ex. when a parameter is missing a "Some parameter is missing for «event ********»." error is shown with its related error code). I do not want to do the testing with AppleScripts since the information being sent is complex.

        Can someone help me?

        Thank you

        • Re: AESendMessage always returns no error (0)
          hhas01 Level 1 Level 1 (20 points)

          AESendMessage() only returns Apple Event Manager errors. For application errors you have to inspect the reply event.

           

          I strongly recommend you write your tests in AppleScript. The language *****, but it’s the de facto reference against which all scriptable apps are measured. There are a lot of subtle (and not-so-subtle) wrinkles in its Apple event behavior which your own code is not guaranteed to replicate.

           

          If you really insist on using Swift, you might consider using SwiftAutomation. Caveat emptor, etc, but it has pretty good quirk-for-quirk compatibility with AppleScript and is a lot less work than rolling Apple events from scratch. Last stable release:

           

          github [dot] com/hhas/SwiftAutomation/commit/d5748e5e2ceea9abb37a090aabf2ba2fe4649443

            • Re: AESendMessage always returns no error (0)
              wiristeam Level 1 Level 1 (0 points)

              Thanks for the answer!

               

              I will check the SwiftAutomation, that seems pretty useful for what we need.

               

              On the other hand, do you know how is the error stored in the reply event? If we do not modify it in the handler function, I would assume that it is returned as was received... so, would we need to store the result information in the handler or is the return code automatically stored?

               

              Among the parameters received by our application, there are arrays of data which afterwards processed differently according to the type stored in the parameter. I believe there is no easy way to read this using AppleScripts. And we cannot intervene with how the client app is sending the information since it does not belong to us. Am I right in assuming this?