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 ;-)