views:

255

answers:

3

I would like to access a couple of different Mac OS X applications from preferably Ruby, but I would settle for PHP. The applications are Elgato's turbo.264 and Apple's iTunes. Both have Applescript Libraries defined that would allow me to do what I want to do from Applescript, but I don't want to do this in Applescript. If I can't do this in Ruby or PHP, perhaps I can do it in objective C / Cocoa and create some kind of wrapper that I could call from Ruby.

Is this even possible? It seems like if the methods are available in Applescript they should be available in other languages, I have just not been able to find anything.

+3  A: 

Tried appscript (http://appscript.sourceforge.net/rb-appscript/index.html)?

The example from the site:

Instead of AppleScript:

tell application "TextEdit"
    get paragraph 1 of document "ReadMe"
end tell

You write in Ruby:

app('TextEdit').documents['ReadMe'].paragraphs[1].get
also
I am curious why this answer received 3 up votes while the RubyOSA received only one. The answers seem very similar just recommending a different tool - is there something inherently better about rb-applescript?
Scott
I think it's basically that this answer came first, and they're more or less equal. If several answers are equivalent, often people will only choose one as sort of a representative. I've personally never found any great difference between Appscript and RubyOSA.
Chuck
+4  A: 

Try RubyOSA (http://rubyosa.rubyforge.org) and then you can do this:

require 'rbosa'
itunes = OSA.app('iTunes')

track = itunes.current_track
p track                     # <OSA::Itunes::FileTrack:0x1495e20>
p track.name                # "Over The Rainbow" 
p track.artist              # "Keith Jarrett" 
p track.duration            # 362.368988037109 
p track.date_added.to_s     # "2006-06-30" 
p track.enabled?            # true

# Play the selected track.
itunes.play                    

# Fade the volume.
100.times { |i| itunes.sound_volume = i; sleep 0.1 }  

# Set iChat's status message to the current track.
OSA.app('iChat').status_message = "Playing: #{track.name}"

You can talk to any Mac OS X app that supports AppleScript

Rod Schmidt
+3  A: 

Mac OS X 10.5 (the current version) includes the Scripting Bridge, a framework that lets you access AppleScript functionality from Cocoa applications. You can also access it from RubyCocoa and PyObjC. Basically, it works like this:

require 'osx/cocoa'
require_framework 'ScriptingBridge'
include OSX

iTunes = SBApplication.applicationWithBundleIdentifier 'com.apple.iTunes'
iTunes.activate
puts "Play #{iTunes.currentTrack.name}?"
iTunes.playpause if gets.strip == "Yes"
Chuck