nick_harambee

Q: Scripting creation of smart playlists in iTunes

Hi,

 

I am exploring the possibility of scripting the creation of smart playlists in iTunes.  As I understand it this can only be achieved with GUI scripting.  I have come across a couple of threads which suggest this can be achieved:

 

https://discussions.apple.com/thread/20 … p;tstart=0

 

http://macscripter.net/viewtopic.php?id=9967

 

however both generate an error for me.  The problem appears to be around the line "tell scroll area 1 of group 1 of window 1".  I get the error:

 

System Events got an error: Can't get group 1 of window 1 of process "iTunes". Invalid index.

 

I wonder if someone could help me get this working?

 

Thanks,

 

Nick

Macbook Pro, Mac OS X (10.7), 2.66 GHz Intel Core i7 8GB RAM

Posted on May 6, 2012 3:07 AM

Close

Q: Scripting creation of smart playlists in iTunes

  • All replies
  • Helpful answers

Previous Page 2 of 3 last Next
  • by nick_harambee,

    nick_harambee nick_harambee May 6, 2012 2:32 PM in response to twtwtw
    Level 1 (5 points)
    May 6, 2012 2:32 PM in response to twtwtw

    Thanks.  I have it all working now, though I needed to add:

     

    tell application "System Events"

                        tell process "iTunes"

     

    before:

     

    tell window "Smart Playlist"

     

    to get the keystroke to apply to iTunes.  Without this it applied to whatever application was currently in use/focus.

     

    Thanks for your help with a very neat/handy script.

     

    Nick

  • by nick_harambee,

    nick_harambee nick_harambee May 6, 2012 2:39 PM in response to nick_harambee
    Level 1 (5 points)
    May 6, 2012 2:39 PM in response to nick_harambee

    Arrgh, one more, hopefully minor thing. Is it possible to only do the playlist check against a certain playlist folder, as I may have playlists with the same names in other folders, and that's fine.  I tried changing the line checking the existence of the playlist to:

     

              if exists user playlist theLabel in folder playlist "labels" then return

     

    but this didn't work.

     

    Nick

  • by twtwtw,

    twtwtw twtwtw May 6, 2012 2:40 PM in response to nick_harambee
    Level 5 (4,935 points)
    May 6, 2012 2:40 PM in response to nick_harambee

    actually, you don't need to do that.  That whole section is wrapped in:

     

    tell application "System Events" to tell process "iTunes"

     

    so you should already be in the right scope when you get to the 'tell window "Smart Playlist"' line.  Adding in those extra tell blocks seems to be superfluous.  but it doesn't (in this case) do any harm, so...

  • by nick_harambee,

    nick_harambee nick_harambee May 6, 2012 2:54 PM in response to twtwtw
    Level 1 (5 points)
    May 6, 2012 2:54 PM in response to twtwtw

    Mmmh, seems that adding those lines wasn't necessary after all, but adding:

     

    tell application "iTunes" to activate

     

    above

     

    tell application "System Events" to tell process "iTunes"


    ensures that the keystroke is applied to iTunes.

     

    At least that's what's working right now!

  • by twtwtw,

    twtwtw twtwtw May 6, 2012 2:59 PM in response to nick_harambee
    Level 5 (4,935 points)
    May 6, 2012 2:59 PM in response to nick_harambee

    To check this you have to get a little tricky:

     

    tell application "iTunes"

              if (every user playlist whose name is theLabel and parent's name is theFolderName) is {} then

      -- ...

              end if

    end tell

     

    the parent of a user playlist is the folder playlist that contains it, and the every keyword returns a list of items matching the criteria.  this will either be a list of 1 - if there is a playlist of that name in that folder - or an empty list. 

  • by twtwtw,

    twtwtw twtwtw May 6, 2012 3:00 PM in response to nick_harambee
    Level 5 (4,935 points)
    May 6, 2012 3:00 PM in response to nick_harambee

    Right, GUI scripting generally needs to have the application being scripted active.

  • by nick_harambee,

    nick_harambee nick_harambee May 6, 2012 3:09 PM in response to twtwtw
    Level 1 (5 points)
    May 6, 2012 3:09 PM in response to twtwtw

    When I try to add the script above which checks for playlists in a specific folder, the script is still trying to create another playlist even when a playlist exists in that folder

  • by nick_harambee,

    nick_harambee nick_harambee May 6, 2012 3:37 PM in response to nick_harambee
    Level 1 (5 points)
    May 6, 2012 3:37 PM in response to nick_harambee

    After further testing it would seem that the folder check script adds a playlist in the specified folder if it finds that there is already a playlist of the same name in the playlist folder, which is the opposite of what I would like it to do.  I tried adding "not", as in:

     

    if (every user playlist whose name is theLabel and parent's name is playlistFolder) is not {} then return

     

    but this led to strange results!

  • by Jacques Rioux,

    Jacques Rioux Jacques Rioux May 6, 2012 3:54 PM in response to nick_harambee
    Level 4 (3,418 points)
    Mac OS X
    May 6, 2012 3:54 PM in response to nick_harambee

    You can use the exists command that works well here

    Also, you must verify the property smart , because there may exist a normal playlist with that name

     

    -------------------------------------

    set theLabel to "Jockey"

    tell application "iTunes"

          if not (exists (user playlists whose name is theLabel and its smart is true and parent's name = "labels")) then

                reveal folder playlist "labels" -- this will create the New Smart Playlist in the folder "labels"  automatically

                activate -- to keystroke in iTunes instead of "AppleScript Editor"

                my createSmartPlaylist(theLabel)

          end if

    end tell

     

    on createSmartPlaylist(theLabel)

          tell application "System Events" to tell process "iTunes"

                keystroke "n" using {option down, command down} -- New Smart Playlist…

                delay 0.5

                tell pop up button 1 of group 1 of scroll area 1 of window "Smart Playlist"

                      click

                      click menu item "Comments" of menu 1

                end tell

                delay 0.5

                keystroke "label:" & theLabel & return

                delay 0.5

                keystroke theLabel & return -- to rename the playlist

                delay 0.5

          end tell

    end createSmartPlaylist

  • by nick_harambee,

    nick_harambee nick_harambee May 6, 2012 3:59 PM in response to Jacques Rioux
    Level 1 (5 points)
    May 6, 2012 3:59 PM in response to Jacques Rioux

    Hello again Jacques.  That script is working well for me, and does everything I want.  Thanks very much!

     

    Nick

  • by nick_harambee,

    nick_harambee nick_harambee May 6, 2012 8:58 PM in response to Jacques Rioux
    Level 1 (5 points)
    May 6, 2012 8:58 PM in response to Jacques Rioux

    I spoke a little too soon.  The behaviour of the last script posted by Jacque is a little eratic.  It seems to work OK on its own, but when I include it at the end of a script which passes the 'theLabel' value to it, I get an error message from iTunes:

     

    System Events got an error: Can’t get window "Smart Playlist" of process "iTunes".

  • by twtwtw,

    twtwtw twtwtw May 6, 2012 9:48 PM in response to nick_harambee
    Level 5 (4,935 points)
    May 6, 2012 9:48 PM in response to nick_harambee

    change this:

     

    tell application "System Events" to tell process "iTunes"

      --...

    end tell

     

    to this:

     

    tell application "System Events"

              tell process "iTunes"

      --...

              end tell

    end tell

     

    for some reason that seems to work better.

  • by nick_harambee,

    nick_harambee nick_harambee May 7, 2012 7:07 AM in response to twtwtw
    Level 1 (5 points)
    May 7, 2012 7:07 AM in response to twtwtw

    Thanks.

     

    Putting together various information from this thread I am now finding that the following script is most reliable, except that it doesn't reliably rename the smart playlists - i.e. it sometimes keeps the name of the text entered in the Smart Playlist window, "label:[theLabel]", rather than renaming it to just "[theLabel]".  Essentially the text for the newly created Smart Playlist doesn't stay highlighted, so it's not editable.

     

    on createSmartPlaylist(theLabel)

              tell application "System Events"

                        tell process "iTunes"

      keystroke "n" using {option down, command down} -- New Smart Playlist…

                                  delay 0.5

                                  tell window "Smart Playlist"

                                            tell scroll area 1

                                                      tell group 1

                                                                tell pop up button 1

                                                                          click

                                                                          tell menu 1

                                                                                    click menu item "Comments"

                                                                          end tell

                                                                end tell

                                                                delay 0.5

                                                                keystroke "label:" & theLabel & return

                                                      end tell

                                            end tell

                                  end tell

                                  delay 0.5

      keystroke theLabel & return -- to rename the playlist

                                  delay 0.5

                        end tell

              end tell

    end createSmartPlaylist

  • by nick_harambee,

    nick_harambee nick_harambee May 8, 2012 5:32 AM in response to Jacques Rioux
    Level 1 (5 points)
    May 8, 2012 5:32 AM in response to Jacques Rioux

    I realise there is one more thing I would like the script to be able to do.  Some of my playlists are in a folder structure two deep, i.e.

     

    Screen Shot 2012-05-08 at 13.22.11.png

     

    Because I don't want to have to specify the subfolders in the script, I am wondering if the script can be adapted to check two folders deep from the main folder, which in this case is 'country'.  I'm aware that, as I don't want to specify all of the subfolders in the script that new playlists will have to be put in the top level folder, but that's OK - I can manually drag them in to the correct subfolders later on.  If the script can at least search for already existing playlists two folders deep then at least I can use the script with this folder structure.

     

    I'm never too sure whether to ask a new question on these forums or keep new issues in the same thread in cases such as this.  It would seem that the former gives the chance for folks to earn more points, and the second keeps everything together in the same thread, so information is better organised.  Any advice on this would be welcome.

     

    Thanks,

     

    Nick

  • by twtwtw,

    twtwtw twtwtw May 8, 2012 8:57 AM in response to nick_harambee
    Level 5 (4,935 points)
    May 8, 2012 8:57 AM in response to nick_harambee

    doing this is just an extension of what I showed you before:

     

    tell application "iTunes"

              if (every user playlist whose name is theLabel and parent's name is theSubFolderName and parent's parent's name is theTopFolderName) is {} then

      -- ...

              end if

    end tell

     

    As for the 'where to post' question, it doesn't matter that much. Use your best judgement.  To my mind, something like this (which is just a tweak on the previous question) should be in the same thread, but something that's likely to take a few posts to work out should get its own thread.  The main thing to avoid is hijacking one question with a different-but-related question, which happens occassionally. 

Previous Page 2 of 3 last Next