run script when playlist/track selection changes

is there a way for a script to be triggered when a new track or playlist is selected i iTunes?

Hi fhill2,

I think it could get more complicated depending on what you want to do when the playlist or track changes. It might make it easier if you know what you want to do when they change.

gl,
kel

Everytime I selected a new track, I want it to create a link to the selected track in a folder, which my DJ software “watches”.
This way I can load my selected track in my dj software while using iTunes as my track browser.

The script I want to be triggered when a track changes is:

set theLOADFolder to (path to music folder as text) & "iTunes:LOAD"
tell application "System Events" to delete (every file of folder theLOADFolder)
set theLOADFolder to quoted form of (POSIX path of (theLOADFolder))
tell application "iTunes" to set theLocation to location of (item 1 of selection)
set theLocation to quoted form of (POSIX path of theLocation)
set theScript to "ln " & theLocation & " " & theLOADFolder
do shell script theScript

hi fhill2,

You have a great idea there.

Edited: you see, you want to make your program global. That might help.

Edited: no not global, modular. It’s been a while.

gl,
kel

Do you want the AppleScript to run iTunes?

Hi fhill2,

It’s been a while since
I
ve scripted iTunes or anything but, you can try something like this:
[applescriptroperty current_playlist : missing value
property current_tracklist : missing value

– initialize
on run
tell application “iTunes”
launch
activate
set current_playlist to current playlist
set current_track to current track
end tell
end run

– monitor iTunes
on idle
tell application “iTunes”
if not (current_playlist is current_playlist or current_track is current track) then
set current_playlist to current playlist
else if not (current_track is current track) then
set current_track to current track
end if
beep
end tell
return 2
end idle

– what to do when user quits
on quit

try
display dialog “Really quit?”
end try
– rerun?
quit
end quit]


There has been problems with idle handlers. If you try something like this check your cpu usage especially with memory.

Personally, I think it should be fixed by now.
ps. I hope I split up the playlist and tracks.

gl,
kel

That script is terrible. Don’t know what happened. But, you can get the gist of it hopefully.

gl,
kel

Sorry I had to pst unfinished scripts, but I have to remove some branches from the yard. Good lock in your pursuist. :slight_smile:

I use a similar system, but fortunately the DJ software is a bit scriptable, enough to add tracks, but I have a script in the iTunes script menu, that I choose when I really want to add the track. It seems a bit too much to just sent anything you click on.

Something in the lines of the kel1 idea, hastily adapted and untested, for the playing track:


property TrackInf : {TID:"", changed:false, location:""}
on idle
	copy TID of TrackInf to oldTrackInf
	
	try
		tell application "iTunes"
			if player state ≠ stopped then
				set curr to current track
				-- will error if iTunes is stopped
				set TID of TrackInf to database ID of curr
				set changed of TrackInf to result ≠ oldTrackInf
				
				if result then
					if (class of curr) is file track then
						set locat of TrackInf to location of curr
						-- do what you want to do here
					end if
					
				end if -- changed
			else
				-- "Player stopped"
			end if
		end tell
	end try
	return 2
end idle