Implementing a "change" check and delay in a script (CODE INCLUDED)

Hi there.

New to Applescript, and have to say, I’m finding this worse than programming (lol).

Just knocked up a script during the learning phase and can’t, for the life of me, figure out why it continuously repeats.

Wanted Outcome:
I want the script to check iTunes is open. Then to send the song details to Xirc (irc client). Then, to check to see if the song has changed, and ONLY WHEN IT DOES, to repeat the process.

Actual Outcome:
iTunes is detected. Song is spammed every 5 seconds, regardless of the proposed wait time.

See code:

set itunes_active to false

tell application "System Events"
	if (get name of every process) contains "iTunes" then set itunes_active to true
end tell

if itunes_active then
	tell application "iTunes"
		
		if player state is playing then
			set theTrack to name of the current track
			set theArtist to artist of the current track
			set theAlbum to album of the current track
			set theBitrate to bit rate of the current track
			if theArtist is not "" then
				set artistinfo to " by " & theArtist
			else
				set artistinfo to ""
			end if
			if theAlbum is not "" then
				set Albuminfo to " from the album " & theAlbum
			else
				set Albuminfo to ""
			end if
		end if
		
		repeat
			if theTrack is not equal to current track then
				set theTrack to name of the current track
				set theArtist to artist of the current track
				set theAlbum to album of the current track
				set theBitrate to bit rate of the current track
				set theContents to "is listening to " & theTrack & artistinfo & Albuminfo & " at " & theBitrate & " Kbps"
				tell application "Xirc"
					set theString to "/me " & theContents
					do theString
				end tell
			end if
			delay 5
		end repeat
		
	end tell
end if

Any and all help would be much appreciated,

Mo

Model: MacBook Pro 17" IC2D 2.33GHz 2GB Ram 120GB HDDrive
AppleScript: 1.10.7
Browser: Firefox 3.0b6
Operating System: Mac OS X (10.4)

My guess that your problem stems from the fact that theTrack is always set to the name of the current track, but the if statement compares it directly to current track. They will never be equal, so it will always tell Xirc to issue a “/me”. Try comparing theTrack to the name of the current track.

Or if you want to compare track objects directly (instead of their names), you could globally rename theTrack to something like theName, use set theTrack to current track to initialize and reset the variable, and check if theTrack is not equal to current track. You might need to use (get current track) in the if statement (sorry, I have not tested it).

Also, there is a flaw in the logic. If iTunes is running, but not playing, the script will get down the to repeat loop without ever having initialized theTrack.

Try something like this (untested):

tell application "System Events"
	if not (process "iTunes" exists) then return
end tell

set tune to ""
set previousName to missing value

repeat
	tell application "iTunes" to tell current track
		if name is not equal to previousName then
			set previousName to name
			set tune to name
			if artist is not "" then set tune to tune & " by " & artist
			if album is not "" then set tune to tune & " from the album " & album
			set tune to tune & " at " & bit rate & " Kbps"
		else
			set tune to ""
		end if
	end tell
	
	if tune is not "" then tell application "Xirc" to do "/me " & tune
	delay 5
end repeat

Thanks Bruce. That worked wonders. 1 Flaw though: It doesn’t spam the first song I listen to. Always starts from Number 2.

Cheers,

Mo.

I edited the script above.

Absolute Genius :slight_smile: I understand it too. Thank you.

Mo.