Get iTunes Album Art

Posted on
Wed Apr 03, 2019 11:53 am
jltnol offline
Posts: 989
Joined: Oct 15, 2013

Get iTunes Album Art

Soooo I'm guessing this may have been asked, and probably answered, but I did search and couldn't come up with anything.

Is it possible for retrieve Album Art and have it displayed on a Control Page?

I'm guessing the best way would be to invoke the "refreshing image url" ... I can see how to set up a trigger when there is an iTunes track change, but I'm guessing some level of scripting would be needed to get the art, and then past it into a local web page so Indigo Pages could retrieve it.

Any suggestions ?

Posted on
Wed Apr 03, 2019 4:27 pm
Korey offline
User avatar
Posts: 811
Joined: Jun 04, 2008
Location: Henderson, NV

Re: Get iTunes Album Art

This is the Applescript I used to use before I changed to using Airfoil (Not sure if it will still work, but worth a shot)

Set it up as a Trigger when iTunes has any change to a Track, it fires this script.

It will needed to be updated to Python in the future, but the wizards here can help with that.


Code: Select all
-- configuration properties to set specific to your
-- own setup
property outputTempFN : "Library:Application Support:Perceptive Automation:Indigo 7.2:IndigoWebServer:images:iTunesCurrentAlbum.tiff"
property outputPNGFN : "Library:Application Support:Perceptive Automation:Indigo 7.2:IndigoWebServer:images:iTunesCurrentAlbum.png"
property noArtworkFN : "Library:Application Support:Perceptive Automation:Indigo 7.2:IndigoWebServer:images:iTunesNoArtworkAvailable.png"
property notPlayingFN : "Library:Application Support:Perceptive Automation:Indigo 7.2:IndigoWebServer:images:iTunesNotPlaying.png"
property targetSize : 256

-- main run routine
on run
   -- checks to ensure that itunes is running and available
   if my checkItunesIsActive() is false then
      -- uncomment below to show an error or add your own logging
      -- set opt to (display dialog "iTunes is not running." buttons {"OK"} default button 1 with title "Cannot proceed..." with icon 0 giving up after 30)
      my outputPlaceholderImage(notPlayingFN)
      return false
   end if
   
   -- ensure that itunes will respond, apparently some version will not if a modal dialog
   -- window is showing
   if my itunesIsNotAccesible() is true then
      -- uncomment below to show an error or add your own logging
      -- set opt to (display dialog "Close any utility windows that may be open in iTunes." buttons {"OK"} default button 1 with title "Cannot proceed..." with icon 0 giving up after 30)
      my outputPlaceholderImage(notPlayingFN)
      return false
   end if
   
   -- itunes should be available… attempt to process now
   tell application id "com.apple.iTunes"
      if (player state is playing) or (player state is paused) then
         if (artworks of current track exists) then
            -- the current track has artwork available, attempt to output
            -- that to a PNG now
            set artwork_data to (get raw data of first artwork of current track)
            my outputAlbumArtwork(artwork_data)
         else
            -- export the "no artwork available" file to the output
            my outputPlaceholderImage(noArtworkFN)
         end if
      else
         -- export the "not playing" artwork file to the output
         my outputPlaceholderImage(notPlayingFN)
      end if
   end tell
end run

-- this routine checks to see if iTunes is running
to checkItunesIsActive()
   tell application "System Events" to return (exists (some process whose name is "iTunes"))
end checkItunesIsActive

-- this routine ensures that itunes is responding to applescript commands
to itunesIsNotAccesible()
   try
      with timeout of 1 second
         tell application id "com.apple.iTunes" to get name of library playlist 1
      end timeout
   on error
      return true
   end try
   return false
end itunesIsNotAccesible

-- this routine will output the artwork data provided to the artwork
-- file specified in the properties at top
to outputAlbumArtwork(artwork_data)
   set tempFileHandle to (open for access outputTempFN with write permission)
   try
      tell application "System Events"
         write artwork_data to tempFileHandle starting at 0
         set file type of (outputTempFN as alias) to ".tiff"
      end tell
      close access tempFileHandle
      
      tell application "Image Events"
         launch
         set tempImageRef to open outputTempFN
         scale tempImageRef to size targetSize
         save tempImageRef as PNG in outputPNGFN with icon
         close tempImageRef
      end tell
   on error err_msg
      close access tempFileHandle
      -- log error message if desired
      -- set opt to (display dialog "Error: " & err_msg buttons {"OK"} default button 1 with title "Cannot proceed..." with icon 0 giving up after 30)
   end try
