You are not logged in.
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.
Offline
do you mean something like this?
Applescript:
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
Offline
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?
Offline
second hiccup is resolvable with this line, which won’t add unchecked tracks to the new playlist.
Applescript:
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
Offline
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?
Offline
selecting the album via the drilldown (Genre > Artist > Album) browser
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.
Applescript:
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
Offline
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.
Offline
you’re welcome.
It took just a few minutes to develop this amazing piece of art ![]()
![]()
[Edit] I’ve tried to click the current cover with this GUI script (to select the first track) but it failed ![]()
Applescript:
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
Last edited by StefanK (2006-11-07 01:31:52 pm)
Offline
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?
Last edited by WCityMike (2006-11-07 10:29:27 pm)
Offline
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.
Offline
Yup. They have track numbers; the way I sort them (since they arrive in an unsorted state) is by clicking the appropriate column.
Offline
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
Offline
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.
Offline
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,
Offline
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
Applescript:
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
Offline
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.
Offline
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,
Offline
It seems to be exactly what I was looking for ... I'm quite grateful! Thank you!
Offline
Yeah, when you set things up it works faster. Something like this:
Edited: forgot to use the applescript tags
Applescript:
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,
Last edited by kel (2006-11-09 12:31:09 am)
Offline
Actually, StefanK's script seems to require no manual setup.
Offline
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,
Last edited by kel (2006-11-09 07:19:59 am)
Offline
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. ![]()
Applescript:
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!
Last edited by Nigel Garvey (2006-11-09 08:54:15 am)
Offline