Newbie help required: accessing iTunes track properties in Xcode

Discussion in 'Mac Programming' started by jingoman, Dec 8, 2010.

  1. jingoman macrumors newbie

    Jul 30, 2010
    i am using my current needs as an excuse to have a go at Obj-C programming. it is likely very possible to achieve in a bunch of other ways, but i am hoping its also simplistic enough for me to dabble, whilst completing something i would like to accomplish.

    ok, so intro's out the way, i hope someone is ready & able to help this newb through the murky maze...

    i need to read the ratings of a selection of tracks (or the whole library, or whatever) and to create a new ID3 tag element (POPM, specifically) so that my XBMC library can read it and use the same ratings i have been building up over the past decade.

    my questions are:
    1. how do i access iTunes library? is there a framework? searching google for tutorials hasnt helped - as is the case with search you need to know what terms to search for!

    2. i have seen the Taglib Framework, that i hope should allow me to accomplish my goals. anyone have experience?

    3. this could be achieved with AppleScript (well, reading of the ratings from the library at the very least), so failing the use of Xcode, pointers in this direction would help alternatively.

    many thanks in advance...

  2. ulbador, Dec 8, 2010
    Last edited: Dec 8, 2010

    ulbador macrumors 68000


    Feb 11, 2010
    One word: ScriptingBridge .. If you can do it in AppleScript (Which you can), you can do it with the scripting bridge.

    If you aren't keen on reading, the three major steps involve creating the iTunes.h file from your iTunes application folder using 'sdef' and dragging it into your project, which they show you precisely how to do at the link and then add the ScriptingBridge.framework

    You import a few things:

    #import <ScriptingBridge/ScriptingBridge.h>
    #import "iTunes.h"
    Then you can do something similar to this:
    	iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@""];
            NSLog(@"%d",iTunes.currentTrack.rating); // Between 0 and 100
  3. mfram macrumors 65816

    Jan 23, 2010
    San Diego, CA USA
    I created an Application where I wanted to access my iTunes library metadata. The file is stored in an XML that can be read quickly by the system. By default, the file is in '~/Music/iTunes/iTunes Music Library.xml'. I used the following tidbit of code to find the file:

            NSArray *musicPaths;
            NSEnumerator *pathEnum;
            NSURL *pathURL;
            NSString *ITLPath = nil;
            // Find the iTunes Library
            musicPaths = [[NSFileManager defaultManager] URLsForDirectory:NSMusicDirectory inDomains:NSUserDomainMask];
            pathEnum = [musicPaths objectEnumerator];
            pathURL = [pathEnum nextObject];
            while ((ITLPath == nil) && (pathURL != nil))
                    NSString *TestPath;
                    TestPath = [[pathURL path] stringByAppendingPathComponent:@"iTunes/iTunes Music Library.xml"];
                    if ([[NSFileManager defaultManager] fileExistsAtPath:TestPath])
                            ITLPath = TestPath;
                            pathURL = [pathEnum nextObject];
    At the end, the ITLPath variable should have the file name. Then to read the data, I just did:

    NSDictionary *ITLDict;
    ITLDict = [NSDictionary dictionaryWithContentsOfFile:ITLPath];
    Look at the XML file to get an idea how the data is organized. You can do something like:

     tracksDict = [ITLDict valueForKey:@"Tracks"];
    ... then iterate over the Tracks in the library to build up whatever data structures you want. You can get playlist data and all kinds of stuff.

    Pull out data like:
    NSString *track_filename = [trDict valueForKey:@"Location"];
  4. seepel macrumors 6502


    Dec 22, 2009
    This combined with the ID3 library of your choosing seems like the best bet and is absolutely what I would do if my requirement were to use objective-C and make a native app. As you describe it however it seems like your use case is better suited to an AppleScript combined with a nice ID3 command line utility. I say this just because the C based ID3 libraries I've seen aren't the easiest things to deal with... In my opinion anyway.
  5. jingoman thread starter macrumors newbie

    Jul 30, 2010
    thanks for the suggestions guys. i am starting to have a dabble with this, but as a complete newbie i am also struggling too.

    is Scripting Bridge only available to code against as a command-line tool?

    i created the iTunes.h interface file following the links earlier in this thread from ulbador. but if i open Interface Builder and create an NSObject (the blue cube) and set the class of this to any of the iTunes classes inherited from this file, it doesnt produce that classes methods, etc, as connectors or whatever they are called.

    is this because i cant use it like this? i.e. if command line based?

    also i cant seem to find any tutorials on Scripting Bridge... i would like gently walking through this, as of the aforementioned newbie-ness! haha

    seepel - thanks for the additional comments. given how i seem to be struggling with the concepts and app design in this situation, i may revert to AppleScript! do you know of a decent command line based ID3 tagger?

    apologies for my lack of skills currently. was hoping this (what i thought) simple need would be a good intro to Obj-C...

    cheers again
  6. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    No need to apologize, everyone starts somewhere. Unfortunately if you have an idea for something that would be useful, it's something good to strive for, because it is something you want so it might keep you interested. However, it's probably not a good "First project" because rarely can you do something useful your first go.

  7. jingoman thread starter macrumors newbie

    Jul 30, 2010
    true enough, lee, true enough... looks like i am back to that damned calculator app and some random number generator thing from the "learning obj-c" books!

    my search has moved me towards macports of id3v2 (based on the libid3 macport) however, i cant seem to find the syntax to be able to write the tag i need (POPM) with this utility, or if its even possible...

    and this seems such a simple task!

Share This Page