I don't have Pandora's Box, so I commented out the two Pandora-specific code blocks. The script compiled and ran, however it has no code to generate an image file, in the event the currently playing iTunes song has no artwork. You just don't get anything in that case.
I'd be curious about the down-scaling as well. It scales down based purely on resulting file size, rather than desired image dimensions. Seems like if you were using this for album art on a control page, you'd likely want consistent dimensions.
Here's a rework test that I did, that just deals with iTunes, uses a filler image file (need to create that yourself) in case the current song doesn't have artwork, and sets the resulting image to no more than 256 pixels on it's longest side. It's got quite a few comments in it. For the filler artwork (bogus.tiff), I just opened an existing album art file, filled it with a solid color and added "No Artwork" to it. You could use any tiff you wanted, as long as it was size appropriate.
Your mileage may vary. More for comparison purposes than anything else. I use this same general technique to produce a png file of the current SiriusXM channel logo playing on Pulsar, and reference that png file as a refreshing image on a control page.
- Code: Select all
-- set the file paths to save the artwork in
-- tiff file is artwork coming from iTunes
-- png file holds converted artwork
-- bogus.tiff is just a filler artwork file, in case the song doesn't have artwork
property tiffFile : "/tmp/iTunes.tiff"
property pngFile : "/tmp/iTunes.png"
property bogusFile : "/tmp/bogus.tiff"
-- scale factor to reduce size of final image, if desired
property scaleFactor : 0.5
-- size to scale final image to (longest side), if desired
property targetSize : 256
-- call subroutine be sure iTunes is running
if isApplicationRunning("iTunes") then
tell application "iTunes"
-- if it is running, be sure a song is playing
if (player state is playing) then
-- call subroutine to get artwork for playing song
set theResult to my writeITunesArtworkToFile()
end if
end tell
end if
-- convert the tiff file to a png file
tell application "Image Events"
launch
-- if artwork was found, we'll use the tiff file created
if (theResult is equal to 1) then
set theImageReference to open tiffFile
else
-- artwork was not found, so use a filler file
set theImageReference to open bogusFile
end if
-- create the png file
scale theImageReference to size targetSize
save theImageReference as PNG in pngFile with icon
close theImageReference
end tell
-- subroutine to check for running application
on isApplicationRunning(appName)
tell application "System Events" to return (exists (some process where name is appName))
end isApplicationRunning
-- subroutine to write iTunes current track artwork to file
on writeITunesArtworkToFile()
try
tell application "iTunes"
-- get song artwork
set the_data to data of artwork 1 of current track
end tell
-- save the artwork to the tiff file
set iTunesArtwork to (open for access tiffFile with write permission)
write the_data to iTunesArtwork starting at 0
close access iTunesArtwork
-- return success code
return 1
on error
-- couldn't get artwork for some reason, return error code
return 0
end try
end writeITunesArtworkToFile