UNPKG

4.47 kBJavaScriptView Raw
1/*
2
3BSD LICENSED
4
5Copyright (c) 2011, Janne Julkunen
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without modification,
9are permitted provided that the following conditions are met:
10
11* Redistributions of source code must retain the above copyright notice, this
12list of conditions and the following disclaimer.
13
14* Redistributions in binary form must reproduce the above copyright notice,
15this list of conditions and the following disclaimer in the documentation
16and/or other materials provided with the distribution.
17
18* Neither the name of the Enlightened Linux Solutions nor the names of its
19contributors may be used to endorse or promote products derived from this
20software without specific prior written permission.
21
22THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31OF THE POSSIBILITY OF SUCH DAMAGE.
32
33*/
34
35var exec = require('child_process').exec;
36
37var applescript = require("applescript");
38
39exports.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
48exports.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