Hello everyone,

I raised this problem on the list in May, although I was unable to follow up in timely fashion. I've since had time to look at it again, and I see some more people have asked about adding artwork to iTunes. As Jens suggests, an ID3 framework http://drewfamily.homemail.com.au/Cocoa_-_ID3Tag_framework.html exists, which although a little out of date does work. Here is what I did to write artwork to mp3 files in iTunes. Note you'll need to generate the header file for iTunes to use the Scripting Bridge - run the following command from Terminal.app : sdef /Applications/ iTunes.app | sdp -fh --basename iTunes

First, I used the ScriptingBridge to get a reference to my iTunes Music Library:

// Create an iTunes application objeect
iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
iTunesLibraryPlaylist *library;
for(iTunesSource *source in [iTunes sources]) {
        if(source.kind == iTunesESrcLibrary) {
                for(iTunesLibraryPlaylist *playlist in [source 
libraryPlaylists]) {
                        if([playlist.name isEqualToString:@"Library"]) {
                                library = playlist;             
                                break;
                        }
                }                       
        }
}

Using an iTunesLibraryPlaylist allows you to call [library fileTracks] which returns an array of iTunesFileTrack references. These are necessary (rather than iTunesTrack references) so that you can get the location attribute of the files. To get the tracks for a particular artist/album I used this method:

- (NSArray *)tracksForAlbum:(Album *)album byArtist:(Artist *)artist {
        
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(artist like %@) && (album like %@)", artist.name, album.name];
        SBElementArray *tracks = [library fileTracks];  
        NSArray *albumTracks = [tracks filteredArrayUsingPredicate:predicate];  
        return albumTracks;
}

The above method is what Apple suggests doing when using the ScriptingBridge (<http://developer.apple.com/documentation/Cocoa/Conceptual/ScriptingBridgeConcepts/UsingScriptingBridge/chapter_3_section_7.html#//apple_ref/doc/uid/TP40006104-CH4-SW6 >), rather than iterating through the whole library, however it still crashes iTunes, although my music library has over 40,000 tracks. Therefore I'd suggest that before running this, quit iTunes, rename your entire iTunes library folder (~/Music/iTunes/) to something else (~/Music/iTunes main/) and then start iTunes again, so it'll create an empty library - then add the music you want to add artwork to.

You can iterate over this NSArray and access each track using the ID3 tag framework, assuming an image to embed for the artwork needs to be stored as an NSBitmapImageRep exists:


NSBitmapImageRep *newArtwork; // this needs to exist            
TagAPI *id3Tag = [[TagAPI alloc] initWithGenreList:nil];
NSArray *tracks = [self tracksForAlbum:anAlbum byArtist:anArtist];

for(iTunesFileTrack *track in tracks) {
        [track get];
        [id3Tag examineFile:[track.location path]];
                                
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:newArtwork, @"Image",
                                                        @"NSTIFFFileType", @"Mime 
Type",
                                                        @"Other", @"Picture 
Type",
                                                        @"", @"Description", 
nil];                                                                  
        [id3Tag setImages:[NSMutableArray arrayWithObject:dic]];
        if([id3Tag updateFile] == 0) {
                NSLog(@"successfully processed %@", [id3Tag getTitle]);         
                              
        } else {
                NSLog(@"failed to process %@", [id3Tag getTitle]);              
                      
        }
}                       

That should embed your artwork into the files - iTunes probably wont display the artwork until you select or play the track. This doesn't work for .m4a files however. It looks like it might be possible to use the - (void)addImage:(NSImage *)image forDuration:(QTTime)duration withAttributes:(NSDictionary *)attributes method of QTTrack/QTMovie from the QTKit, although I've not implemented it myself yet....

Anyway, hope this helps someone, or if anyone has a better method - let us all know!

Cheers
Dan


On 11 Jun 2008, at 14:59, Nicolas Goles wrote:

Hi, I want to develop a Cocoa Application to manage an embed Cover Art files to Mp3's. The thing is that I don't know how to achieve this, or where to read about how to do it. I asked in several places / googled a lot and ended
up here.
Could anyone help me out a bit ? Link me to some reading would be amazing, but if you get into more detailed explanation, that would be great too.

Thanks a lot .
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/danthorpe%40gmail.com

This email sent to [EMAIL PROTECTED]

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to