How to retrieve a rectangle from an AppleEvent?


I’m sending an AppleEvent to retrieve the bounds of a window in another app. I don’t know how to coerce the result as a rectangle.

In Script Editor, it’s straightforward:
tell application AppName to get bounds of window 1
Result: {0, 25, 1238, 597}

In Xojo, it’s more complicated. The AppleEvent has these values after the event was sent:

There are only 3 fields that contain valid data:
• ReplyDescList, which only holds an item:
(however Xojo won’t let know what it contains)

• ReplyPtr, but how would I convert a pointer to a rectangle?
• ReplyString. My current workaround is to use this string, which happen to return correct bounds with the following formula, as long as the window is completely on screen:

Var Bounds() As Integer

for i as Integer=1 to s.Length step 2
  Bounds.Add s.Middle(i,1).Asc*256+s.Middle(i-1,1).Asc
if Bounds.LastIndex<>3 then Return Result

Return Array(Bounds(1),Bounds(0),Bounds(3),Bounds(2))

That’s not reliable.

In various websites, bounds are referred to QuickDrawRectangles, and code examples (in other languages) look to mention getting the rectangle’s bounds as a pointer, which is probably half-way of the answer, but Xojo+pointers+me=nothing.

Can someone enlighten me, please?

That ReplyDescList is exactly what you need. I’m not at my computer so I can’t verify, but it should contain another AppleEventDescList which contains four numbers.

Unfortunately this is all opaque in the debugger, so you’re stuck just experimenting until you figure it out. But keep at it!

1 Like

Thanks! At first, I thought it was a good news, but… Xojo crashes.

Here’s the code I tried:

Var ae As new AppleEvent("core","getd",BundleID)
ae.ObjectSpecifierParam("----")=GetPropertyObjectDescriptor(TheWindow,"pbnd") //TheWindow is a valid AppleEventObjectSpecifier

call ae.Send
Var d As AppleEventDescList=ae.ReplyDescList
Var d2 As AppleEventDescList=d.DescListItem(1)
Var i As Integer=d2.IntegerItem(1)

When I step thru this code, after the assignment of d2, the debugger reports incorrectly that d2 is an AppleEventRecord object (so, accessing its IntegerItem crashes the app, since it’s the wrong object type).
This is certainly the same issue I reported some days ago: How to convert an AppleEventRecord to an AppleEventObjectSpecifier when the framework mangles them? - #11 by Arnaud_N

This auto-conversion to AppleEventRecord in various places of the framework really sucks…

May I ask you if you can try on your side, if you get the same crash?

In my early days of RealBasic, I already played with AppleEvents. I experimented like you suggests. Trials and errors, for possibly months.
Opacity in the debugger and types changing-bugs (AppleEventRecord), along with rare successful attempts in some cases eventually led me to lose patience in this area. Since nothing has evolved in Xojo regarding AppleEvents, I’m not sure I want to revive this bad experience, hence the reason I asked in the forum (but I tried a bit, anyway).

After some creative fiddling around, I can find no way around this. I’m further convinced that the framework is taking a pointer to an AppleEventDescList returned from MacOS and improperly casting it as an AppleEventRecord. Only an Xojo engineer can verify this hypothesis.

Since that’s exceedingly unlikely, I see two options for you to look into.

First, AppleEvents have a ReplyPtr property. This is a pointer to the OS object that represents the reply. If you’re keen to write a bunch of Declares, you can use this pointer to retrieve information from the reply, bypassing the Xojo AppleEvent framework. The MacOSLib project has some basic support for reading a reply directly via Declares.

Second, you can do what I’ve done and largely abandon Xojo’s AppleEvent classes, and replace them with AppleScripts. You can pass parameters to AppleScripts and the reply will always be a string, which you can then parse to your heart’s delight. This is probably easier than working with Declares, but you’ll have to be more careful with error handling when interacting with AppleScripts.

I had already suggested that. I even have a wrapper for NSAppleScriptMBS which creating AppleScripts really simple.

Don’t get me wrong: I appreciate your help. It’s just that moving to AppleScript will take time (which I don’t have yet), so I’m currently trying to repair what can be repaired in a short-sighted version. When I get more time, I’ll make the conversion.
For now, it mostly works with AppleEvents, except the issues I mentioned; it’s an in-house app, not needed to be perfect.

Thanks for your attempts. Hope my Issue report will be reviewed (and, as a miracle, fixed).

I used to make declares for Carbon, but I’ve never truly made Cocoa ones (and when I try, I don’t even know where to look for building them). If you can point me to some starting point, that would be appreciated.

In a distant future, I will (unless Xojo fixes the AppleEventRecord conversion issue in the meantime). I have other apps (and things) to do that need to be fixed/implemented.
Still, it’s a shame Xojo provides AppleEvent but they’re malfunctioning. Just enough to lead one to getting accustomed up to the point where things need to be rewritten.

Thank you.

This has really been bothering me, so I spent several hours last night reading up on the Apple Event Manager and playing around with Declares. I’ve made some progress - I’m close to solving this particular case. I’ll update you soon.


Update: I have working code for this. :slight_smile: I’m generalizing it.

1 Like

Great news!
Is that something you may share, please?