Create iTunes smart playlist in AppleScript

I was wondering if anybody knows how to create a smart iTunes playlist via AppleScript. I know how to create a standard user playlist, but I need a smart playlist. Also, I was wondeirng if there is any way to specify the parameters of the smart playlist directly using AppleScript. For example, I would like to set the new smart playlist to only include the genre “electronic” (which is specified by on of the script’s fields). I know this may be confusing, let me know if I can clarify anything

I’m curious about this also. I’ve not been able to do it, but had to resort to creating a standard playlist. It would be nice if you could create smart playlists “on the fly” from applescript.

Ã…ny iTunes gurus out there?

I was trying to create smart playlists using AppleScript for the past two days.
No success yet…
I tried the following approaches

1.) Plain AppleScript
The iTunes dictionary doesn˜t contain commands to do this.

2.)Edit iTunes Music Library.xml
I tried rewriting the iTunes Music Library.xml
In fact it is possible to read and write data that defines smart playlist.
But iTunes doesn˜t execute the changes made to this file.
There is another file called iTunes Library. It seems like changes have to made in here for iTunes to accept the changes.
However this file is in a format called “iTunes Database File”, displays as binary in TextMate and i don˜t know how to edit it.
Interesting is the fact, that both files have the same size. This permits to conclude that both share the same data!

Also, i just found out that you could do this:
!Caution, be sure to backup your original iTunes Library files just in case!
Quit iTunes
Duplicate iTunes Music Library.xml
Edit the copy
Delete the original iTunes Music Library.xml and iTunes Library files
Open iTunes
Choose Import and select the edited iTunes Music Library.xml
iTunes will then create a new iTunes Library file corresponding to the data in the xml

Anyways, that’s not a very elegant approach!

3.) GUI script iTunes
I managed to open the new smart playlist dialog, but failed to specify the smart playlist rules.
I stumbled into some unknown UI Elements and the relevant pop up buttons are text fields, very strange…
That˜s how far i got:

tell application "iTunes" to activate
tell application "System Events"
	tell application process "iTunes"
		tell menu 1 of menu bar item "File" of menu bar 1
			click menu item 3
		end tell
		tell window "Smart Playlist"
			click text field 1 of UI element 1 of group 1
		end tell
	end tell
end tell

I’m getting the strange feeling Apple doesn’t want Smart Playlists to be created automatically.
For which reason, I don’t know…

I’ve tried this every update to iTunes.

My main problem is I can get it to do most of what I want.
Bu I can not get it to click the default Artist contains field and change it to Album.
so once this script runs I have to then manually go back and change it.

This is a working extract from one of my scripts

(* Created by mark hunte on Mon March  28 2005.
 *)


set biglist to {} (* this is a property list that the script will add to *)
set bigListRef to a reference to biglist
tell application "iTunes"
	set album_name to album of item 1 of selection
	copy album_name to end of bigListRef (* copies album name to property list bigList *)
	
	set Artist_name to artist of item 1 of selection
	copy Artist_name to end of bigListRef (* copies first Artist name to property list bigList *)
	
	
	set Artist_name to artist of item 2 of selection
	copy Artist_name to end of bigListRef (* copies second Artist name to property list bigList *)
	
	
	
	if item 2 of biglist is not equal to item 3 of biglist then (* checks Artist name 1 
	
	against artist name 2 in the property list bigList *)
		
		set item 2 of biglist to "Compilation" (* if Artist name 1 and 2 in the property list bigList are not the same, change the name to compilation *)
	end if
	
end tell

set the_smart_Name to item 1 of biglist & " - " & item 2 of biglist
get the_smart_Name
activate application "iTunes"

tell application "System Events"
	tell process "iTunes"
		click menu item "New Smart Playlist." of menu 1 of menu bar item "File" of menu bar 1
		delay 1
		set value of text field 3 of UI element 1 of group 1 of window "Smart Playlist" to album_name
		
		keystroke return
		delay 1
		keystroke the_smart_Name
		keystroke return
	end tell
end tell

As i am trying to have my app create a pre defined smart-playlist, i found a very valuable AppleAcript by Doug here:
http://www.dougscripts.com/itunes/scripts/ss.php?sp=exportsmartcriteria
This one let˜s you export a smart playlists criteria.
By choosing Import in iTunes you then can actually add this smart playlist to your iTunes library.

So, now I am trying to get the Import process automated.
But I am failing when i want to select the needed file in the dialog.
Here’s the script so far

tell application "iTunes" to activate
tell application "System Events"
	tell application process "iTunes"
		keystroke "O" using {shift down, command down}
		delay 0.5
		tell window "Import"
			-- keystroke "D" using command down
			set value of text field 1 of group 1 to "dfj235rwdfg.xml"
			delay 2
		
			tell group 1 of row 1 of outline 1 of scroll area 1 of group 1
				set myPos to position of static text 1
			end tell

		end tell
		
	end tell
	click at myPos
end tell

Do i really need extra suites to perform mouse clicks, i thought thats something GUI Scripting is able to do…

Holy!

This works great, thanks a lot Jacques!

Guess I have to work on my GUI abilities…

Look, this is even better as you don’t need to enable assistive devices(and thus prompting for an administrator password):
Attention, only works with files on the desktop!
(Maybe the home folder would be better, as the user will most likely not see a file temporarly placed there.)

tell application "iTunes" to activate
tell application "System Events"
	tell application process "iTunes"
		keystroke "O" using {shift down, command down}
		-- delay 0.5
		tell window "Import"
			keystroke "D" using command down
			keystroke (ASCII character 9)
			keystroke (ASCII character 9)
			keystroke (ASCII character 9)
-- replace with your xml file			
keystroke "dfj235rwdfg.xml"
			keystroke (ASCII character 13)
		end tell
	end tell
end tell

it seems one doesn’t need the delay here, as applescript is waiting for the window to be opened.
correct me if i’m wrong, and this might cause problems on slower computers…

cheers
jns

if you wonder, i am just working on an update of uptunes www.uptunes.blogspot.com.
i will probably use this feature to add a smart playlist for search results.