Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
jwalgran committed Jul 13, 2012
0 parents commit bb1320b
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
4 changes: 4 additions & 0 deletions applescripts/ApplicationLib.applescript
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
on ApplicationIsRunning(appName)
tell application "System Events" to set appNameIsRunning to exists (processes where name is appName)
return appNameIsRunning
end ApplicationIsRunning
Binary file added applescripts/ApplicationLib.scpt
Binary file not shown.
72 changes: 72 additions & 0 deletions applescripts/ITunesTransport.applescript
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
property ApplicationLib : load script POSIX file "ApplicationLib.scpt"

on IsRunning()
tell ApplicationLib
return ApplicationIsRunning("iTunes")
end tell
end IsRunning

on IsPlaying()
tell ApplicationLib
if ApplicationIsRunning("iTunes") then
tell application "iTunes"
return player state is playing
end tell
else
return false
end if
end tell
end IsPlaying

on GetCurrentTrack()
if IsPlaying() then
tell application "iTunes"
if not (exists current track) then return null
set trackName to (get name of current track)
set trackArtist to (get artist of current track)
set trackAlbum to (get album of current track)
return "{\"name\":\"" & trackName & "\",\"artist\":\"" & trackArtist & "\",\"album\":\"" & trackAlbum & "\"}"
end tell
else
return "null"
end if
end GetCurrentTrack

on PausePlaying()
if IsRunning() then
tell application "iTunes"
pause
end tell
end if
return "{\"ok\":true}"
end PausePlaying

on StartPlaying()
tell application "iTunes"
launch
play
end tell
return GetCurrentTrack()
end StartPlaying

on StopPlaying()
if IsRunning() then
tell application "iTunes" to stop
end if
return "{\"ok\":true}"
end StopPlaying

on run argv
set command to item 1 of argv
if command is "currenttrack" then
return GetCurrentTrack()
else if command is "play"
StartPlaying()
else if command is "pause"
PausePlaying()
else if command is "stop"
StopPlaying()
else
return "{\"error\":\"Unsupported command\"}"
end if
end run
Binary file added applescripts/ITunesTransport.scpt
Binary file not shown.
4 changes: 4 additions & 0 deletions demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var t = require('./');
t.on('playing', function(data){ console.dir(data);} );
t.on('paused', function(data){ console.log('paused');} );
t.play();
88 changes: 88 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
var spawn = require('child_process').spawn;
var events = require('events');
var util = require('util');

var Tunesport = function() {
var that = this;
events.EventEmitter.call(this);

this.runTransportApplescript = function(command, callback) {
// console.log(command);
var that = this;
var scriptRunner = spawn('osascript', ['applescripts/ITunesTransport.scpt', command]);
scriptRunner.stdout.on('data', function (data) {
var result;
try {
result = JSON.parse(data);
} catch(e) {
result = data;
}
if (callback) {
callback(result);
} else {
if (command === 'play') {
that.playing = result;
that.emit('playing', result);
} else if (command === 'stop') {
that.emit('stopped', result);
} else if (command === 'pause') {
that.emit('paused', result);
} else if (command === 'currenttrack') {
that.emit('playing', result);
}
}
});
};

that.playing = null;

// Poll for changes to the current track
setInterval(function() {
that.runTransportApplescript('currenttrack', function(data) {
if (data || that.playing) {
var track;
try {
track = JSON.parse(data);
} catch(e) {
track = data;
}
if (that.playing && track) {
if (track.artist !== that.playing.artist ||
track.album !== that.playing.album ||
track.name !== that.playing.name ) {
that.playing = track;
that.emit('playing', track);
}
} else if (that.playing !== track) {
that.playing = track;
if (track) {
that.emit('playing', track);
} else {
that.emit('paused', track);
}
}
} else {
that.playing = null;
}
});
}, 200 );
};
util.inherits(Tunesport, events.EventEmitter);

Tunesport.prototype.play = function(callback) {
this.runTransportApplescript('play', callback);
};

Tunesport.prototype.pause = function(callback) {
this.runTransportApplescript('pause', callback);
};

Tunesport.prototype.stop = function(callback) {
this.runTransportApplescript('stop', callback);
};

Tunesport.prototype.currentTrack = function(callback) {
this.runTransportApplescript('currenttrack', callback);
};

module.exports = new Tunesport();
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "tunesport",
"description": "Control the iTunes transport and receive track change events",
"version": "0.0.1",
"repository": {
"url": "git://github.com/jwalgran/tunesport.git"
},
"main": "index.js",
"scripts": {},
"dependencies": {},
"devDependencies": {}
}

0 comments on commit bb1320b

Please sign in to comment.