iTunes 'Restart Current Album' Script?

I’m curious as to whether anyone’s ever programmed an AppleScript along these lines: a script that notes the album of the current playing track, creates a playlist containing all tracks in the library with the same album, and starts playing that playlist from track no. 1 of the album … in essence, if you hear a song from an album and decide you want to listen to the whole album, facilitating that.

Anyone ever throw that together? It seems like such a common idea, but I couldn’t find it on Doug’s AppleScripts for iTunes or anywhere else.

do you mean something like this?

tell application "iTunes"
	if player state is playing then
		set theAlbum to album of current track
		set AlbumName to "Album " & theAlbum
		make new playlist with properties {name:AlbumName}
		duplicate (every track of playlist "Library" whose album is theAlbum) to playlist AlbumName
		play playlist AlbumName
	end if
end tell

Very close … I gave it a test run with what I’ve got, and it looks like there are two hiccups. First, ideally, the idea of this script would be to play the whole album in original track order; second, it seems to choke if the tracks have been unchecked from play. Any thoughts?

second hiccup is resolvable with this line, which won’t add unchecked tracks to the new playlist.

duplicate (every track of playlist "Library" whose album is theAlbum and enabled is true) to playlist AlbumName

I’m wondering about the tracks are not in the right order.
Normally albums are imported in consecutive order and then
the tracks appear correctly in the created playlist

I shall try that change when I get home. As for the improper order concern — well, basically, I can only speak from live experience — I tried to import an album called “Glorious” (a comedy album by Eddie Izzard), and although it did create a new playlist, the tracks were not in track-no. order.

Also, I’ve been wondering in the back of my mind, an alternate route towards the same end which I wonder whether is scriptable – selecting the album via the drilldown (Genre > Artist > Album) browser, as opposed to creating a playlist for it? Whaddya think?

I don’t understand exactly what this means.
It’s not possible to set the selection property in iTunes via AppleScript
You can only select the tracks manually.

Here is a modified script which uses a fixed playlist named “Album to play”
If this playlist already exists, all tracks of it will be deleted before copying the new tracks,
otherwise the playlist will be created.

tell application "iTunes"
	if player state is playing then
		set theAlbum to album of current track
		set AlbumName to "Album to play"
		if playlist AlbumName exists then
			delete every track of playlist AlbumName
		else
			make new playlist with properties {name:AlbumName}
		end if
		duplicate (every track of playlist "Library" whose album is theAlbum and enabled is true) to playlist AlbumName
		play playlist AlbumName
	end if
end tell

The order of the tracks should be displayed correctly, when the tags (track number and album) of the tracks are set properly

Thank you, Stefan — I shall give the script a test run tonight when I’m back at my Mac, and let you know of any bugs. I appreciate the assistance and the work on your part very much; please let me know if I can reciprocate in any way.

The “drilldown menu” I so inartfully spoke of was in fact the interface brought up by the Browse button.

you’re welcome.
It took just a few minutes to develop this amazing piece of art ;):wink:

[Edit] I’ve tried to click the current cover with this GUI script (to select the first track) but it failed :frowning:

tell application "iTunes" to activate
tell application "System Events"
	tell process "iTunes"
		tell window 1
			tell UI element 1 of UI element 7 of UI element 1 of UI element 1
				set {x, y} to position
				set {xSize, ySize} to size
				click at {x + xSize / 2, y + ySize / 2}
			end tell
		end tell
	end tell
end tell

Stefan, I much like the current version — but it does have that same problem where the resulting created playlist is not in “track no.” order.

Perhaps our libraries are different somehow?

Are you sure, that your mp3-tags of these files are set properly?
You can figure this out either in the library window with view option “track number”
or in the information window of the tracks.

If you import a CD and there is no entry in the CDDB, the tracks will be imported
with no tags at all, then you should add them manually.

Yup. They have track numbers; the way I sort them (since they arrive in an unsorted state) is by clicking the appropriate column.

very strange. Normally if you have both fields tracks [x] of [y] filled out and also
the album name of the tracks is the same, the order is displayed correctly in the “sort by album” view

Ahhhh … wait a minute. “Sort by album” view. When my thing comes up it’s in … well, not sure what the name of it is. “Traditional view”? “List view”? Basically, the way all track listings appeared pre-iTunes-7.

In Pre-iTunes-7 I mean the sort by album view when you click in the album headline

Hi,

If shuffle is on, then the tracks probably won’t be in order. Also, If you don’t have the sort by artist or album, then there’s no guarantee of correct order.

gl,

Mike,

this is an extended version of the script above.
If there are track numbers in unsorted order
then a sorting routine will be called

tell application "iTunes"
	if player state is playing then
		set {theAlbum, discNumber} to {album, disc number} of current track
		set theTracks to every track of playlist "Library" whose album is theAlbum and disc number is discNumber and enabled is true
		
		set TrackNum to {}
		set flag to true
		repeat with i from 1 to count theTracks
			if track number of item i of theTracks = 0 then exit repeat
			if track number of item i of theTracks is not equal to i then set flag to false
			set end of TrackNum to track number of item i of theTracks
		end repeat
		
		if (count TrackNum) = (count theTracks) and flag = false then
			my sort_items(TrackNum, theTracks)
		end if
		set AlbumName to "Album to play"
		
		if playlist AlbumName exists then
			delete every track of playlist AlbumName
		else
			make new playlist with properties {name:AlbumName}
		end if
		repeat with i in theTracks
			duplicate i to playlist AlbumName
		end repeat
		play playlist AlbumName
	end if
end tell

to sort_items(sortList, SecondList)
	script L
		property srt : sortList
		property sec : SecondList
	end script
	tell (count L's srt) to repeat with i from (it - 1) to 1 by -1
		set s to (L's srt)'s item i
		set r to (L's sec)'s item i
		repeat with i from (i + 1) to it
			tell (L's srt)'s item i to if s > it then
				set (L's srt)'s item (i - 1) to it
				set (L's sec)'s item (i - 1) to (L's sec)'s item i
			else
				set (L's srt)'s item (i - 1) to s
				set (L's sec)'s item (i - 1) to r
				exit repeat
			end if
		end repeat
		if it is i and s > (L's srt)'s end then
			set (L's srt)'s item it to s
			set (L's sec)'s item it to r
		end if
	end repeat
end sort_items

Thank you for doing that — I’m unfortunately unable to test it here at work but will eagerly try it out when I return home late this evening.

What I do sometimes is set up a playlist or smart playlist to do something. In this case, you could create a playlist “Current Album” and set the view options to show track numbers. Select the sort order by track numbers (the column header). Now, when you add tracks to the playlist, it doesn’t matter what order they are added when you play the playlist it will play by track number.

gl,

It seems to be exactly what I was looking for … I’m quite grateful! Thank you!

Yeah, when you set things up it works faster. Something like this:

Edited: forgot to use the applescript tags


tell application "iTunes"
	activate
	try
		current track
	on error -- no track targeted
		return
	end try
	if not (exists playlist "Current Album") then
		display dialog "You must create a playlist named \"Current Album\" and sort by track number."
		return
	end if
	tell current track
		set {cur_artist, cur_album} to {artist, album}
	end tell
	delete every track of playlist "Current Album"
	duplicate (every track of first library playlist whose ¬
		artist is cur_artist and album is cur_album) to playlist "Current Album"
	set bw to first browser window
	set view of bw to playlist "Current Album"
	play playlist "Current Album"
end tell

gl,