Script to add song to iTunes DJ

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

PEZfueled

New member
Joined
Jul 20, 2009
Messages
6
Points
0
Ok, so I spent half of today trying to write this script and I realized... I can't code for the life of me.

I need a script that will add a specific song to the iTunes DJ que.

I'm taking an old, no longer functioning, Jukebox and dropping a Mac inside.
I've rewired the buttons to simulate a keyboard so when you press a-1 on the juke it's send command-1 to the computer... Using the Keyboard system panel I can tell iTunes to run a specific script.

Now I need to make a script that will add a specific song to the DJ que.
I would then need to duplicate that script 200 times for each combination but...
I just can't figure out AppleScripting.

Anyone have a suggestion?

Thanks in advance.
 

S2_Mac

New member
Joined
Oct 24, 2006
Messages
4,878
Points
0
Location
About 3 feet in front of the monitor
I need a script that will add a specific song to the iTunes DJ que.

That's do-able, but more details on exactly what you want to do will make it easier to figure out how to code. Do you want the specific track to be added to DJ at a specific spot? As in, make the specific track the first one in the DJ list, or make it the last track in the DJ list, or make it the next one to play, or it doesn't matter where it shows up, etc.? What edge cases do you want to cover? Like, if juke box button A1 is already playing, what happens if someone hits A1 again...should the specific track then become the next one to play, or if already played should it appear again, etc.?

You might also check out Quicksilver...I think it's got an iTunes plugin that does what you want. A regular smart playlist might also do what you want: some random tracks, some unplayed tracks, some highly-rated tracks, and a track whose name is <specific_track>.(Plus, a smart playlist has the advantage of being shufflable via script.)
 

PEZfueled

New member
Joined
Jul 20, 2009
Messages
6
Points
0
I'd probably want the tracks added to the end of the Que. I have the DJ set to not load track ahead of time so unless there's other requests that song would play after the currently playing song.
If a track already exist in the Que and someone pressed the button again I'd be fine with it adding the track again.

I tried quicksilver and it worked for a short period of time, but some reason it would stop working until I switched out of iTunes and then back over... kind-a weird. Plus I want to keep cover flow in full screen, doing it through applescripts will allows me to do.

Thanks so much S2_Mac... I've been playing around with the scripting more, but I can't figure out the code for telling it a specific track.

Thanks Again
Tommy Nuckels
 

PEZfueled

New member
Joined
Jul 20, 2009
Messages
6
Points
0
was fairly simple... I just need the quotes.

tell application "iTunes"
duplicate track "Trackname" of library playlist 1 to playlist "iTunes DJ"
end tell

thanks again
 

PEZfueled

New member
Joined
Jul 20, 2009
Messages
6
Points
0
So after much research I've made a script that works exactly as I need it... Thought I'd share it for anyone trying to do a similar project.

tell application "iTunes"
if player state is playing then
duplicate track "SONG_TITLE" of library playlist 1 to playlist "iTunes DJ"
end if
if player state is paused or player state is stopped then
delete every track of playlist "iTunes DJ"
duplicate track "SONG_TITLE" of library playlist 1 to playlist "iTunes DJ"
play track "SONG_TITLE" of playlist "iTunes DJ"
end if
end tell
 

S2_Mac

New member
Joined
Oct 24, 2006
Messages
4,878
Points
0
Location
About 3 feet in front of the monitor
Sorry to be so long in responding....camping won out over coding ;-)

Inserting a track into the "iTunes DJ" playlist is easy. (And, as is usually the case, Doug's already been there ;-) Once you get a track, the 'inserting' part of the sample code should be very portable to other scripts.

Linking jukebox button presses (via Cmd-key combos) to specific tracks is a bit harder; this sample is my take on one way to do it with minimal work. In this method, all the tracks that will be available for insertion into the "iTunes DJ" playlist are put in a playlist named "Jukebox Hits" (just to keep them neat and tidy; more manageable and faster). Each of these tracks has its jukebox buttons entered as a Comment. Assuming a traditional 2-dimensioned grid of buttons -- A through M and 1 through 14 for instance, which yields a grid of 196 button combos -- these Comments would be strings such as "A7" (no quotes), D8, M14, etc. For example, in my run-throughs I was assigning Steely Dan's "Bodhisattva" to buttons B-2, so its Comment field contains "B2" (no quotes).

Each button combo gets its own script. The content of each script is the same, so you can just dupe the one script 195 times. The script file names are what make them unique; each script is named for its buttons. Thus, the script that will trigger inserting "Bodhisattva" into the "iTunes DJ" playlist is named "B2.scpt".

The 196 'button scripts' trigger another script that does the inserting work. This script must be named "JukeboxHits.scpt", and it must be located in the same folder as the buttons scripts. (Doesn't matter what that folder is named or where it is; the scripts just all have to be together inthe same folder.)

So, to take this thing out for a test drive, do this --
1) Create a regular playlist named "Jukebox Hits", and put at least one track in it
2) Set the Comment of one of the tracks in the "Jukebox Hits" playlist to a jukebox button combo; let's stick with "B2" (no quotes)
3) Save the first script below as a regular script, naming it for the button combo used above (in this case, "B2")
4) Save the second script below as a regular script, naming it "JukeboxHits" and saving it to the same folder as the button script(s).
5) Set System Prefs to trigger a button script via Cmd-key combo
6) Start playing the "iTunes DJ" playlist (the big script won't work if iTunes DJ isn't playing), then trigger a button script....and it should work.

