Skip to content

Instantly share code, notes, and snippets.

@edibiase
Created Dec 21, 2008
Embed
What would you like to do?
#import <Foundation/Foundation.h>
#import "iTunes.h"
#import "iTunesLibraryDumperKeys.h"
NSMutableDictionary *mutableDictionaryForItem(iTunesItem *item) {
return [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInteger:[item id]], EZiTunesLibraryDumperKeyItemID,
[NSNumber numberWithInteger:[item index]], EZiTunesLibraryDumperKeyItemIndex,
[item name], EZiTunesLibraryDumperKeyItemName,
[item persistentID], EZiTunesLibraryDumperKeyItemPersistentID,
nil];
}
NSDictionary *dictionaryForPlaylist(iTunesPlaylist *playlist) {
NSMutableDictionary *playlistDict = mutableDictionaryForItem(playlist);
return playlistDict;
}
NSArray *arrayForPlaylists(SBElementArray *playlists) {
NSMutableArray *dicts = [NSMutableArray array];
for (iTunesPlaylist *playlist in playlists) {
[dicts addObject:dictionaryForPlaylist(playlist)];
}
return dicts;
}
NSDictionary *playlistDictionaryForSource(iTunesSource *source) {
NSMutableDictionary *playlists = [NSMutableDictionary dictionary];
[playlists setObject:arrayForPlaylists([source audioCDPlaylists])
forKey:EZiTunesLibraryDumperKeySourceAudioCDPlaylists];
[playlists setObject:arrayForPlaylists([source devicePlaylists])
forKey:EZiTunesLibraryDumperKeySourceDevicePlaylists];
[playlists setObject:arrayForPlaylists([source libraryPlaylists])
forKey:EZiTunesLibraryDumperKeySourceLibraryPlaylists];
[playlists setObject:arrayForPlaylists([source playlists])
forKey:EZiTunesLibraryDumperKeySourcePlaylists];
[playlists setObject:arrayForPlaylists([source radioTunerPlaylists])
forKey:EZiTunesLibraryDumperKeySourceRadioTunerPlaylists];
[playlists setObject:arrayForPlaylists([source userPlaylists])
forKey:EZiTunesLibraryDumperKeySourceUserPlaylists];
return playlists;
}
NSArray *sourceArrayForiTunesApplication(iTunesApplication *iTunes) {
NSMutableArray *sources = [NSMutableArray array];
for (iTunesSource *source in [iTunes sources]) {
NSMutableDictionary *sourceDictionary = mutableDictionaryForItem(source);
[sourceDictionary setObject:[NSNumber numberWithInt:[source kind]] forKey:EZiTunesLibraryDumperKeySourceKind];
[sourceDictionary addEntriesFromDictionary:playlistDictionaryForSource(source)];
[sources addObject:sourceDictionary];
}
return sources;
}
/* Dumps local iTunes library to stdout as a property list */
int main(int argc, const char *argv) {
iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
NSMutableDictionary *iTunesDict = [NSMutableDictionary dictionary];
[iTunesDict setObject:sourceArrayForiTunesApplication(iTunes) forKey:EZiTunesLibraryDumperKeySources];
NSString *errorDescription;
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:iTunesDict
format:NSPropertyListXMLFormat_v1_0
errorDescription:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment