Displaying iTunes Album Art

Posted on
Sun Apr 01, 2012 10:33 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Displaying iTunes Album Art

Tried compiling the entire script but get "Can’t set «class usTA» to false. Access not allowed."

It will compile and run with the Pandora Box sections removed but no artwork is saved.

Curious, when loading the script into Script Editor it prompts for the progressBar.app.
Is that something I should have installed?

Thanks,

Carl

Posted on
Mon Apr 02, 2012 1:42 am
gtreece offline
Posts: 171
Joined: Sep 26, 2011

Re: Displaying iTunes Album Art

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

Posted on
Mon Apr 02, 2012 9:51 am
Dewster35 offline
Posts: 1030
Joined: Jul 06, 2010
Location: Petoskey, MI

Re: Displaying iTunes Album Art

Carl,
Honestly I don't know much about what the script is actually doing. I'm no script writer. The developer of Pandora's box wrote it for me as a favor.

I do know I did not install anything new on my machine to be to make this work. I'm running the latest version mac OS (10.7.3) and the latest version of iTunes (10.6.1). It works for my purposes though... just to simply create the album art file if it's available from whatever is running and display it on my control page. Wish I could be more helpful. I do know he tried it and a couple of others have tried it and it does what they need as well.

Posted on
Mon Apr 02, 2012 10:23 am
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Displaying iTunes Album Art

Thanks. Wish I could get it to work..still nothing. I'm running 10.5.

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 : "Library/Application Support/Perceptive Automation/iTunes.tiff"
property pngFile : "Library/Application Support/Perceptive Automation/iTunes.png"
property bogusFile : "Library/Application Support/Perceptive Automation//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



The code runs and creates a png but it's not the current track. The script wouldn't run unless i had
a iTunes.tiff file in the save location, so I made one and put it there but the png that is created is just a copy of the it,
not the currently playing art.

I realize this script isn't well understood....I'll plug away at it a bit more

Thanks,

Carl

Posted on
Mon Apr 02, 2012 10:55 am
gtreece offline
Posts: 171
Joined: Sep 26, 2011

Re: Displaying iTunes Album Art

Hi Carl. You'll need a leading "/" in your file name properties. They need to be valid paths.

Code: Select all
property tiffFile : "/Library/Application Support/Perceptive Automation/iTunes.tiff"


And of course, write permission to any location you specify. You should be good with yours, once you fix the path.
Greg

Posted on
Mon Apr 02, 2012 10:57 am
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Displaying iTunes Album Art

There's a bug in the file paths in the script - they need to start with "/Library" not "Library". Also the path to the bogus.tiff file has two slashes (which shouldn't make any difference) but you'll want to make sure that file exists or the script will fail when the first song that doesn't have an image hits.

[EDIT] someone beat me to it... ;)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Apr 02, 2012 12:03 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Displaying iTunes Album Art

Fixed the paths but still no go. The png will update, and size correctly to however
I have it set in the script, but the tiff won't update to the currently playing artwork.

Should it matter that music is on an external drive?

Found this script that saves the current album as a pict file to the desktop. Seems to work fine.

Code: Select all
tell application "iTunes"
   set s to item 1 of (selection)
   set pd to data of first artwork of s
end tell
set fs to ("" & (path to desktop) & "MyArt.pict") as file specification
set rn to (open for access fs with write permission)
try
   set eof rn to 512
   write pd to rn starting at 513
   close access rn
on error err_mess
   close access rn
   error err_mess
end try

tell application "Finder"
   set file type of fs to "PICT"
   set creator type of fs to "????"
   activate
end tell


Would it difficult to mod it to scale the image and save as a png?


Thanks a bunch,

Carl

Thanks,

Carl
Last edited by ckeyes888 on Mon Apr 02, 2012 12:44 pm, edited 2 times in total.

Posted on
Mon Apr 02, 2012 12:40 pm
gtreece offline
Posts: 171
Joined: Sep 26, 2011

Re: Displaying iTunes Album Art

This is the part that gathers the iTunes artwork:

Code: Select all
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


It actually requires that two things happen successfully:
1 - that the current song actually has artwork (you'd be able to see it in iTunes)
2 - that the user running the script has write permission to the file specified for 'tiffFile'

So if you know the current song has artwork, then I'd say the script is failing to write it to the file (most likely either a permission issue, or the path is invalid). I didn't put anything in there to specifically alert if it had trouble writing the file to disk.

You might change the file path to the /tmp location that I used originally, to see if it yields different results. You can use the finder 'Go' menu to go to the /tmp folder to see the results.

Greg

Posted on
Mon Apr 02, 2012 1:04 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Displaying iTunes Album Art

The tiff definitely has read write privileges but still no go. Tried a different path as well.
Odd for sure. Guess I'll try to rework the one that does work to scale and save as a png.

Thanks,

Carl

Posted on
Mon Apr 02, 2012 1:32 pm
Dewster35 offline
Posts: 1030
Joined: Jul 06, 2010
Location: Petoskey, MI

Re: Displaying iTunes Album Art

I was told that pulling information out of iTunes was messy, even though it was possible. If you have some suggestions for improvements, I'd be happy to pass them along if I think they are worthwhile.

Posted on
Mon Apr 02, 2012 1:54 pm
gtreece offline
Posts: 171
Joined: Sep 26, 2011

Re: Displaying iTunes Album Art

That is odd, Carl. The functional differences between the script you found, and the one I posted are, a) where it writes the file, and b) the format it writes the file in (I don't know details, but the starting at 513 part is tied to creating a PICT).

Otherwise, the two are doing the same thing. There are slight differences in the way the two scripts address the artwork of the iTunes track, which could be version differences. There are many ways to say the same thing in Applescript, but they don't always work on every OS/App version.

I'm on 10.7.3. I don't have an earlier machine to test with at the moment, but I've run into similar oddities between major OS X versions before when working on Applescripts. Particularly with file/folder operations.

Posted on
Mon Apr 02, 2012 1:58 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Displaying iTunes Album Art

Well my last attempt works great if you select the songs directly in iTunes...but does not work if I
change the songs using AS within Indigo.

Code: Select all
tell application "iTunes"
   set s to item 1 of (selection)
   set pd to data of first artwork of s
end tell
set fs to ("" & (path to desktop) & "MyArt.pict") as file specification
set rn to (open for access fs with write permission)
try
   set eof rn to 512
   write pd to rn starting at 513
   close access rn
on error err_mess
   close access rn
   error err_mess
end try

tell application "Finder"
   set file type of fs to "PICT"
   set creator type of fs to "????"
   activate
end tell

property pngFile : "/Library/Application Support/Perceptive Automation/iTunes.png"
property targetSize : 125

tell application "Image Events"
   launch
   set theImageReference to open fs
   -- create the png file
   scale theImageReference to size targetSize
   save theImageReference as PNG in pngFile with icon
   close theImageReference
end tell


Any idea how that could be? Controlling iTunes from the Indigo iTunes server works fine.

Thanks,

Carl

Posted on
Mon Apr 02, 2012 2:11 pm
gtreece offline
Posts: 171
Joined: Sep 26, 2011

Re: Displaying iTunes Album Art

The (selection) in iTunes will not be the same as the current track. A song is selected when it's highlighted, but any song could be playing.

That's why I went with:

Code: Select all
set the_data to data of artwork 1 of current track

Posted on
Mon Apr 02, 2012 2:32 pm
Dewster35 offline
Posts: 1030
Joined: Jul 06, 2010
Location: Petoskey, MI

Re: Displaying iTunes Album Art

Just curious, is all this just to account for music that doesn't have album art in iTunes?

Posted on
Mon Apr 02, 2012 3:11 pm
gtreece offline
Posts: 171
Joined: Sep 26, 2011

Re: Displaying iTunes Album Art

Dewster35 wrote:
Just curious, is all this just to account for music that doesn't have album art in iTunes?


Not exactly. I just know that the first time I tested the script you posted originally, I got no artwork. I'd rather have a custom graphic show if there's no artwork, so I checked for it in the script I patched together.

Most of this has been working with Carl, to get a script running at all. Neither yours, nor the shorter one I did, worked for him.

Who is online

Users browsing this forum: No registered users and 3 guests