-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bb1320b
Showing
8 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} |