Friday, July 30, 2010

#1 2006-11-07 06:30:54 am

Trash Man
Sanitation Department
Registered: 2005-10-20
Posts: 5336

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.


One mans trash is another mans treasure

Offline

 

#2 2006-11-07 06:50:06 am

StefanK
Member
From: Sankt Gallen, Switzerland
Registered: 2006-10-21
Posts: 7316
Website

Re: iTunes 'Restart Current Album' Script?

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


regards

Stefan

Filed under: iTunes

Offline

 

#3 2006-11-07 07:01:14 am

Trash Man
Sanitation Department
Registered: 2005-10-20
Posts: 5336

Re: iTunes 'Restart Current Album' Script?

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?


One mans trash is another mans treasure

Offline

 

#4 2006-11-07 08:56:38 am

StefanK
Member
From: Sankt Gallen, Switzerland
Registered: 2006-10-21
Posts: 7316
Website

Re: iTunes 'Restart Current Album' Script?

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


regards

Stefan

Offline

 

#5 2006-11-07 10:43:15 am

Trash Man
Sanitation Department
Registered: 2005-10-20
Posts: 5336

Re: iTunes 'Restart Current Album' Script?

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?


One mans trash is another mans treasure

Offline

 

#6 2006-11-07 11:17:16 am

StefanK
Member
From: Sankt Gallen, Switzerland
Registered: 2006-10-21
Posts: 7316
Website

Re: iTunes 'Restart Current Album' Script?

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


regards

Stefan

Filed under: iTunes

Offline

 

#7 2006-11-07 11:33:56 am

Trash Man
Sanitation Department
Registered: 2005-10-20
Posts: 5336

Re: iTunes 'Restart Current Album' Script?

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.


One mans trash is another mans treasure

Offline

 

#8 2006-11-07 12:41:46 pm

StefanK
Member
From: Sankt Gallen, Switzerland
Registered: 2006-10-21
Posts: 7316
Website

Re: iTunes 'Restart Current Album' Script?

you’re welcome.
It took just a few minutes to develop this amazing piece of art winkwink

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

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)


regards

Stefan

Filed under: iTunes, System

Offline

 

#9 2006-11-07 10:29:04 pm

Trash Man
Sanitation Department
Registered: 2005-10-20
Posts: 5336

Re: iTunes 'Restart Current Album' Script?

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)


One mans trash is another mans treasure

Offline

 

#10 2006-11-08 08:46:54 am

StefanK
Member
From: Sankt Gallen, Switzerland
Registered: 2006-10-21
Posts: 7316
Website

Re: iTunes 'Restart Current Album' Script?

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.


regards

Stefan

Offline

 

#11 2006-11-08 09:10:44 am

Trash Man
Sanitation Department
Registered: 2005-10-20
Posts: 5336

Re: iTunes 'Restart Current Album' Script?

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


One mans trash is another mans treasure

Offline

 

#12 2006-11-08 09:15:53 am

StefanK
Member
From: Sankt Gallen, Switzerland
Registered: 2006-10-21
Posts: 7316
Website

Re: iTunes 'Restart Current Album' Script?

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


regards

Stefan

Offline

 

#13 2006-11-08 09:25:23 am

Trash Man
Sanitation Department
Registered: 2005-10-20
Posts: 5336

Re: iTunes 'Restart Current Album' Script?

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.


One mans trash is another mans treasure

Offline

 

#14 2006-11-08 09:40:36 am

StefanK
Member
From: Sankt Gallen, Switzerland
Registered: 2006-10-21
Posts: 7316
Website

Re: iTunes 'Restart Current Album' Script?

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


regards

Stefan

Offline

 

#15 2006-11-08 09:57:25 am

kel
Member
Registered: 2002-11-19
Posts: 2031

Re: iTunes 'Restart Current Album' Script?

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,


Kel

Offline

 

#16 2006-11-08 10:15:57 am

StefanK
Member
From: Sankt Gallen, Switzerland
Registered: 2006-10-21
Posts: 7316
Website

Re: iTunes 'Restart Current Album' Script?

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


regards

Stefan

Filed under: iTunes

Offline

 

#17 2006-11-08 10:25:37 am

Trash Man
Sanitation Department
Registered: 2005-10-20
Posts: 5336

Re: iTunes 'Restart Current Album' Script?

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.


One mans trash is another mans treasure

Offline

 

#18 2006-11-08 10:53:54 am

kel
Member
Registered: 2002-11-19
Posts: 2031

Re: iTunes 'Restart Current Album' Script?

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,


Kel

Offline

 

#19 2006-11-08 09:49:30 pm

Trash Man
Sanitation Department
Registered: 2005-10-20
Posts: 5336

Re: iTunes 'Restart Current Album' Script?

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


One mans trash is another mans treasure

Offline

 

#20 2006-11-09 12:11:32 am

kel
Member
Registered: 2002-11-19
Posts: 2031

Re: iTunes 'Restart Current Album' Script?

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)


Kel

Filed under: iTunes

Offline

 

#21 2006-11-09 06:21:26 am

Trash Man
Sanitation Department
Registered: 2005-10-20
Posts: 5336

Re: iTunes 'Restart Current Album' Script?

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


One mans trash is another mans treasure

Offline

 

#22 2006-11-09 07:15:11 am

kel
Member
Registered: 2002-11-19
Posts: 2031

Re: iTunes 'Restart Current Album' Script?

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)


Kel

Offline

 

#23 2006-11-09 07:42:03 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 1969

Re: iTunes 'Restart Current Album' Script?

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

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)


NG

Filed under: iTunes

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.582 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)