end outputAlbumArtwork

-- this rouitne will output the given filename, assumed to already be a PNG,
-- to the output filename defined in the properties up top
to outputPlaceholderImage(filename)
   set outputFileHandle to (open for access (outputPNGFN as alias) with write permission)
   try
      write (read (filename as alias)) to outputFileHandle starting at 0
      close access outputFileHandle
   on error err_msg
      close access outputFileHandle
      -- log error message if desired
      -- set opt to (display dialog "Error: " & err_msg buttons {"OK"} default button 1 with title "Cannot proceed..." with icon 0 giving up after 30)
   end try
end outputPlaceholderImage

--
Korey

Posted on
Wed Apr 03, 2019 8:01 pm
jltnol offline
Posts: 989
Joined: Oct 15, 2013

Re: Get iTunes Album Art

I. am. floored. This is amazing.

I've got everything up and running, and can actually see both the tiff and the png images in the Library->Perceptive...... folder, but didn't see a way to put those into a Control Page. As far as I can tell, the only way to get images in a Control Page is if they are in the control/devices or the control/static directories.

I'm guessing I'm wrong in this assumption, but did create two separate scripts and changed the path for the images on one to the devices folder, and one to the static folder. So now, I have the right art in both folders, but while I can set up a Static Image and a Device State on the Control Page, only the static image shows up in the browser, but the web page doesn't refresh with a track change, I have to manually refresh the page. BUT, I can almost easily see a way to create an html page and go the "refreshing image url" route. But before I struggle thru with that, I'm wondering if there is a better way.. :)


And later:

Sooooo Yes, I Am An IDIOT.

Finally got the refreshing URL "file:///........" option and the refresh rate figured out, and THAT solved the problem!!

Posted on
Wed Apr 03, 2019 9:07 pm
Korey offline
User avatar
Posts: 811
Joined: Jun 04, 2008
Location: Henderson, NV

Re: Get iTunes Album Art

yep that's the way, refreshing image :D :D

Glad it's working for you!!

file:///Library/Application Support/Perceptive Automation/Indigo 7.2/IndigoWebServer/images/albumArt.png

is my path, 4 second refresh.

--
Korey

Posted on
Thu Apr 04, 2019 2:03 pm
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Get iTunes Album Art

The good news is that this script will continue to work even after we deprecate direct AppleScript communication support because it doesn't directly talk to the IndigoServer.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Dec 16, 2019 10:42 am
jalves offline
Posts: 744
Joined: Jun 16, 2013

Re: Get iTunes Album Art

I was going to use the script above but note that it refers to a hard-coded directory, specific to Indigo 7.2. I'm assuming I can just change that to 7.4. Right?? Or would it be better to change a more generic Perceptive Automation folder so it works no matter what the Indigo version?

Running Indigo 2023.2 on a 24" iMac M1), OS X 14.4
Jeff

Posted on
Mon Dec 16, 2019 4:35 pm
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Get iTunes Album Art

Without looking too closely, I'm guessing that the path has to be correct. Changing it to 7.4 should work.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Dec 18, 2019 11:01 am
jalves offline
Posts: 744
Joined: Jun 16, 2013

Re: Get iTunes Album Art

I gave this a shot, but I'm missing something somewhere.
I copied this script and changed all four instances of 7.2 to 7.4. I also changed all references to "iTunes" to "Music". Saved the script in the appropriate spot in the library. I then created a trigger called "Album Art Change" which triggers on device state change on the music (Indigo device, not the app) Current Track. Went to the Actions tab and selected the scripts the desired action. However, when I try to run the script, I get no result.

Code: Select all
-- configuration properties to set specific to your
-- own setup
property outputTempFN : "Library:Application Support:Perceptive Automation:Indigo 7.4:IndigoWebServer:images:MusicCurrentAlbum.tiff"
property outputPNGFN : "Library:Application Support:Perceptive Automation:Indigo 7.4:IndigoWebServer:images:MusicCurrentAlbum.png"
property noArtworkFN : "Library:Application Support:Perceptive Automation:Indigo 7.4:IndigoWebServer:images:MusicNoArtworkAvailable.png"
property notPlayingFN : "Library:Application Support:Perceptive Automation:Indigo 7.4:IndigoWebServer:images:MusicNotPlaying.png"
property targetSize : 256

