Re: creating image from itunes artwork
Re: creating image from itunes artwork
- Subject: Re: creating image from itunes artwork
- From: Sander Tekelenburg <email@hidden>
- Date: Sun, 21 May 2006 17:01:33 +0200
At 12:16 +0200 UTC, on 2006-05-21, Federico Tessmann wrote:
> i'm developing an application in cocoa and i wanted to show the
> artwork of a track of itunes in a NSimageView on my app.
> i was able to get the artwork but what i can't do is to create an
> image in my app from that artwork so as to show it in the view.
I use the below handler in a script that takes iTunes' 'artwork' and makes
image files from it. Note that I don't fully understand the tail aspect of
the cleanup shell script -- it's something I borrowed from someone else. The
file name extension routine is ugly; done this way because it doesn't seem
possible to ask iTunes in what format the image is. It's good enough for me
since I use this script in a situation that I control, but if you're panning
to distribute something, you should probably come up with something more
elegant.
-- property targetFolder : "a path (as string)"
-- property theName : "a file name, without its name extension (as string)"
on grabArtwork(theTrack)
-- theTrack needs to be an iTunes reference to a track
try
set finalartworkFile to false
set extension to false
tell application "iTunes"
set artworkData to (data of artwork 1 of theTrack) as picture
set artworkFormat to (format of artwork 1 of theTrack) as string
if artworkFormat contains "JPEG" then
set extension to ".jpg"
else if artworkFormat contains "PNG" then
set extension to ".png"
else if artworkFormat contains "GIF" then
set extension to ".gif"
end if
set tempartworkFile to (targetFolder & "temp" & extension) as string
set finalartworkFile to (targetFolder & theName & extension) as string
set file_reference to (open for access tempartworkFile write
permission 1)
set eof file_reference to 0
write artworkData starting at 0 to file_reference as picture
close access file_reference
-- some clean-up routine I stole and don't yet fully understand...
do shell script "cd " & quoted form of (POSIX path of targetFolder)
& ¬
";tail -c+223 " & quoted form of ("temp" & extension) & "> " & ¬
quoted form of (theName & extension) & ";rm " & ¬
quoted form of ("temp" & extension)
end tell
set artworkData to "" --empty this variable
return {extension, finalartworkFile}
on error m number n
log {n, m}
end try
end grabArtwork
--
Sander Tekelenburg, <http://www.euronet.nl/~tekelenb/>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden