iTunes 'Restart Current Album' Script?

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,

Actually, StefanK’s script seems to require no manual setup.

Hi Mike,

That’s right. It’s your choice what to do. I was just suggesting a different way for speed. When I tried sorting earlier, it was very slow. I didn’t make timing tests, but it seems to me that iTunes could sort it faster. After all, it sorts the whole library very quickly.

gl,

Hi.

This version doesn’t do a full sort, but gets the highest track applicable track number and tries all the numbers from 0 to there. It’s quite fast on my machine, but I suspect that might depend on the size of your playlist library.

Script edited after unconscious assumptions noticed in the original. :rolleyes:

on showError(msg)
	tell application (path to frontmost application as Unicode text)
		display dialog msg buttons "Cancel" default button 1 with icon stop
	end tell
	error number -128
end showError

tell application "iTunes"
	activate
	if (player state is in {playing, paused}) then
		set {cur_artist, cur_album} to current track's {artist, album}
		if ((count cur_album) is 0) then my showError("This track has no album!")
		set trackNumbers to track number of tracks of library playlist 1 whose album is cur_album and artist is cur_artist
		set max to item 1 of trackNumbers
		repeat with i from 2 to (count trackNumbers)
			if (item i of trackNumbers > max) then set max to item i of trackNumbers
		end repeat
		--if (max > 0) then
		if (playlist "Current Album" exists) then delete playlist "Current Album" -- with all its settings.
		set albumPlaylist to (make new playlist with properties {name:"Current Album"})
		repeat with i from 0 to max
			try
				duplicate (first track of library playlist 1 whose album is cur_album and artist is cur_artist and track number is i) to albumPlaylist
			end try
		end repeat
		set view of browser window 1 to albumPlaylist
		play albumPlaylist
		--end if
	end if
end tell

This assumes, of course, that “Current Album” isn’t one of the user’s own playlist names!