Displaying iTunes Album Art

Posted on
Thu Apr 07, 2016 12:33 am
gtreece offline
Posts: 171
Joined: Sep 26, 2011

Re: Displaying iTunes Album Art

Fishysan wrote:
Thanks for the info all.

Anyone have any luck displaying Apple Music artwork? The scripts here don't show data when you are listening to Apple Music "Radio Station" like Beats, or the featured stations. The iTunes app of course displays the album artwork for the track playing but the AppleScript doesn't get the artwork. The Indio trigger does trigger with a track change on radio stations..

Thanks!



For me, this code from above is grabbing artwork for the currently playing iTunes radio station:

Code: Select all
tell application "iTunes"
   set pd to data of first artwork of current track
end tell


To test this, I did a quick test script:

Code: Select all
property fs : "Users:gregtreece:Pictures:MyArt.tiff"

tell application "iTunes"
   set pd to data of first artwork of current track
end tell
set rn to (open for access fs with write permission)
try
   write pd to rn starting at 0
   close access rn
on error err_mess
   close access rn
   error err_mess
end try

tell application "Finder"
   set file type of (fs as alias) to "tiff"
end tell

property pngFile : "Users:gregtreece:Pictures:iTunes.png"
property targetSize : 256

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


The resulting iTunes.png file matches the current radio track.

Posted on
Thu Apr 07, 2016 7:36 am
Fishysan offline
Posts: 86
Joined: Feb 01, 2012

Re: Displaying iTunes Album Art

Good to hear Greg, thanks, I'll have a look tonight and report back.

Posted on
Fri Apr 08, 2016 8:00 am
Different Computers offline
User avatar
Posts: 2533
Joined: Jan 02, 2016
Location: East Coast

Re: Displaying iTunes Album Art

Nobody using iTunes on a separate computer from Indigo?

I can usually figure this stuff out on my own with search fu, but this is some pretty obscure stuff. So anyone who knows how to get the file locations out of a remote iTunes, please chime in.

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Posted on
Fri Apr 08, 2016 12:30 pm
gtreece offline
Posts: 171
Joined: Sep 26, 2011

Re: Displaying iTunes Album Art

Generally, to target a remote machine via applescript, it would be something like this:

Code: Select all
set remoteMachine to "eppc://username:password@remotemachine_ip"

using terms from application "iTunes"
   try
      tell application "iTunes" of machine remoteMachine
        <some code here>
      end tell
   end try
end using terms from


iTunes would need to already be running on the remote machine. I don't think this would launch it if it wasn't running.

disclaimer: I haven't tested this, and don't have need to use this method, so YMMV...

Posted on
Fri Apr 08, 2016 12:39 pm
Different Computers offline
User avatar
Posts: 2533
Joined: Jan 02, 2016
Location: East Coast

Re: Displaying iTunes Album Art

Thanks!

I was researching and found the same ideas: http://usetechnology.org/tag/remotely-c ... lescripts/

I'll try plugging these in to Indigo tonight.

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Posted on
Sat Apr 09, 2016 7:45 am
Different Computers offline
User avatar
Posts: 2533
Joined: Jan 02, 2016
Location: East Coast

Re: Displaying iTunes Album Art

I got this to compile on my iTunes Mac, with changing outputFile to a folder on my Indigo mac.

But when I run it manually, nothing happens. No file written, no error. Nothing. Is there some logic testing for something besides a playing song?

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Posted on
Sat Apr 09, 2016 9:10 am
Different Computers offline
User avatar
Posts: 2533
Joined: Jan 02, 2016
Location: East Coast

I think I'm stuck

With Apple's current blocking of remote script execution, I can either have the script running right on my iTunes Mac, or have it triggering with the right timing on the Indigo mac.

Anyone got an idea? My searches are missing a way to have iTunes tell the script that there's a new track playing.

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Posted on
Sat Apr 09, 2016 9:52 am
gtreece offline
Posts: 171
Joined: Sep 26, 2011

Re: Displaying iTunes Album Art

I'm not quite sure what you're referring to with apple blocking remote scripting.

I took the original script above that I tested locally on my Indigo Mac, and compiled it on my desktop Mac. I applied the modification for a remote machine, and put in the IP and credentials of my Indigo Mac. So I was running it from my desktop Mac, but directing it to my Indigo Mac.

On my Indigo Mac (the remote machine), I made sure that "remote apple events'" was checked in the sharing preference pane.

The script executed correctly, and retrieved the artwork from the currently playing iTunes radio track on my Indigo Mac, into a local file on my desktop Mac.

That proved it worked. In your scenario, it would be the opposite it sounds like. You'd compile the script on the Indigo Mac, and target some other remote computer (be sure to have "remote apple events" checked on that remote computer), to have Indigo pull the artwork directly from the remote machine.

That still leaves you how does Indigo know that the track changed on the remote machine. I have had poor results running applescripts outside of Indigo lately, but in theory, you could have a script running on your iTunes Mac, that updated a variable on your Indigo Mac. When Indigo saw a change in that variable, it could run the script to go get the new artwork. This would require that you install the Indigo client on your iTunes Mac, so that Applescript knew about it's dictionary.

I am one who has had a lot of trouble targeting the IndigoServer application from scripts that are not embedded in Indigo. There are several forums where this issue is discussed, and it seems to be some sort of bug with OS X that remains unfixed.

There may be some options for file monitoring from Indigo that I'm not familiar with.

