dougscripts.com

Forward Into The Past:
Adventures With iTunes 2.0.4 in OS 9 | 1 2 3 4 5 

Send Track Info to a Web Page, part 2

We'll create a new sub-routine that sends the text file to the website with Fetch 3.0.3, although v4.0.3 works in OS 9, also. I have left out my username, password and directory info in this example for security reasons. But if you've gotten this far, you can probably figure out what has to be substituted and where. (For more info on using Fetch, visit FetchSoftworks.com.)

on uploadFile()
	try
		with timeout of 300 seconds
			tell application "Fetch 3.0.3"
				if name of front window is not "mydomain" then
					make new transfer window at front with properties ¬
						{hostname:"mydomain", userid:"myuser", password:"mypass", initial directory:"somedir/"}
				end if
				put into transfer window "mydomain" item alias data_file
			end tell
		end timeout
	on error error_message number error_number
		beep
		display dialog the error_message buttons {"Cancel"} default button 1
	end try
end uploadFile

[upm] click to open in Script Editor

If you know of another method of sending the "itunesisplaying.inc" text file, then, by all means, slap it into the subroutine.

Now we've got all the pieces. I want to be able to send the text file when iTunes is playing and send a blank text file when iTunes isn't playing or isn't running. Bret Van Horn's original script also sends a blank text file when the script is quit, so we'll emulate that as well. Look below to see where the calls to the sub-routines are placed and what data they send to the sub-routines:

-- declare variables for later use
global sample_frequency -- how often we should check iTunes
global itunes_active -- if iTunes is active or not
global dbid -- stores current track's database id
global data_file -- path to text file to update

-- implicit run handler, runs at startup only
-- initialize variables
set sample_frequency to 15 -- seconds between checking iTunes
set dbid to ""
set data_file to "path:to:itunesisplaying.inc"

-- start of monitoring section
on idle
	-- check if itunes is running
	set itunes_active to false
	tell application "Finder" -- "System Events" in OS X
		if (get name of every process) contains "iTunes" then set itunes_active to true
	end tell
	-- if itunes is running then we are go
	if itunes_active then
		tell application "iTunes"
			if player state is playing then
				try
					tell application "iTunes"
						set d to database ID of current track
						if d is dbid then -- if current track hasn't changed
							return sample_frequency -- check again shortly
						end if
						set dbid to d -- re-init dbid with current track's id
						set theTitle to name of current track
						set theArtist to artist of current track
					end tell
					set theMessage to ("Doug's iTunes is now playing \"" & theTitle & "\" by " & theArtist)
					my write_to_file(theTune, data_file)
					my uploadFile()
				end try
			else
				-- itunes is active but not playing so check back later 
				my write_to_file("", data_file)
				my uploadFile()
				set idle_time to 60
			end if
		end tell
		set idle_time to sample_frequency
	else
		-- itunes is not currently active so wait before checking again
		my write_to_file("", data_file)
		my uploadFile()
		set idle_time to 360
	end if
	return idle_time
end idle

-- subroutine to write a text string to text file
on write_to_file(this_data, target_file)
	try
		set the target_file to the target_file as text
		set the open_target_file to open for access file target_file with write permission
		set eof of the open_target_file to 0 -- erase current contents
		write this_data to the open_target_file
		close access the open_target_file
		return true
	on error error_message number error_number
		return error_message
	end try
end write_to_file

-- subroutine to upload the file
on uploadFile()
	try
		with timeout of 300 seconds
			tell application "Fetch 3.0.3"
				if name of front window is not "mydomain" then
					make new transfer window at front with properties ¬
						{hostname:"mydomain", userid:"myuser", password:"mypass", initial directory:"somedir/"}
				end if
				put into transfer window "mydomain" item alias data_file
			end tell
		end timeout
	on error error_message number error_number
		beep
		display dialog the error_message buttons {"Cancel"} default button 1
	end try
end uploadFile

-- subroutine to handle quit requests
on quit
	my write_to_file("", data_file)
	my uploadFile()
	continue quit
end quit

[upm] click to open in Script Editor

You will—no doubt—want to tweak some of the settings. For instance, the time intervals when iTunes is inactive may not be to your liking. And as I mentioned earlier, if you aren't using PHP include files, you may have to modify how theMessage is constructed. Also, Fetch 4.0.3 has some simpler commands for uploading if you want to investigate that.

Thanks to Bret Van Horn for allowing me to cannibalize his trackReporter script, which, I can't emphasize enough, works great in OS X.

UPDATE I have since added a reopen handler to this script. A reopen handler is activated when you re-activate an application by double-clicking its icon. The reopen handler in this script tells the script to run, thus re-initializing the variables as though it started from scratch.

on reopen
	tell me to run
end reopen
< prev | 1 2 3 4 5 
Site contents © 2001 - 2022 (that's right: 2001) Doug Adams and weblished by Doug Adams. Contact support AT dougscripts DOT com. About.
All rights reserved. Privacy.
AppleScript, iTunes, iPod, iPad, and iPhone are registered trademarks of Apple Inc. This site has no direct affiliation with Apple, Inc.
The one who says "it cannot be done" should not be interrupting the one who is doing it.