iTunes 12.7: not playing tracks properly?

Hi everyone,

I’m in the midst of writing an IFTTT action for Google Home that will trigger iTunes to play tracks by the requested artist. I’ve made good progress, but am now at the stage of writing an AppleScript to search iTunes for that specific artist and play one track after another. The problem is that when run, the following code, doesn’t wait for each song to finish (let alone start) playing. It just starts each song, then immediately goes on to the next one. Any ideas on if this is new behavior or if there is a way to change it (easily)…Thanks. Code is:

tell application “iTunes”
set playlistArtist to (every track of playlist “Library” whose artist is “Nirvana”)
repeat with i in playlistArtist
play i
end repeat
end tell

Here is something that seems to be close to what you are looking for. Substitute your artist name where I have “Your Artist.” It does leave a playlist in iTunes that would need to be deleted at a later time.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions


tell application "iTunes"
	make new user playlist with properties {name:"Temporary"}
	duplicate (every track of playlist "Library" whose artist is "Your Artist") to user playlist "Temporary"
	play user playlist "Temporary"
end tell

One more script that may give you some ideas. I’m sure someone experienced at AppleScript would be able to make it more compact and error proof. I have a feeling this could be error prone, particularly if a track was paused at any point in playback. Replace where I have “Your Artist” with the name of the artist you want. It does assume that tracks are less than 1 hour long.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "iTunes"
	set playlistArtist to (name of every track of playlist "Library" whose artist is "Your Artist")
	repeat with i from 1 to (count of playlistArtist)
		set theTrack to (item i of playlistArtist)
		play track theTrack
		set AppleScript's text item delimiters to {":"}
		set theTime to get time of track theTrack
		set theMinutes to get text item 1 of theTime
		set theSeconds to get text item 2 of theTime
		set totalSeconds to (theMinutes as integer) * 60 + (theSeconds as integer)
		delay totalSeconds + 2
	end repeat
end tell
set AppleScript's text item delimiters to {""}

Thanks for the help!

I only had a quick moment this weekend to try the second script. It mostly worked, but would jump to a different artist/song in the additional 2 seconds that the script adds to the length of the track. I’ll mess around it with it to see if I can get it to behave better.

Thanks again.

Tim

Tim,

Thanks for the feedback. I think that adding the 2 seconds to the delay time was probably a mistake. I did a short test with 2 artists and found that the script worked OK without the extra 2 seconds added to the delay. That said, I still think the script is probably error prone whereas the other script that creates a playlist is likely to perform more reliably. One other advantage of the playlist script is that it doesn’t require the script to continue running the entire time that the iTunes tracks are playing. Good luck with the IFTTT.

Take care,
Dana