views:

21

answers:

2

Can you use KVO with Scripting Bridge?

I want to know if I can make an Scripting Bridge interface for my application and allow client applications to be notified by observing an object returned by one of my app's SB commands.

Is this possible? If not, what's an alternative?

+2  A: 

Scripting Bridge objects are just Cocoa-flavored sugar around sending events to the application and getting replies back. If you observe a property of an object, you aren't sending it any messages, so you wouldn't be sending the application any events. There is no way in the Apple Events system to observe something—you can send events and get replies, and that's it.

The only way to do what you want is to poll: Send the application an event every x seconds asking for the current state.

If you're trying to observe the current track or playing status in iTunes, then for that and that alone, you don't need to poll, because iTunes posts an (undocumented) distributed notification when the current track or playing status changes. You can use Notification Watcher to examine the notification when it comes in so you know what to extract from it.

Peter Hosey
When a client app observes a key for one of my objects returned by a SB command, it signs itself up as an observer to that key **by sending it a addObserver message** and when my app changes that key, all observers are notified (by a message being sent to the observing object). So I don't understand why that wouldn't work over SB.
AshleyS
AshleyS: Because you're the one changing the value. KVO wraps the `setFoo:` method and sends the KVO notifications whenever you call it, same as it would do for any other object in your application. That doesn't help when the value of the property changes in the other application, because that doesn't call any methods of the SBObject in your application.
Peter Hosey
The value of the property changes in my app, not the client app.
AshleyS
AshleyS: Exactly. That will cause KVO notifications, because KVO wraps the setter method in your SBObject instance. The value changing in the other application cannot cause KVO notifications, because that doesn't send a setter message to your SBObject and cannot cause the notifications by any other means either.
Peter Hosey
I get it now. Thank you Peter.
AshleyS
A: 

I have found an alternative solution called Distributed Objects for anyone who cares about my question.

AshleyS