views:

346

answers:

2

I'd like to control iTunes through Objective-C ( I just can't get Python appscript installed correctly on my OS/X 10.6.3 system ... that would have been my first choice ).

From what I gather, the IPC on Cocoa is based on Apple Events : is there either:

  1. Online documentation on iTunes / Apple Events API ?
  2. Instrospection mechanism to get to iTunes API?

I know about Applescript Editor / Open Dictionary functionality but I can't figure out how to translate the information I see into IPC calls.

Note: I have already tried working out a solution through PyObjC but the main function I am after is track searching which I can't figure out.

Disclaimer: OS/X super-newbie here.

+6  A: 

Have you considered using Scripting Bridge? It's built into the OS and also works with PyObjC. There's no distinct searching API because searching is built into AppleScript (whose clauses), which map to NSPredicate in Scripting Bridge.

>>> from Foundation import *
>>> from ScriptingBridge import *
>>> itunes = SBApplication.applicationWithBundleIdentifier_(u"com.apple.itunes")
>>> tracks = itunes.sources()[0].libraryPlaylists()[0].tracks()
<SBElementArray @0x468a630: every ITunesTrack of ITunesLibraryPlaylist 0 of ITunesSource 0 of application "iTunes" (157)>
>>> predicate = NSPredicate.predicateWithFormat_(u'artist == "Pink Floyd"')
>>> tracks.filteredArrayUsingPredicate_(predicate)
<SBElementArray @0x457b6c0: ITunesTrack whose 'cmpd'{ 'relo':'=   ', 'obj1':'obj '{ 'want':'prop', 'from':'exmn'($$), 'form':'prop', 'seld':'pArt' }, 'obj2':'utxt'("Pink Floyd") } of ITunesLibraryPlaylist 0 of ITunesSource 0 of application "iTunes" (157)>

Also, if you're new to AppleScript, I highly recommend Script Debugger's dictionary explorer.

Michael Tsai
jldupont
Hi jldupont, please use OS X not OS/X ... it's not OS/2! Sorry for my irrelevant comment, but I couldn't resist:p
Yuji
The advantage of SB is that it's included in the OS; the downside is that it's not as good as AppleScript (or appscript). You pays your money and takes your choice...BTW, iTunes does include a `search` command in addition to supporting `whose` clauses; which is best for a particular task depends on what you want to do. e.g. Using a `whose` clause: `app('iTunes').tracks[its.artist=='Pink Floyd'].name()`; using the `search` command: `app('iTunes').library_playlists[1].search(for_='Pink Floyd', only=k.artists)`
has
has: thanks a million! With these snippets, I am starting to understand the philosophy behind Applescript bridging!
jldupont
+2  A: 

If you're having problems with py-appscript, please contact the author (that'd be me) to troubleshoot it. Include information on Python installation(s) being used, Xcode version, and whether or not the problem is specific to appscript or affects all third-party modules or third-party modules with C extensions. Also, if you're using a python.org framework build rather than the built-in Python, make sure you have the optional OS X 10.4 SDK installed.

API documentation is available through OS X's AppleScript Editor (File > Open Dictionary), appscript's ASDictionary, or other third-party AS editors. If ASDictionary is installed, you can also use appscript's built-in help() method to browse the dictionary interactively.

Scriptable applications' API docs are notoriously inadequate, however, so a good understanding of how AppleScript in general and application scripting in particular works is also necessary. Apple's own AppleScript Language Guide describes the various features involved, but doesn't say much about putting them to practical use; there are several good books available if you're willing to put down some cash (disclaimer: I've just co-written one myself).

And expect to do a fair amount of digging through online articles, discussion forums and existing scripts for clues. Doug's AppleScripts for iTunes is a great source of iTunes scripts. They're written in AppleScript, of course, but many of them can be read in AppleScript Editor so are a great source of tips, and if you're using appscript you can use ASTranslate to convert from AppleScript to appscript syntax as a starting point in writing your own.

has
... and again, many many thanks! +1
jldupont