Just about any button-naming scheme will work OK. Review jukebox tracks from the "Jukebox Hits" playlist; set a play order and a simple script can add all the appropriate Comment tags...

Here's the code --
the Button script:
Code:
(*
	BUTTON script
	name this script for its jukebox button; save it as a plain script 
	(for instance, to link to button B-2, name the script "B2"); 
	save it to the same folder as the 'JukeboxHits' script.
*)

set p to path to me as string -- ex: "HD:Jukebox Folder:B2.scpt"
set {TID, text item delimiters} to {text item delimiters, ":"}
set t to text items of p -- t is {"HD","Jukebox Folder","B2.scpt"}
set str to last item of t -- str is "B2.scpt"
set last item of t to "JukeboxHits.scpt" -- t is {"HD","Jukebox Folder","JukeboxHits.scpt"}
set p to (t as string) as alias -- p is <alias HD:Jukebox Folder:JukeboxHits.scpt>
set text item delimiters to TID
set my_name to text 1 through ((offset of ".scp" in str) - 1) of str -- my_name is "B2"
-- load and call the insertion script
set jukebox_script to load script p
tell jukebox_script to load_hit(my_name)
and the inserting script:
Code:
(*
	TRACK INSERTION script
	name this script "JukeboxHits"; save it as a plain script and 
	save it to the same folder as the 'jukebox button' scripts
*)

on load_hit(jukebox_button)
	tell application "iTunes"
		-- first, get the "DJ" playlist...
		set dj_playlist to some playlist whose special kind is Party Shuffle
		-- ...and the source playlist for hits
		set src_playlist to some playlist whose name is "Jukebox Hits"
		
		-- DJ playlist must be playing
		if ((player state is not playing) or (current playlist is not dj_playlist)) then return "STOPPED"
		
		-- next, use the incoming jukebox button parameter to get a specific track
		set trak to missing value
		try
			set trak to some track of src_playlist whose comment is jukebox_button
		end try
		if trak is missing value then return "NO TRACK"
		
		-- absolute info about the track
		set pid to persistent ID of trak
		
		-- first, make sure the track's not already playing
		if persistent ID of current track is pid then return "PLAYING"
		
		-- remove it from DJ list, if present
		delete (every track of dj_playlist whose persistent ID is pid)
		
		-- based on routine written by Doug Adams, dougscripts.com, which was  
		-- based on routine written by J Nicholas Jitkoff, Blacktree.com; also cf.  
		-- http://www.macosxhints.com/article.php?story=20040830035448525
		set indx to index of current track
		tell dj_playlist
			set all_tracks_beyond_current_track to a reference to (every track whose index > indx and index is less than or equal to (count tracks))
			duplicate trak to dj_playlist
			duplicate all_tracks_beyond_current_track
			delete all_tracks_beyond_current_track
		end tell
		return "DONE"
	end tell
end load_hit
Only tested under Leopard/iTunes 8.2.1; not tested via Cmd-key combos.

(And because I'm feelin' nostalgiac...damn, this would have been a wonderful HyperCard project ;-)
 

PEZfueled

New member
Joined
Jul 20, 2009
Messages
6
Points
0
Wow... that's some impressive coding... Thanks so much for your help.

Yeah, I would have gone camping too.

Thanks Again
Tommy Nuckels
 

RonfromLA

New member
Joined
Apr 22, 2012
Messages
7
Points
0
So after much research I've made a script that works exactly as I need it... Thought I'd share it for anyone trying to do a similar project.

tell application "iTunes"
if player state is playing then
duplicate track "SONG_TITLE" of library playlist 1 to playlist "iTunes DJ"
end if
if player state is paused or player state is stopped then
delete every track of playlist "iTunes DJ"
duplicate track "SONG_TITLE" of library playlist 1 to playlist "iTunes DJ"
play track "SONG_TITLE" of playlist "iTunes DJ"
end if
end tell

Hey, how can i change this to have it add the a "track" as a "play next in ITunes Dj?
 
Top