Cycle thru iTunes Playlists [next playlist command]

Posted on
Tue Aug 27, 2013 3:04 pm
CraigM offline
Posts: 589
Joined: Oct 28, 2007

Cycle thru iTunes Playlists [next playlist command]

Interested in how others are using iTunes.
1a) Do you have iTunes always open and paused? [I'm concerned that being in constant pause might hang iTunes]
1b) Do you use a script to launch iTunes/play, then stop/quit iTunes?

The iTunes plugin doesn't seem to be able to select/control a particular playlist. It just plays from the entire MUSIC library. So I need a way to get to playlists.
2) Applescript: set shuffle prefs and playlist. I use this now and it works fine if my action requires just 1 exact playlist every time.

What I'm looking for now is 1 CP element button that cycles thru my playlists. I'll know when I've selected the right one because the plugin shows Current Playlist Name. I don't want a button for each playlist name.

Is this do-able?
Last edited by CraigM on Fri Aug 30, 2013 4:38 pm, edited 1 time in total.

Posted on
Tue Aug 27, 2013 3:20 pm
durosity offline
User avatar
Posts: 4320
Joined: May 10, 2012
Location: Newcastle Upon Tyne, Ye Ol' England.

Re: Cycle thru Playlists

1a - I have my iTunes open 24/7 on my server.. it has a 12TB or so sized library and has been running non stop now for 57 days. In fact the only time it closes is for updates or server maintenance/upgrades. Hasn’t ever hung to the best of my knowledge.
1b - I have a script to stop iTunes if somethings been paused for a long time, but i’ve disabled it since i never had any issues.

2 - I’m not sure about cycling playlists.. but it seems likely it’s possible. Have a look at http://dougscripts.com to see if there’s anything you could use.

Computer says no.

Posted on
Tue Aug 27, 2013 10:07 pm
RogueProeliator offline
User avatar
Posts: 2501
Joined: Nov 13, 2012
Location: Baton Rouge, LA

Re: Cycle thru Playlists

iTunes is open all the time for me as well... no problems encountered related to iTunes at all. As with the previous poster, it closes (restarts) for updates, but has been up at least a month or longer at a time for sure without problems.

I have buttons for selecting play lists... there may be other ways that people will chime in, but running an action of Execute Script -> AppleScript works:

Code: Select all
tell application "iTunes"
     play playlist "Top 100 Most Played"
end tell


If you want to get the list of all playlists, you can then find the current one and go to the next (of course, be sure if at the last item to wrap around to the next. I haven't done this myself, but you can get the list of all playlists in the system with:

Code: Select all
tell application "iTunes"
   set all_Playlist_Names to the name of every playlist
end tell


It does, however, include the built-in lists ("Recently Added", "Recently Played"). The list if finite if you want to exclude them or could possibly get more info on it to see if it is user-created.

I know that isn't the best idea/solution, but might help...
Adam

Posted on
Wed Aug 28, 2013 1:14 pm
CraigM offline
Posts: 589
Joined: Oct 28, 2007

Re: Cycle thru Playlists

Puzzling why Apple didn't include Next Playlist as a command, to me it's just as important as Next Track. Even my iPod Shuffle has a button sequence to cycle through, and select playlists.

Lets hear from the scripting experts out there.

(Option 1)
- Each click of a CP button cycles you through YOUR DEFINED playlists. Jazz>Rock>Relax>Jazz.
- Should act like the Fan controls cycle through speeds.

(Option 2)
- Can we name the actual playlists in a script, and tell it that each click should advance to the next playlist?

onClick play playlist "Jazz"
onClick play playlist "Rock"
onClick play playlist "Relax"
onClick play playlist "Jazz"

Maybe with triggers, conditions or variables you could get it to advance to a pre-defined playlist?

ifCurrentPlaylist is "Jazz" play playlist "Rock"
ifCurrentPlaylist is "Rock" play playlist "Relax"
ifCurrentPlaylist is "Relax" play playlist "Jazz"

Posted on
Fri Aug 30, 2013 4:56 pm
CraigM offline
Posts: 589
Joined: Oct 28, 2007

Re: Cycle thru iTunes Playlists [next playlist command]

Solved! One CP button that cycles you through your iTunes playlists.

Code: Select all
tell application "iTunes"
   if (name of view of window "iTunes" is "Jazz") then
      reveal playlist "Rock"
      play playlist "Rock"
      
   else if (name of view of window "iTunes" is "Rock") then
      reveal playlist "Country"
      play playlist "Country"
      
   else if (name of view of window "iTunes" is "Country") then
      reveal playlist "Jazz"
      play playlist "Jazz"
   end if
end tell


Just substitute the above "Rock", "Country", "Jazz" playlist name examples for your playlist names and if you have more than 3 playlists, just add more else if blocks.

OS 10.8.4
iTunes 11.0.5

Posted on
Fri Aug 30, 2013 7:38 pm
nlagaros offline
Posts: 1646
Joined: Dec 20, 2010

Re: Cycle thru iTunes Playlists [next playlist command]

How about something list this:

Code: Select all
tell application "iTunes"
   set first_pl to 4
   set count_pl to get count of playlists
   
   try
      set current_playlist to index of current playlist
   on error
      set current_playlist to first_pl - 1
   end try
   
   set x to current_playlist + 1
   if x > count_pl or x < first_pl then
      set x to first_pl
   end if
   
   play playlist x
end tell


I set the first playlist to 4 to exclude the builtin lists.

Posted on
Sat Aug 31, 2013 2:05 pm
CraigM offline
Posts: 589
Joined: Oct 28, 2007

Re: Cycle thru iTunes Playlists [next playlist command]

nlagaros wrote:
How about something list this:

Code: Select all
tell application "iTunes"
   set first_pl to 4
   set count_pl to get count of playlists
   
   try
      set current_playlist to index of current playlist
   on error
      set current_playlist to first_pl - 1
   end try
   
   set x to current_playlist + 1
   if x > count_pl or x < first_pl then
      set x to first_pl
   end if
   
   play playlist x
end tell


I set the first playlist to 4 to exclude the builtin lists.


Thanks for the posted option. I tested it and have some questions/comments.

A) I was able to go from PL1 [the top PL in list] to PL2, PL2 to PL3, but executing the AS while on PL3 doesn't cycle to PL1. It just stays on PL3 and gives this error
Code: Select all
Error                           script error: around characters 304 to 319
  Error                           script error: iTunes got an error: Parameter error. (-50)


