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 | var exec = require('child_process').exec;
|
36 |
|
37 | var applescript = require("applescript");
|
38 |
|
39 | exports.setup = function(cb) {
|
40 | var child = exec("osascript -e 'help'", function(error, stdout, stderr) {
|
41 | if(error)
|
42 | cb(null);
|
43 | else
|
44 | cb("itunes", "iTunes", "Music Player");
|
45 | });
|
46 | };
|
47 |
|
48 | exports.execute = function(req, res) {
|
49 | console.log("Executing itunes command: " + req.params[0]);
|
50 |
|
51 | var script_string = "";
|
52 |
|
53 | if(req.params[0] == "output/mute") {
|
54 | var script_string = 'tell application "iTunes" to set mute to ' +
|
55 | req.param("state") + '\n';
|
56 | } else if(req.params[0] == "output/volume") {
|
57 | var script_string = 'tell application "iTunes" to set sound volume to ' +
|
58 | req.param("level") + "\n";
|
59 | } else if(req.params[0] == "playback/state") {
|
60 | var script_string = 'tell application "iTunes" to ' + req.param("action") + '\n';
|
61 | } else if(req.params[0] == "playback/song") {
|
62 | if(req.param("action") == "prev")
|
63 | var script_string = 'tell application "iTunes" to previous track\n';
|
64 | else if(req.param("action") == "next")
|
65 | var script_string = 'tell application "iTunes" to next track\n';
|
66 | } else if(req.params[0] == "playmode/random") {
|
67 | var script_string = 'tell application "iTunes"\n';
|
68 |
|
69 | script_string += 'if player state is playing or player state is paused then\n';
|
70 |
|
71 | script_string += 'set shuffle of current playlist to ' + req.param("state") + '\n';
|
72 |
|
73 | script_string += 'end if\nend tell\n';
|
74 | } else if(req.params[0] == "playmode/repeat") {
|
75 | var script_string = 'tell application "iTunes"\n';
|
76 |
|
77 | script_string += 'if player state is playing or player state is paused then\n';
|
78 |
|
79 | if(req.param("state") == "true")
|
80 | script_string += 'set song repeat of current playlist to all\n';
|
81 | else
|
82 | script_string += 'set song repeat of current playlist to off\n';
|
83 |
|
84 | script_string += 'end if\nend tell\n';
|
85 | }
|
86 |
|
87 | script_string += 'tell application "iTunes" to get player state & sound volume & mute';
|
88 |
|
89 | applescript.execString(script_string, function(error, info) {
|
90 | if(error) {
|
91 | res.send({"state": "unknown", "repeat": false, "random": false, "volume": 0, "mute": true});
|
92 | } else {
|
93 | if((info[0] != "playing") && (info[0] != "paused")) {
|
94 | res.send({"state": "stopped", "repeat": false, "random": false, "volume": parseInt(info[1]),
|
95 | "mute": (info[2] == "true")});
|
96 | } else {
|
97 | script_string = 'tell application "iTunes" to get song repeat of current playlist & ' +
|
98 | 'shuffle of current playlist & {artist, name} of current track';
|
99 |
|
100 | applescript.execString(script_string, function(error, status) {
|
101 | if(error) {
|
102 | res.send({"state": "stopped", "repeat": false, "random": false, "volume": parseInt(info[1]),
|
103 | "mute": (info[2] == "true")});
|
104 | } else {
|
105 | res.send({"state": info[0], "repeat": (status[0] != "off"), "random": (status[1] == "true"),
|
106 | "volume": parseInt(info[1]), "mute": (info[2] == "true"), "artist": status[2], "title": status[3]});
|
107 | }
|
108 | });
|
109 | }
|
110 | }
|
111 | });
|
112 | };
|
113 |
|