English 中文(简体)
iTunes Plugin Programming: Intercept Events
原标题:

Are there any good solutions for capturing events from within iTunes?

I d like to build a plugin that sits inside of iTunes and respondes to rating changes for particular songs. For Windows they have an SDK that I haven t delved into yet, but I d like to find something for Mac too.

Any suggestions?

最佳回答

As far as I know, Apple provides a free (as in zero-cost) SDK for iTunes Visual Plug-Ins for both OS X and Windows. If you can t get what you need through that, you could always use iTunes Apple Events scripting interface to monitor via polling from another OS X app using an OSA-compatible interface such as AppleScript or appscript with Python, Ruby, or Objective-C. That may not be what you d like but it is documented and supported.

For example, with py-appscript, here s how to access the current track and the those in the recently played smartlist:

>>> from appscript import *
>>> it = app( iTunes )
>>> it.current_track.rating()  # 40 == 2 stars
40
>>> len(it.playlists[ Recently Played ].tracks())
80
>>> it.playlists[ Recently Played ].tracks[1].rating()
40
>>> it.playlists[ Recently Played ].tracks[1].rating.set(to=100)
>>> it.playlists[ Recently Played ].tracks[1].rating()  # 100 = 5 stars
100

It s also possible to filter on various metadata fields (open the iTunes.app scripting definition in the AppleScript Script Editor to browse):

>>> import datetime
>>> an_hour_ago = datetime.datetime.now() - datetime.timedelta(hours=1)
>>> it.playlists[ Library ].tracks[its.modification_date >= an_hour_ago]()
[app(u /Applications/iTunes.app ).sources.ID(45).library_playlists.ID(49347).file_tracks.ID(72017)]

But note that changes to ratings do not appear to affect the modification date.

问题回答

Mac iTunes emits distributed notifications on track changes and a few other events of significant interest. I doubt it sends notifications for minor changes to track info, however. As Ned says, iTunes plugin support is limited to visualizers, so your other options would be:

  1. periodically polling from an external process, although that s going to cost both you and iTunes if the user s playlist is large

  2. maybe look into using PreFab UI Actions to glom onto iTunes UI widgets and trigger AppleScripts

  3. see if you can attach an FSEvent notification to the iTunes Music Library.xml file that iTunes keeps in the user s music library folder. Assuming iTunes updates that file immediately on settings changes, you could probably figure out from there what has changed since last time.

None of which are ideal, but the sort of plugin-based extensibility you re talking about is really pretty rare amongst Mac apps, so you re probably going to have to kludge it one way or another, or else rethink your needs.





相关问题
2 mysql instances in MAC

i recently switched to mac. first and foremost i installed xampp. then for django-python-mysql connectivity, i "somehow" ended up installing a seperate MySQL. now the seperate mysql installation is ...

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Controlling OSX windows

I m trying to control windows of a foreign OSX applications from my application. I d like to 1. move the windows on the screen 2. resize the windows on the screen 3. change the currently active window ...

Switching J2SE versions on Mac OS (SnowLeopard)

My current JDK on Mac OS (10.6) is set to 1.6 and I d like to switch to 1.5. A listing of /System/Library/Frameworks/JavaVM.framework/Versions/ shows: lrwxr-xr-x 1 root wheel 10 Nov 3 18:34 ...

Scrolling inside Vim in Mac s Terminal

I ve been googling around trying to figure out if it s possible to use my mouse wheel to scroll while inside Vim in Mac s Terminal, with no luck. It seems as if only X11 or iTerm support this. Before ...

export to MP3 from quicktime API

A question for Apple,QT programmers. Would like to know if it s possible to export a Movie object to MP3 using the QuickTime API. Preferably the ConvertMovieToFile function. I ve looked at ...

热门标签