Log
Kung-Tunes-like Script for Windows
Here at my new job, much to my chagrin, I’m on a Windows machine (hopefully not for long) and I’ve been missing a lot of Mac stuff, including Kung-Tunes to post currently playing music in iTunes to this site. I’ve searched for an app that would work by sending the current track via POST, but only came across things that were either not free or used GET to add the track info. And we all know that GET should never be used when modifying data, right?
After a lot of occasional research over the last month, I finally found a bit of JScript (Microsoft’s version of Javascript) that reproduces the functionality that I was looking for. I modified it to play nicely with my script that receives the ping, which is a php script inspired by this one.
So here’s the code. If you’re going to use it, there are a few things to modify:
- The xmlTrack string to reflect the variables that you’re sending to the server (line 39)
- The URL to send to (line 41)
- The date format, if you need it (line 57)
You can also uncomment line 47 to see what you’re sending to the server and the response it sends back.
Edit it in your favorite text editor and open it with Windows Based Script Host. If you want to quit it, you need to use Task Manager and kill wscript.exe. I love Windows…
So here’s hoping I don’t need this for much longer.
var iTunesApp = WScript.CreateObject("iTunes.Application");
var iTunesIsRunning = true;
function ITEventTest_OnPlayerPlayEvent(iTrack) {
getCurrentTrackInfo();
}
function ITEventTest_OnPlayerPlayingTrackChangedEvent(iTrack) {
getCurrentTrackInfo();
}
function ITEventTest_OnQuittingEvent() {
iTunesIsRunning = false;
}
function getCurrentTrackInfo() {
var current = iTunesApp.CurrentTrack;
var stream = iTunesApp.CurrentStreamTitle;
var trackName = "";
var trackArtist = "";
var trackAlbum = "";
if (stream != "") {
trackName=stream;
trackArtist=current.Name;
trackAlbum=iTunesApp.CurrentStreamURL;
} else {
trackName=current.Name;
trackArtist=current.Artist;
trackAlbum=current.Album;
}
var response = postTrack(trackName, trackArtist, trackAlbum);
}
function postTrack(trackName, trackArtist, trackAlbum) {
var xmlTrack = encodeURI("code=-YOURCODE-&t=" + trackName + "&p=" + trackAlbum + "&a=" + trackArtist + "&d=" + rightNow());
var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");
xmlhttp.open("POST", "-YOURURL-",false);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send(xmlTrack);
if (xmlhttp.status != "200") {
WScript.Echo("Error posting to server. Code is: " + xmlhttp.status);
}
// WScript.Echo( xmlTrack + "\n" + xmlhttp.responseText);
}
function rightNow() {
var d = new Date();
var m = pad(d.getMonth() + 1);
var dd = pad(d.getDate());
var dm = pad(d.getMinutes());
var dh = pad(d.getHours());
var ds = pad(d.getSeconds());
return dh + ":" + dm + ":" + ds + " " + d.getFullYear() + "/" + m + "/" + dd;
}
function pad (num)
{
if (num.toString().length == 1)
{
num = "0" + num;
}
return num;
}
WScript.ConnectObject(iTunesApp, "ITEventTest_");
WScript.Echo("Waiting for changes...");
getCurrentTrackInfo();
while (iTunesIsRunning) {
WScript.Sleep(1000);
}
WScript.DisconnectObject(iTunesApp);
WScript.Echo("iTunes was closed, iTunesListener exiting.");
07/07/05 02:22PM Geekiness
Recently Played on iTunes
-
“Heroin”
The Velvet Underground & Nico
The Velvet Underground
11/17/08 16:26 -
“All Tomorrow's Parties”
The Velvet Underground & Nico
The Velvet Underground
11/17/08 16:20 -
“Run Run Run”
The Velvet Underground & Nico
The Velvet Underground
11/17/08 16:16