Now that I successfully have iTunes talking to my website, I thought it would be nice to post the code that does it, since I didn’t really write much of it.
The server side is just a PHP script that handles a POST and stores the information in a single-row, single-table SQLite database, so it’s not interesting.
The client is marginally interesting to me. Basically, I modified every line from code I discovered by googling, and all credit belongs to
those that wrote it (but of course, I forgot to note who wrote what as I went along).
I started with the main program definition, which sets up the listener and starts waiting for events to be passed to said listener.
The real work is all done inside the iTunesObserver object
First, the header (iTunesObserver.h):
We have two functions defined, trackNotification, which is called by the notification center when the iTunes event occurs, and urlEncodeValue, which will encode a string to be passed in a URL.
The code is defined in iTunesObserver.m:
That’s basically it. Add CoreServices.framework and CoreFoundation.framework to the Xcode project, compile, and save the resulting binary
in a good location. I use ~/bin
Then, create a launchd config file in Library/LaunchAgents directory in your Home directory
The entry LimitLoadToSessionType enables the tool to talk to the GUI and launchd will launch the tool on login when the user has a GUI session. In particular, this means the tool will not run for ssh sessions. When the tool has access to the Aqua layer, it can talk to the keychain, which means if the upload handler on the remote server is protected by a HTTP password, the NSMutableURLRequest can get that password from the keychain.
All that’s left is to start the tool, which is accomplished by loading the launchd plist into launchd.
launchctl load -S Aqua ~/Library/LaunchAgents/com.panix.users.awhitema.itunes.list