1 | var exec = require('child_process').exec,
|
2 | applescript = require('applescript');
|
3 |
|
4 |
|
5 | var scripts = {
|
6 | track : [
|
7 | 'tell application "Spotify"',
|
8 | 'set ctrack to "{"',
|
9 | 'set ctrack to ctrack & "\\"artist\\": \\"" & current track\'s artist & "\\""',
|
10 | 'set ctrack to ctrack & ",\\"album\\": \\"" & current track\'s album & "\\""',
|
11 | 'set ctrack to ctrack & ",\\"disc_number\\": " & current track\'s disc number',
|
12 | 'set ctrack to ctrack & ",\\"duration\\": " & current track\'s duration',
|
13 | 'set ctrack to ctrack & ",\\"played_count\\": " & current track\'s played count',
|
14 | 'set ctrack to ctrack & ",\\"track_number\\": " & current track\'s track number',
|
15 | 'set ctrack to ctrack & ",\\"starred\\": " & current track\'s starred',
|
16 | 'set ctrack to ctrack & ",\\"popularity\\": " & current track\'s popularity',
|
17 | 'set ctrack to ctrack & ",\\"id\\": \\"" & current track\'s id & "\\""',
|
18 | 'set ctrack to ctrack & ",\\"name\\": \\"" & current track\'s name & "\\""',
|
19 | 'set ctrack to ctrack & ",\\"album_artist\\": \\"" & current track\'s album artist & "\\""',
|
20 | 'set ctrack to ctrack & ",\\"spotify_url\\": \\"" & current track\'s spotify url & "\\""',
|
21 | 'set ctrack to ctrack & "}"',
|
22 | 'return ctrack',
|
23 | 'end tell'
|
24 | ],
|
25 | state: [
|
26 | 'tell application "Spotify"',
|
27 | 'set cstate to "{"',
|
28 | 'set cstate to cstate & "\\"volume\\": " & sound volume',
|
29 | 'set cstate to cstate & ",\\"position\\": \\"" & player position & "\\""',
|
30 | 'set cstate to cstate & ",\\"state\\": \\"" & player state & "\\""',
|
31 | 'set cstate to cstate & "}"',
|
32 | 'return cstate',
|
33 | 'end tell'
|
34 | ],
|
35 | volumeUp : [
|
36 | 'tell application "Spotify" to activate',
|
37 | 'tell application "System Events"',
|
38 | 'keystroke (ASCII character 30) using {command down}',
|
39 | 'end tell'
|
40 | ],
|
41 | volumeDown : [
|
42 | 'tell application "Spotify" to activate',
|
43 | 'tell application "System Events"',
|
44 | 'keystroke (ASCII character 31) using {command down}',
|
45 | 'end tell'
|
46 | ],
|
47 | play : [
|
48 | 'tell application "Spotify"',
|
49 | 'play',
|
50 | 'end tell'
|
51 | ],
|
52 | playTrack : [
|
53 | 'tell application "Spotify"',
|
54 | 'play track "{{track}}"',
|
55 | 'end tell'
|
56 | ],
|
57 | playPause : [
|
58 | 'tell application "Spotify"',
|
59 | 'playpause',
|
60 | 'end tell'
|
61 | ],
|
62 | pause : [
|
63 | 'tell application "Spotify"',
|
64 | 'pause',
|
65 | 'end tell'
|
66 | ],
|
67 | next : [
|
68 | 'tell application "Spotify"',
|
69 | 'next track',
|
70 | 'end tell'
|
71 | ],
|
72 | previous : [
|
73 | 'tell application "Spotify"',
|
74 | 'previous track',
|
75 | 'end tell'
|
76 | ]
|
77 | };
|
78 |
|
79 | var execScript = function(script, callback, transformer){
|
80 | if (!callback) return null;
|
81 | script = scripts[script].join('\n');
|
82 | if (transformer) script = transformer(script);
|
83 | applescript.execString(script, callback);
|
84 | };
|
85 |
|
86 | var createJSONResponseHandler = function(callback){
|
87 | if (!callback) return null;
|
88 | return function(error, result){
|
89 | if (!error){
|
90 | try {
|
91 | result = JSON.parse(result);
|
92 | } catch(e){
|
93 | return callback(e);
|
94 | }
|
95 | return callback(null, result);
|
96 | } else {
|
97 | return callback(error);
|
98 | }
|
99 | };
|
100 | };
|
101 |
|
102 | exports.open = function(uri, callback){
|
103 | return exec('open "'+uri+'"', callback);
|
104 | };
|
105 |
|
106 | exports.play = function(callback){
|
107 | execScript('play', callback);
|
108 | };
|
109 |
|
110 | exports.playTrack = function(track, callback){
|
111 | execScript('playTrack', callback, function(script){
|
112 | return script.replace('{{track}}', track);
|
113 | });
|
114 | };
|
115 |
|
116 | exports.pause = function(callback){
|
117 | return execScript('pause', callback);
|
118 | };
|
119 |
|
120 | exports.playPause = function(callback){
|
121 | return execScript('playPause', callback);
|
122 | };
|
123 |
|
124 | exports.next = function(callback){
|
125 | return execScript('next', callback);
|
126 | };
|
127 |
|
128 | exports.previous = function(callback){
|
129 | return execScript('previous', callback);
|
130 | };
|
131 |
|
132 | exports.volumeUp = function(callback){
|
133 | return execScript('volumeUp', callback);
|
134 | };
|
135 |
|
136 | exports.volumeDown = function(callback){
|
137 | return execScript('volumeDown', callback);
|
138 | };
|
139 |
|
140 | exports.getTrack = function(callback){
|
141 | return execScript('track', createJSONResponseHandler(callback));
|
142 | };
|
143 |
|
144 | exports.getState = function(callback){
|
145 | return execScript('state', createJSONResponseHandler(callback));
|
146 | };
|