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

Reply
30 replies

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.

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

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 areax

tell group y

tell pop up buttonz

-- ...

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.

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


keystroketheArtist & 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

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?

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.

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 (existsuser playlisttheLabel) 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


keystroketheLabel & return-- to rename the playlist

delay 0.5

end tell



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


end if

end tell

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 existsuser playlisttheLabel 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


keystroketheLabel & return-- to rename the playlist

delay 0.5

end tell


tell application "iTunes"


move (get view of front browser window) tofolder 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 (existsuser playlisttheLabel) then


-- make a playlist

my GUIScripter(theLabel)

else


-- do something else

end if

end tell


on GUIScripter(theLabel)


-- scripting routine here

end GUIScripter

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Scripting creation of smart playlists in iTunes

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.