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

Page 1 of 3 last Next
  • by twtwtw,Helpful

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

    This is a problem with GUI scripting: as the software goes through revisions, changes get made to the interface which can break the software.  Such scripts need periodic revisions.

     

    My advice to you is to download UIElementInspector. It comes as sample code, but there's a compiled executable included.  That way you can inspect the interface to what's been changed and revise the System Events tell blocks appropriately so they point to the right interface element.

  • by nick_harambee,

    nick_harambee nick_harambee May 6, 2012 11:18 AM in response to twtwtw
    Level 1 (5 points)
    May 6, 2012 11:18 AM in response to twtwtw

    Thanks.  UIElementInspector is returning the following for the pop up button I would like to control, but I'm not sure what sense to make of it.  There are no numbers showing to identify which group, window, scoll area, etc:

     

    <AXApplication: “iTunes”>

    <AXWindow: “Smart Playlist”>

      <AXScrollArea>

       <AXGroup>

        <AXPopUpButton>

     

    Attributes:

       AXRole:  “AXPopUpButton”

       AXRoleDescription:  “pop up button”

       AXHelp:  “(null)”

       AXEnabled:  “1”

       AXFocused (W):  “0”

       AXParent:  “<AXGroup>”

       AXWindow:  “<AXWindow: “Smart Playlist”>”

       AXTopLevelUIElement:  “<AXWindow: “Smart Playlist”>”

       AXPosition:  “x=415 y=192”

       AXSize:  “w=149 h=17”

       AXValue:  “Artist”

       AXChildren:  “<array of size 0>”

     

     

    Actions:

       AXShowMenu - show menu

       AXPress - press

  • by twtwtw,Solvedanswer

    twtwtw twtwtw May 6, 2012 11:37 AM in response to nick_harambee
    Level 5 (4,935 points)
    May 6, 2012 11:37 AM in response to nick_harambee

    well, you see that it's going to be something like this:

    tell application "System Events"

              tell process "iTunes"

                        tell window "Smart Playlist"

                                  tell scroll area x

                                            tell group y

                                                      tell pop up button z

                                                                -- ...

                                                      end tell

                                            end tell

                                  end tell

                        end tell

              end tell

    end tell

     

    to find out what x, y and z are, hover the mouse over the element and type cmd-F7.  This freezes the inspector on that element, and you'll get a new 'Locked on' panel.  you can use the 'goto' pulldown menu and the 'highlight' toggle to navigate up and down the GUI hierarchy (go to the parent, check the parent's children, and figure out the index from the order in the list).  You could also do it by trial and error, but that can be annoying.

  • by nick_harambee,

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

    Thanks,

     

    That worked well.  The final script is as follows:

     

    set theArtist to "Bob Dylan"

     

    tell application "iTunes" to activate

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

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

              delay 0.5

              tell application "System Events"

                        tell process "iTunes"

                                  tell window "Smart Playlist"

                                            tell scroll area 1

                                                      tell group 1

                                                                tell pop up button 1

                                                                          click

                                                                          tell menu 1

                                                                                    click menu item "Artist"

                                                                          end tell

                                                                end tell

                                                                delay 0.5

                                                                keystroke theArtist & return

                                                      end tell

                                            end tell

                                  end tell

                        end tell

              end tell

              delay 0.5

      keystroke theArtist & return -- to rename the playlist

    end tell

     

    Now I don't suppose it's possible to script adding the smart playlist to a certain folder?? That would probably be asking too much, given that the only way of doing this manually appears to be drag and drop.

     

    Nick

  • by Jacques Rioux,

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

    nick_harambee wrote:

    Now I don't suppose it's possible to script adding the smart playlist to a certain folder??

    It's possible

     

    tell application "iTunes"

           --*** move the created Smart playlist ***--

          move (get view of front browser window) to folder playlist "folderName"

    end tell


  • by nick_harambee,

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

    Thanks Jacques.

     

    I tried that and got an error:

     

    iTunes got an error: Can’t get user playlist id 968299 of source id 84.

     

    Nick

  • by Jacques Rioux,Helpful

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

    nick_harambee wrote:

    I tried that and got an error:

     

    iTunes got an error: Can’t get user playlist id 968299 of source id 84.

     

    Nick

    folder playlist "folderName"  :

    "folderName" must be replaced by the name of an existing folder


  • by nick_harambee,

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

    Doh! I thought I had the correct playlist folder in place.  Sorry, it's late here!

     

    Thanks again Jaques (and twtwtw).  Automated Smart Playlists.  Very neat.

     

    Nick

  • by nick_harambee,

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

    Oh, one more question, if that's not too cheeky.  Is there a way of building in a check to see if a smart playlist with the same name already exists in the playlist folder before creating it?

  • by twtwtw,

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

    I'm not sure what you mean by "adding the smart playlist to a certain folder".  Smart playlists work on the iTunes library, which doesn't depend that much on the file system folder hierarchy.  You can use the 'grouping' key to indicate group relationships that aren't covered by other keys, though that requires an extra set of steps to set up effective groups.  what exactly are you aiming for?

  • by nick_harambee,

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

    I have a script that checks to see if certain tags are already in the comments field of each track selected in iTunes.  If not it adds these tags, based on text entered in dialog boxes.  I would then like the script to add a new smart playlist based on the tags entered, but only if it doesn't already exist.

     

    So, for example, one tag would be for subgenres, and there would be a playlist folder called 'subgenres'.  If the subgenre entered for a particular track doesn't already have a smart playlist in this folder, I would like the script to generate a new smart playlist.

  • by twtwtw,

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

    it is too cheeky, but we'll over look that. 

     

    tell application "iTunes"

              if not (exists user playlist "smart playlist name") then

      -- playlist missing, you can create it here

              end if

      -- continue with script.

    end tell

  • by nick_harambee,

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

    Thanks, but I seem to have hit another snag.  When I wrap the script in the if statement, the keystroke "n" is applied to my script editing program which creates a new file for a new script!

     

    set theLabel to "Jockey"

     

    tell application "iTunes"

              if not (exists user playlist theLabel) then

     

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

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

                                  delay 0.5

                                  tell application "System Events"

                                            tell process "iTunes"

                                                      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

                                            end tell

                                  end tell

                                  delay 0.5

      keystroke theLabel & return -- to rename the playlist

                                  delay 0.5

                        end tell

     

      move (get view of front browser window) to folder playlist "labels"

     

              end if

    end tell

  • by twtwtw,

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

    apologies, you have to separate when you're talking to iTunes directly and when you're talking to iTunes through system events.  one way of doing that is as follows:

     

    set theLabel to "Jockey"

     

    tell application "iTunes"

      --if the playlist exists, nothing to be done, so bail out.

              if exists user playlist theLabel then return

    end tell

     

    tell application "System Events" to 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

     

    tell application "iTunes"

      move (get view of front browser window) to folder playlist "labels"

    end tell


    if you need the script to continue doing something else rather than simply returning when the playlist exists, then you'll want to turn the GUI bit into a subroutine, like so:

     

    tell application "iTunes"

              if not (exists user playlist theLabel) then

      -- make a playlist

                        my GUIScripter(theLabel)

              else

      -- do something else

              end if

    end tell

     

    on GUIScripter(theLabel)

      -- scripting routine here

    end GUIScripter


Page 1 of 3 last Next