iTunes Playlist in Indigo 7

Posted on
Thu Aug 31, 2017 3:36 pm
Different Computers offline
User avatar
Posts: 1970
Joined: Jan 02, 2016
Location: East Coast

Re: iTunes Playlist in Indigo 7

Yup.

Looks like everything except playlist is reporting correctly.

SmartThings refugee, so happy to be on Indigo. 10.13.6 on an i5 MBP w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKit Bridge, MatplotLib, Plex, uniFAP, Wunderground, Nanoleaf, LED Simple Effects, Airfoil Pro, Grafana.

Posted on
Fri Sep 01, 2017 2:17 am
marketability offline
User avatar
Posts: 168
Joined: Dec 08, 2015
Location: UK

Re: iTunes Playlist in Indigo 7

incidentally - how are you viewing the custom states in the GUI?

Posted on
Fri Sep 01, 2017 7:51 am
Different Computers offline
User avatar
Posts: 1970
Joined: Jan 02, 2016
Location: East Coast

Re: iTunes Playlist in Indigo 7

marketability wrote:
incidentally - how are you viewing the custom states in the GUI?

You're asking me?

It has been a long time since I set them up, but first, I'm viewing it in various control pages. And I've created some variables that reflect the iTunes statuses. The short answer is I followed instructions found here in the thread about getting album art on a control page, and expanded them from there for things like volume control of various airplay devices, star ratings for tracks and such.

There's a way to grab and display playlists using the same method, or there was before iTunes made the API change.

The following AppleScript is NOT MINE, though any issues with it are certainly ones I've introduced. Sorry I don't recall who did the original, but all credit to him. Somebody on the forums here.
Code: Select all
-- configuration properties to set specific to your
-- own setup
property outputTempFN : "Library:Application Support:Perceptive Automation:Indigo 6:IndigoWebServer:images:iTunesCurrentAlbum.tiff"
property outputPNGFN : "Library:Application Support:Perceptive Automation:Indigo 6:IndigoWebServer:images:iTunesCurrentAlbum.png"
property noArtworkFN : "Library:Application Support:Perceptive Automation:Indigo 6:IndigoWebServer:images:iTunesNoArtworkAvailable.png"
property notPlayingFN : "Library:Application Support:Perceptive Automation:Indigo 6: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
         copy (get rating of current track) to trackRating
         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)
            -- THIS IS MIKE'S EDIT FOR TRACK RATING <===============================
            --Original locations copy (get rating of current track) to trackRating
            -- tell application "IndigoServer"
            -- set value of variable "iTunesStars" to trackRating
            -- end tell
            -- THIS IS MIKE'S EDIT FOR TRACK RATING <===============================
         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
      -- -----------this is Mike's edit for telling Indigo  <==================================
      tell application "IndigoServer"
         set value of variable "iTunesStars" to trackRating / 20
      end tell
   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

SmartThings refugee, so happy to be on Indigo. 10.13.6 on an i5 MBP w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKit Bridge, MatplotLib, Plex, uniFAP, Wunderground, Nanoleaf, LED Simple Effects, Airfoil Pro, Grafana.

Posted on
Fri Sep 01, 2017 9:00 am
jay (support) offline
Site Admin
User avatar
Posts: 16112
Joined: Mar 19, 2008
Location: Austin, Texas

Re: iTunes Playlist in Indigo 7

marketability wrote:
incidentally - how are you viewing the custom states in the GUI?


Do you mean in the Mac Client UI? Just select the device from the device list and use the split view handle (just below the list) to expand the detail section below the list to show the custom states tile.

If you're talking about viewing them on a custom control page, then just use the standard device state control page element and select the state.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Sep 01, 2017 11:16 am
marketability offline
User avatar
Posts: 168
Joined: Dec 08, 2015
Location: UK

Re: iTunes Playlist in Indigo 7

ha - magic!
two years into Indigo and you've made my day!!

Do you mean in the Mac Client UI? Just select the device from the device list and use the split view handle (just below the list) to expand the detail section below the list to show the custom states tile.

If you're talking about viewing them on a custom control page, then just use the standard device state control page element and select the state.


Different...
Thanks - thats an interesting approach - might well try it
I've got a similar result with some AppleScripts that detect whether itunes or spotify is playing and populate variables with the album art, media data, status, etc from the relevant playing source

Who is online

Users browsing this forum: No registered users and 1 guest