-- main run routine
on run
   -- checks to ensure that Music is running and available
   if my checkMusicIsActive() is false then
      -- uncomment below to show an error or add your own logging
      -- set opt to (display dialog "Music is not running." buttons {"OK"} default button 1 with title "Cannot proceed..." with icon 0 giving up after 30)
      my outputPlaceholderImage(notPlayingFN)
      return false
   end if
   
   -- ensure that Music will respond, apparently some version will not if a modal dialog
   -- window is showing
   if my MusicIsNotAccesible() is true then
      -- uncomment below to show an error or add your own logging
      -- set opt to (display dialog "Close any utility windows that may be open in Music." buttons {"OK"} default button 1 with title "Cannot proceed..." with icon 0 giving up after 30)
      my outputPlaceholderImage(notPlayingFN)
      return false
   end if
   
   -- Music should be available… attempt to process now
   tell application id "com.apple.Music"
      if (player state is playing) or (player state is paused) then
         if (artworks of current track exists) then
            -- the current track has artwork available, attempt to output
            -- that to a PNG now
            set artwork_data to (get raw data of first artwork of current track)
            my outputAlbumArtwork(artwork_data)
         else
            -- export the "no artwork available" file to the output
            my outputPlaceholderImage(noArtworkFN)
         end if
      else
         -- export the "not playing" artwork file to the output
         my outputPlaceholderImage(notPlayingFN)
      end if
   end tell
end run

-- this routine checks to see if Music is running
to checkMusicIsActive()
   tell application "System Events" to return (exists (some process whose name is "Music"))
end checkMusicIsActive

-- this routine ensures that Music is responding to applescript commands
to MusicIsNotAccesible()
   try
      with timeout of 1 second
         tell application id "com.apple.Music" to get name of library playlist 1
      end timeout
   on error
      return true
   end try
   return false
end MusicIsNotAccesible

-- this routine will output the artwork data provided to the artwork
-- file specified in the properties at top
to outputAlbumArtwork(artwork_data)
   set tempFileHandle to (open for access outputTempFN with write permission)
   try
      tell application "System Events"
         write artwork_data to tempFileHandle starting at 0
         set file type of (outputTempFN as alias) to ".tiff"
      end tell
      close access tempFileHandle
      
      tell application "Image Events"
         launch
         set tempImageRef to open outputTempFN
         scale tempImageRef to size targetSize
         save tempImageRef as PNG in outputPNGFN with icon
         close tempImageRef
      end tell
   on error err_msg
      close access tempFileHandle
      -- log error message if desired
      -- set opt to (display dialog "Error: " & err_msg buttons {"OK"} default button 1 with title "Cannot proceed..." with icon 0 giving up after 30)
   end try
end outputAlbumArtwork

-- this rouitne will output the given filename, assumed to already be a PNG,
-- to the output filename defined in the properties up top
to outputPlaceholderImage(filename)
   set outputFileHandle to (open for access (outputPNGFN as alias) with write permission)
   try
      write (read (filename as alias)) to outputFileHandle starting at 0
      close access outputFileHandle
   on error err_msg
      close access outputFileHandle
      -- log error message if desired
      -- set opt to (display dialog "Error: " & err_msg buttons {"OK"} default button 1 with title "Cannot proceed..." with icon 0 giving up after 30)
   end try
end outputPlaceholderImage

Running Indigo 2023.2 on a 24" iMac M1), OS X 14.4
Jeff

Posted on
Wed Dec 18, 2019 11:53 am
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Get iTunes Album Art

You might want to add some debug display dialog lines to try to see where it's failing (run it from Script Editor so you'll see the dialogs).

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Dec 19, 2019 8:19 am
jalves offline
Posts: 744
Joined: Jun 16, 2013

Re: Get iTunes Album Art

jay (support) wrote:
You might want to add some debug display dialog lines to try to see where it's failing (run it from Script Editor so you'll see the dialogs).

Thanks for the suggestion Jay. The script had a few error reporting lines commented out. I un-commented them and then ran the script. It showed a security dialog that was apparently keeping it from operating correctly before. I granted the needed access and everything seems to be working properly now!

Running Indigo 2023.2 on a 24" iMac M1), OS X 14.4
Jeff

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests