Applescript needed for Smart Playlist edits

GO TO ADMIN PANEL > ADD-ONS AND INSTALL VERTIFORO SIDEBAR TO SEE FORUMS AND SIDEBAR

RonfromLA

New member
Joined
Apr 22, 2012
Messages
7
Points
0
I'm currently looking for a script that allows me to edit a smart list changing the current setup drop down playlist selection.

My iTunes dj source is set to my smart list called "The Dj Playlist"

The Dj Playlist is currently setup with a playlist drop down selecting another smart list name "party list.

I also have other (mood) smart list called mellow list, Dance list, and Rock list.

I would like to control "The Dj Playlist" with several applescripts changing the mood playlist.

My hope is to use these scripts with ical to alter the style of music throughout the day. Can anyone help? I would great appreciate any help or advice!

Ron
 

S2_Mac

New member
Joined
Oct 24, 2006
Messages
4,878
Points
0
Location
About 3 feet in front of the monitor
Scripting has no access to smart playlist conditionals, so there's no direct way to accomplish your task. Typical workarounds: 1) Create enough smart playlists to cover all combinations, then use a script to select the right smart list, or 2) Simulate smart playlist action in a script, or 3) some combination of the above two methods.

The first could be something like:
Code:
(* Build a name to match an existing smart playlist *)
tell application "iTunes"
	-- start with smart playlists named for all mood-times, 
	-- then build a name that matches one of them.
	-- (default playlist name is "The DJ Playlist", in case we screw up)
	set str to "The"
	set the_hours to hours of (current date)
	if the_hours < 4 then -- midnight to 4 a.m.
		set str to "dance"
	else if the_hours < 11 then -- 4 a.m.to 1 1a.m.
		set str to "mellow"
	else if the_hours < 22 then -- 11 a.m. to 10 p.m.
		set str to "rock"
	else if the_hours ≥ 22 then -- 10 p.m. to midnight
		set str to "dance"
	end if

	-- so, at 9 a.m. the dj_list_name will be "mellow DJ Playlist"
	set dj_list_name to (str & " DJ Playlist")
	
	set list_to_play to first user playlist whose smart is true and name is dj_list_name
	play list_to_play
	-- or: play last track of list_to_play; play some track of list_to_play; 
	-- play track 33 of list_to_play; etc.
end tell
The second could be something like:
Code:
(* Populating a static playlist by simulating "smart" conditionals *)
tell application "iTunes"
	-- get a handle on the Music lib
	set music_lib to some playlist whose special kind is Music
	
	-- get the current year
	--set cur_year to (do shell script "date '+%Y'")
	-- OR --
	set cur_year to my curYear()
	(*
	Note: some versions of iTunes can't get the "year" property of Date objects, 
	so we have to go outside the 'tell iTunes' block to get that info. Either of the 
	two methods above should work fine. You could also just 'end tell', then 
	"set cur_year to year of current date", then start a new 'tell iTunes' block
	*)
	
	-- get all "Rock" tracks newer than 6 years
	set all_rock_trax to every track of music_lib whose genre contains "rock" and year > (cur_year - 5)
	
	-- get an empty playlist
	set dj_playlist_name to "DJ List"
	if (exists user playlist dj_playlist_name) then
		set dj_playlist to user playlist dj_playlist_name
		delete every track of dj_playlist
	else
		set dj_playlist to (make user playlist with properties {name:dj_playlist_name})
	end if
	
	-- put all the found tracks into that playlist
	repeat with trak in all_rock_trax
		try
			duplicate trak to dj_playlist
		end try
	end repeat
	play dj_playlist
end tell

-- some iTunes versions can't get the "year" prop of a date object 
-- from within a "Tell iTunes" block, so we drop out to this function.
on curYear()
	return year of (current date)
end curYear
 

RonfromLA

New member
Joined
Apr 22, 2012
Messages
7
Points
0
These are great! Thanks for the quick response! I'm so new to scripting.... can I ask what is "str"?

example:
set str to "dance"
 

RonfromLA

New member
Joined
Apr 22, 2012
Messages
7
Points
0
I actually found a script that changes the ITunes DJ "source list" to my mood playlists and it works fine... The problem with changing the DJ "source" list to point to different smart playlist is that I also want to change the "restricted request" playlist to match that same style of music. This is why i thought having one static smart playlist for both would be easier and just alter (via script) that one playlist to suit my mood styles.

Do you know of a way to match the "source" and the "request" lists for iTunes Dj ?
I really need these to match. I don't want quest to select "mellow" music during the "dance" hours.

Again... thanks for your help!
 

S2_Mac

New member
Joined
Oct 24, 2006
Messages
4,878
Points
0
Location
About 3 feet in front of the monitor
I actually found a script that changes the ITunes DJ "source list" to my mood playlists and it works fine...

So....mind sharing? Like, where is this script? (As a new member you can't yet post "real" links, but you can post a pseudo-llink such as: forums.ilounge dot com/applescripts-itunes-mac/274704-applescript-needed-smart-playlist-edits.html)

I'm guessing the script you found uses "GUI scripting" to simulate clicks on the popup menu and Refresh button. (That's one reason I'd like to see the script you found....if it uses GUI scripting I'd like to learn its technique for dismissing that popup menu; I whipped up a script for you that uses GUI scripting to set the DJ source playlist popup, but I can't make it dismiss the menu <g>. But if there's now an Applescript property for the iTunes DJ source playlist, that's a big change from earlier versions, and that would be fun to learn about, too...I use an old iTunes and am always looking for good reasons to update ;-)

If the script you found uses GUI scripting, I can't help with the Guest source playlist issue...I recently lost all my Macs to an electrical storm, and the only one I've restored to service runs 10.5; I'm guessing my Dev Tools won't be much good for poking around the innards of a "modern" iTunes ::rueful grin::

OTOH, if the script you found uses a (new) iTunes Applescript property to set the DJ source playlist, there may be hope for other properties such as the Guest source list. If I could see that script you found, I'd know how/if I could advise you further....
 

RonfromLA

New member
Joined
Apr 22, 2012
Messages
7
Points
0
Sure S2...... No problem!

Here is the link for that script. I'm only using the top section of the script.

Applescript PITA: Set iTunes DJ playlist and multiple speakers | polishing ruby by ryan davis



I had to alter my script a bit so that when I run a new "mood" script it doesn't stop the currently playing song.

Here is my version of that script which is working for me.

property myPlaylist : "Dance"
property myVolume : 100

tell application "iTunes"
activate

set dj to playlist "iTunes DJ"
reveal dj

my setSource()

set sound volume to myVolume


set minimized of front browser window to false
end tell

on setSource()
tell application "System Events"
tell application process "iTunes"
tell window "iTunes"
if value of pop up button 1 is not myPlaylist then
-- this is so stupid... apple ####ed up the UI and detaches the menu so we can't access it
-- so we must set the value and then... UGH... _type_ to select the value we just set
set focused of pop up button 1 to true
set value of pop up button 1 to myPlaylist
delay 0.5
keystroke " "
delay 0.5
keystroke " "
delay 0.5
click button "Refresh"
end if
end tell
end tell
end tell
end setSource


This works great.... however, If I use this method I also need a way to change the ITunes Dj "request" list so that it also points to that same mood playlist.

Hope this helps both of us achieve what we need!
 

RonfromLA

New member
Joined
Apr 22, 2012
Messages
7
Points
0
OOPS.... I now understand what you mean by adding links....


Her is that link again:

Took off the http:// at the beginning and the .html at the end and added parentheses to the link.

(blog.zenspider.com/blog/2011/08/applescript-pita-set-itunes-dj-playlist-and-multiple-speakers)


PS:
My script also got left justify when pasted on this editor. It's not that way in my script.
I'm not sure if that makes a difference.

Ron
 
Last edited:
Top