unit iTunesArtwork; { Property Store implementation for displaying music file attributes in Windows Vista Explorer and Windows Search Copyright (c) 2008 Andrew Fiddian-Green $Header: /NET/MP4/PropertyStore.pas,v 1.13 2008/05/17 14:13:27 FiddianA Exp $ For more information: Andrew Fiddian-Green see http://www.whitebear.ch In this unit: Function that recovers album artwork from iTunes Limitations: Slow Status: TESTED } interface uses Graphics; function iTunesGetArtWork(anAlbum, anArtist, aTitle: widestring; aTrackNumber: integer): TGraphic; implementation uses ComObj, GraphicEx, JPEG, SysUtils, Windows, iTunesLib_TLB; function GetTempFile(const Extension: string): string; { Generate a temporary file name Status: TESTED } var fname: array[0..MAX_PATH] of Char; begin repeat GetTempPath(SizeOf(fname) -1, fname); GetTempFileName(fname, '~', 0, fname); Result := ChangeFileExt(fname, Extension); until not FileExists(Result); end; function iTunesGetArtWork(anAlbum, anArtist, aTitle: widestring; aTrackNumber: integer): TGraphic; { Use iTunes COM automation interface to retrieve album artwork Status: } var iTunesApplication: OleVariant; artwork: OleVariant; artworkList: OleVariant; track: OleVariant; trackList: OleVariant; playList: OleVariant; i: integer; j: integer; graphicType: integer; fileName: string; const extensions: array[0..3] of string = ('unk', 'jpg', 'png', 'bmp'); begin Result := nil; try // try to open a COM interface on iTunes iTunesApplication := CreateComObject(CLASS_iTunesApp) as IiTunes; except // on error just exist on Exception do exit; end; try try // get iTunes master playlist playList := iTunesApplication.LibraryPlaylist; // create a list of all tracks having the given track title (song name) trackList := playList.Search(aTitle, ITPlaylistSearchFieldSongNames); // process all (duplicate) tracks for i := 1 to trackList.Count do begin track := trackList.Item[i]; // apply a more stringent check to eliminate false duplicate matches if (track.Kind = ITTrackKindFile) and (track.Album = anAlbum) and (track.Artist = anArtist) and (track.TrackNumber = aTrackNumber) then begin artworkList := track.Artwork; // iTunes can have more than one image, so process them all for j := 1 to artworkList.Count do begin artwork := artworkList.Item[j]; // determine the graphic type graphicType := artwork.Format; // create a graphic object if graphicType = ITArtworkFormatJPEG then Result := TJPEGImage.Create; if graphicType = ITArtworkFormatPNG then Result := TPNGGraphic.Create; if graphicType = ITArtworkFormatBMP then Result := Graphics.TBitmap.Create; if Result <> nil then begin // generate a temporary file name fileName := GetTempFile(extensions[graphicType]); try // ask iTunes to save the graphic to the temporary file artwork.SaveArtworkToFile(fileName); // load our own graphic object from the same temporary file Result.LoadFromFile(fileName); finally // delete the temporary file SysUtils.DeleteFile(fileName); end; // we got one graphic; this is enough break; end; end; end; end; except // swallow all exceptions on Exception do ; end; finally // don't leave iTunes open iTunesApplication.Quit; end; end; end.