In my case, I have a variable that tracks the state of iTunes (and SiriusXM), as either playing or paused. Whenever they are playing, I have a schedule set that runs an artwork script every 12 seconds. So the worst case is that I'm 12 seconds into a new track before I see updated artwork. That is plenty fast enough for my needs, as I'm never just staring at a control page looking at the artwork.

Posted on
Sat Apr 09, 2016 8:26 pm
Different Computers offline
User avatar
Posts: 2533
Joined: Jan 02, 2016
Location: East Coast

Re: Displaying iTunes Album Art

>I'm not quite sure what you're referring to with apple blocking remote scripting.

Imagine I've made your cool applescript into an Application called "Album Art for Indigo"

Apple specifically disallows sending something like

Code: Select all
tell application "Album Art for Indigo" of machine "eppc://user:pass@remote.local"  to open

If I try to compile this I get "Application isn't running" as the error

So, because of the iTunes plugin, Indigo knows when a track changes, and can trigger Album Art for Indigo, but NOT on a remote computer.

I'm trying to find the applescript code that will let me run it entirely locally on my iTunes mac, but have yet to figure out how to tell the script to run without Indigo being involved.

I really don't care which computer any particular script runs on--I've tried it from the Indigo mac, but there are too many awkward property changes/path changes, changes of all kinds for me to figure out how to get it to even compile. (Also apparently I'm missing something in application names vs bundles, because I CANNOT get the script to run with references to "com.apple.iTunes" and have to instead use "iTunes".)

When I mount an outputFolder that's on the Indigo mac on the iTunes Mac, everything seems hunky dory, as I mentioned: The script compiles perfectly, runs without complaint... BUT it doesn't actually DO anything. No artwork ever appears in outputFolder. Also, I have to run it manually, because I have no way I'e yet figured to trigger it locally based on the behavior of iTunes.

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Posted on
Sat Apr 09, 2016 9:12 pm
gtreece offline
Posts: 171
Joined: Sep 26, 2011

Re: Displaying iTunes Album Art

Ah, I'm following along now. Applescripts by default, are single event. Whether just compiled as a script, or compiled as an application, the script runs one time and stops.

You can make an Applescript application that stays open, if you specify that option when you save it as an application. This is closer to an 'application', but still, the script within it will only run one time, unless you construct it specifically to do otherwise.

There is an applescript handler called 'idle', that when used in conjunction with a 'stay open' applescript application, will allow your script to run on timed intervals.

Code: Select all
on idle
    beep
    return 5
end idle


If that code were contained in a stay open applescript, it would stay open when you launched it, and it would beep every 5 seconds.

That solution, to me, is not different, but harder to implement, than the approach I'm using of an Indigo schedule to run every x seconds to update the album art. The fact that your iTunes is not on your Indigo Mac, is preventing you from taking advantage of Indigo's built-in iTunes support.

So a question, why not run iTunes on your Indigo Mac? You could share your local music library if necessary. Seems like that would help you out.

Beyond that, maybe something like EventScripts would help you out:
http://www.mousedown.net/mouseware/EventScripts.html

Posted on
Sun Apr 10, 2016 7:59 am
Different Computers offline
User avatar
Posts: 2533
Joined: Jan 02, 2016
Location: East Coast

Re: Displaying iTunes Album Art

Your point about a local library is well taken. I had moved my main iTunes while still using SmartThings because I was getting inconsistent Airplay streaming from the Mini in my office--the very one I decided to install Indigo on. And it seems unwise to put Indigo on my AV Mini in the TV room.

Might be worth trying in the office again. All sorts of things have improves since I got rid of SmartThings. Maybe that will too!

Many thanks for your thoughtful replies. You've written a hell of a script!

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Posted on
Tue Dec 06, 2016 7:28 am
Sharek326 offline
User avatar
Posts: 377
Joined: Jul 20, 2014
Location: Lansford, PA

Re: Displaying iTunes Album Art

Has anyone that uses this code had any problems since I7 update? I made the needed changes to point to I7 instead of I6 but while my control page shows cover art it does not fire or change with the song. Even if I fire it manually it never changes. Seemed to be working fine prior to I7. Am I missing anything?



Korey wrote:
Different Computers wrote:
I'm probably way ahead of myself on this, since this is day 2 of the free trial for me, but could someone point me at the documentation for attaching applescripts to control pages?


This is what I use:

Applescript code:


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
         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

Posted on
Tue Dec 06, 2016 8:15 am
Different Computers offline
User avatar
Posts: 2533
Joined: Jan 02, 2016
Location: East Coast

Re: Displaying iTunes Album Art

Mine works perfectly, but I don't recall now how I made changes, if any, to these scripts.

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Posted on
Tue Dec 06, 2016 8:17 am
Sharek326 offline
User avatar
Posts: 377
Joined: Jul 20, 2014
Location: Lansford, PA

Re: Displaying iTunes Album Art

I may have figured it out. I had to Download all the Album arts in iTunes again since I restored iTunes after a fresh install

Posted on
Sun Jan 29, 2017 11:22 am
Different Computers offline
User avatar
Posts: 2533
Joined: Jan 02, 2016
Location: East Coast

Re: Displaying iTunes Album Art

Just moved everything to a new computer running Sierra and now my script throws an error on compile or run:
Code: Select all
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
The error highlights the "tempImageRef" after scale on line 4 above and says "Expected end of line, etc. but found identifier." Any ideas? This script has been working well.

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Who is online

Users browsing this forum: No registered users and 3 guests