Search code examples
rubycocoamacosscripting-bridge

Callbacks with Scripting Bridge?


I'm using Ruby to check the position of videos I'm playing in Quicktime via Scripting Bridge.

At the moment I'm just checking the position like so every n seconds:

require 'osx/cocoa'

include OSX
OSX.require_framework 'ScriptingBridge'

app = SBApplication.applicationWithBundleIdentifier_("com.apple.QuickTimePlayerX")

while true
  app.documents.each do |doc|
    p doc.currentTime
    p doc.playing
  end
  wait(n_seconds)
end

This is more CPU intensive than I'd like, is there a way to make Scripting Bridge trigger a Ruby block when a particular event happens?

eg. When a document is opened, closed, paused/resumed and so on?

Thanks in advance!


Solution

  • That's not what Scripting Bridge does. It's just a way to use Cocoa instead of AppleScript to send and receive Apple Events. What you can do is limited only by the other application's scripting dictionary.

    So the question becomes “does QuickTime Player have a way to register my application with it so that it will send an event to my application when certain things happen”, and the answer, as determined by looking in its dictionary, is no.

    You might consider showing the movie in your own app, instead. Then you would be able to passively receive notifications as things happen instead of having to poll.