-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstart.js
More file actions
58 lines (50 loc) · 1.44 KB
/
start.js
File metadata and controls
58 lines (50 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var http = require('http');
var util = require('util'),
exec = require('child_process').exec,
child;
function runApplescriptFile(osascript, args, next) {
commandLine = 'osascript ' + osascript + " '" + args + "'";
exec(commandLine,
function(error, stdout, stderr) {
if (error !== null) {
next('NodeError: ' + error + "\nargs:[" + commandLine + "]");
} else {
next(stdout);
}
});
}
function runApplescript(osascript, inline, next) {
child = exec('osascript -e \'tell application "iTunes"\' -e "' + osascript + '" -e "end tell"',
function (error, stdout, stderr) {
if (error !== null) {
next('NodeError: ' + error);
} else {
next(stdout);
}
});
}
http.createServer(function(request, response) {
urldata = require('url').parse(request.url, true)
response.writeHead(200, {'Content-Type': 'text/plain'});
switch (urldata['pathname']) {
case '/start':
data = runApplescript('play', function(data) {
response.end(data);
});
break;
case '/stop':
data = runApplescript('pause', function(data) {
response.end(data);
});
break;
case '/list':
args = urldata['query']['name'];
// sanitize input
args = args.replace(/[^A-Za-z-_0-9 \-]/g, "")
data = runApplescriptFile('listTracks.applescript', args, function(data) {
response.end(data);
});
break;
}
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');