Tutorial: OS X automation with MacRuby and the Scripting Bridge
arstechnica.com
arstechnica.com
I think that doing it that way sends many more apple events (and is thus much slower) than needed. Here's two examples in Python. The first is similar to the example ruby code in the article:
from ScriptingBridge import SBApplication, NSPredicate
iTunes = SBApplication.applicationWithBundleIdentifier_("com.apple.iTunes")
library = [s for s in iTunes.sources() if s.name() == "Library"][0]
playlist = [p for p in library.playlists() if p.name() == "Top 25 Most Played"][0]
tracks = [t for t in playlist.tracks() if t.rating() > 3]
favsongs = ["<li>%s - %s (%s)<li>" % (t.artist(), t.name(), t.playedCount())
for t in sorted(tracks, key=lambda t:t.playedCount())]
print '\n'.join(favsongs)
The problem with this code is that we first ask iTunes for all its sources, then filter those in Python. Next we ask the library source for all its playlists, and again filter in Python. Etc.Instead, we can rely on so-called lazy evaluation and send a single apple event like so:
library = iTunes.sources().objectWithName_("Library")
playlist = library.playlists().objectWithName_("Top 25 Most Played")
tracks = playlist.tracks().filteredArrayUsingPredicate_(
NSPredicate.predicateWithFormat_("rating > 3"))
See the Apple Scripting Bridge documentation for more info."Appscript is a high-level, user-friendly Apple event bridge that allows you to control scriptable Mac OS X applications from Python, Ruby and Objective-C."
However, it does have to be installed separately. ScriptingBridge OTOH comes installed with OS X.
* https://github.com/albertz/simbl
* https://github.com/albertz/FScriptAnywhereSIMBL
* https://github.com/albertz/Pyjector
* https://github.com/albertz/ChromeWebApps
I have them running and they just inject to all running application and it is pretty easy to make it all stable and just working fine because of Cocoa and ObjC.