B) Not being an AS expert I don't fully understand your code. So my question to you is "does your code account for whether the user has the factory PL's in the list, or does it matter"? The first thing I do in iTunes is delete all the factory PL's, leaving me with just mine [3].

C) Personal preference. I like the reveal feature as it allows the iTunes UI to highlight the PL name and contents that is actually playing.

D) Personal preference. From what I can see iTunes stacks the PL's in alphabetical order, which behind the scenes is probably a numerical value. One benefit to using the actual names, "Jazz", you can dictate a custom cycle order if you want. I suppose you could also force the order by doing this to the PL names. 1Jazz, 2Country, 3Rock.

Posted on
Sat Aug 31, 2013 4:39 pm
CraigM offline
Posts: 589
Joined: Oct 28, 2007

Re: Cycle thru iTunes Playlists [next playlist command]

Here's another example that works, thanks to Hiroto at Apple Support Forum.

The code complexity is way above my pay-grade, but you only need to name your playlists once in line 1.

Code: Select all
set playlist_names to {"Jazz", "Rock", "Country"}
rotate_playlists(playlist_names)

on rotate_playlists(pp)
   (*
               list pp : list of name of playlists to rotate
       *)
   tell application "iTunes"
      set p to browser window 1's view's name
      set k to my bsearch(pp, p) -- current index
      set k1 to k mod (count pp) + 1 -- next index
      
      tell playlist (pp's item k1) -- playlist name is case insensitive
         reveal
         play
      end tell
   end tell
end rotate_playlists

on bsearch(xx, x)
   (*
               list xx : source list
               anything x : item to be searched in xx
               return integer : the first index of x in xx if {x} is in xx, or 0 if not.
       *)
   script o
      property aa : xx
      local i, j, k
      if {x} is not in my aa then return 0
      set i to 1
      set j to count my aa
      repeat while j > i
         set k to (i + j) div 2
         if {x} is in my aa's items i thru k then
            set j to k
         else
            set i to k + 1
         end if
      end repeat
      return i
   end script
   tell o to run
end bsearch

Posted on
Sun Sep 01, 2013 2:29 pm
CraigM offline
Posts: 589
Joined: Oct 28, 2007

Re: Cycle thru iTunes Playlists [next playlist command]

Here is yet another "Safer" version of the previous code.

- It will log an (8000) error if NONE of the playlists exist.
- Ignore non-existent playlists
* Note: names must be correct case to be considered an existing playlist. "Jazz" vs "jazz"

Code: Select all
    rotate playlists in iTunes
    v0.2
   
        * playlist name is treated in case-sensitive manner
        * non-exsistent playlist name in given list is ignored

*)
set playlist_names to {"Jazz", "Rock", "Country"}
rotate_playlists(playlist_names)

on rotate_playlists(aa)
    (*
        list aa : list of name of playlists to rotate
    *)
    script o
        property pp : aa
        property qq : {}
        property nn : {}
        property ii : {}
       
        -- get list of name and id of playlists
        tell application "iTunes"
            tell playlists
                set {nn, ii} to {name, id}
            end tell
            set q to browser window 1's view's id
        end tell
       
        -- translate name list (pp) to id list (qq)
        repeat with p in my pp
            set k to my bsearch(nn, p's contents)
            if k > 0 then -- ignore non-existent playlist in pp
                set end of my qq to my ii's item k
            end if
        end repeat
       
        -- rotate playlist based on id list
        if (count my qq) = 0 then error "None of the specified playlists exist." number 8000
        tell application "iTunes"
            set k to my bsearch(my qq, q) -- current index (may be 0)
            set k1 to k mod (count my qq) + 1 -- next index
            tell playlist id (my qq's item k1)
                reveal
                play
            end tell
        end tell
    end script
    considering case -- for case-sensitive search
        tell o to run
    end considering
end rotate_playlists

on bsearch(xx, x)
    (*
        list xx : source list
        anything x : item to be searched in xx
        return integer : the first index of x in xx if {x} is in xx, or 0 if not.
    *)
    script o
        property aa : xx
        local i, j, k
        if {x} is not in my aa then return 0
        set i to 1
        set j to count my aa
        repeat while j > i
            set k to (i + j) div 2
            if {x} is in my aa's items i thru k then
                set j to k
            else
                set i to k + 1
            end if
        end repeat
        return i
    end script
    tell o to run
end bsearch


Enjoy :D

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 13 guests