spotify_osx_controller 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.DS_Store +0 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/README.md +39 -0
- data/Rakefile +1 -0
- data/SpotifyControl.rb +200 -0
- data/SpotifyControl.scpt +157 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/bin/spotify_osx_controller +34 -0
- data/lib/spotify_osx_controller.rb +172 -0
- data/lib/spotify_osx_controller/version.rb +3 -0
- data/spotify_osx_controller.gemspec +24 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f1cc92301a8266f4a6e65e03775d482ff66952bb
|
4
|
+
data.tar.gz: 064aba3d986c3ad5d9e85200d1b4599677754f11
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 37dd431a8202a1e9ddee37e2d295f20d6a1cd0c4decba36bf151d77d5c5b75e7de09837ba2b6d688be9fe3be7f52b231098368218bae41fdb7fe3a9d2f0af375
|
7
|
+
data.tar.gz: 19286fcdb0ab4a5ab0bf57a62b81a88760e748de6b904be623a7ce21e5d487a954f40c0a0069877ab459a451f496af563bf55a9876b6f3ecab7849710d275356
|
data/.DS_Store
ADDED
Binary file
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# SpotifyOsxController
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/spotify_osx_controller`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'spotify_osx_controller'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install spotify_osx_controller
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it ( https://github.com/[my-github-username]/spotify_osx_controller/fork )
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/SpotifyControl.rb
ADDED
@@ -0,0 +1,200 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
class Command
|
4
|
+
|
5
|
+
def self.play(url)
|
6
|
+
if url
|
7
|
+
buildScript "tell application \"Spotify\" to play track \"#{url}\"\n"
|
8
|
+
else
|
9
|
+
buildScript "tell application \"Spotify\" to play\n"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.play_pause
|
14
|
+
buildScript String <<-END
|
15
|
+
tell application "Spotify" to playpause
|
16
|
+
END
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.pause
|
20
|
+
buildScript "tell application \"Spotify\" to pause"
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.next
|
24
|
+
buildScript "tell application \"Spotify\" to next track"
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.previous
|
28
|
+
buildScript "tell application \"Spotify\" to previous track"
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.jump seconds
|
32
|
+
return if !seconds
|
33
|
+
buildScript String <<-END
|
34
|
+
tell application "Spotify"
|
35
|
+
set jumpTo to #{seconds} as real
|
36
|
+
set tMax to duration of current track
|
37
|
+
if jumpTo > tMax
|
38
|
+
return "Can't jump past end of track."
|
39
|
+
else if jumpTo < 0
|
40
|
+
return "Can't jump past start of track."
|
41
|
+
end if
|
42
|
+
set nM to round (jumpTo / 60) rounding down
|
43
|
+
set nS to round (jumpTo mod 60) rounding down
|
44
|
+
set newTime to nM as text & "min " & nS as text & "s"
|
45
|
+
set player position to jumpTo
|
46
|
+
return "Jumped to " & newTime
|
47
|
+
end tell
|
48
|
+
END
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.forward seconds
|
52
|
+
return if !seconds
|
53
|
+
buildScript String <<-END
|
54
|
+
set jump to #{seconds} as real
|
55
|
+
tell application "Spotify"
|
56
|
+
set now to player position
|
57
|
+
set tMax to duration of current track
|
58
|
+
set jumpTo to now + jump
|
59
|
+
if jumpTo > tMax
|
60
|
+
return "Can't jump past end of track."
|
61
|
+
else if jumpTo < 0
|
62
|
+
set jumpTo to 0
|
63
|
+
end if
|
64
|
+
set nM to round (jumpTo / 60) rounding down
|
65
|
+
set nS to round (jumpTo mod 60) rounding down
|
66
|
+
set newTime to nM as text & "min " & nS as text & "s"
|
67
|
+
set player position to jumpTo
|
68
|
+
return "Jumped to " & newTime
|
69
|
+
end tell
|
70
|
+
END
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.rewind seconds
|
74
|
+
return if !seconds
|
75
|
+
buildScript String <<-END
|
76
|
+
set jump to #{seconds} as real
|
77
|
+
tell application "Spotify"
|
78
|
+
set now to player position
|
79
|
+
set tMax to duration of current track
|
80
|
+
set jumpTo to now - jump
|
81
|
+
if jumpTo > tMax
|
82
|
+
return "Can't jump past end of track."
|
83
|
+
else if jumpTo < 0
|
84
|
+
set jumpTo to 0
|
85
|
+
end if
|
86
|
+
set nM to round (jumpTo / 60) rounding down
|
87
|
+
set nS to round (jumpTo mod 60) rounding down
|
88
|
+
set newTime to nM as text & "min " & nS as text & "s"
|
89
|
+
set player position to jumpTo
|
90
|
+
return "Jumped to " & newTime
|
91
|
+
end tell
|
92
|
+
END
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.volume volume
|
96
|
+
return if !volume
|
97
|
+
buildScript String <<-END
|
98
|
+
set newVolume to #{volume} as real
|
99
|
+
if newVolume < 0 then set newVolume to 0
|
100
|
+
if newVolume > 100 then set newVolume to 100
|
101
|
+
tell application "Spotify"
|
102
|
+
set sound volume to newVolume
|
103
|
+
end tell
|
104
|
+
return "Changed volume to " & newVolume
|
105
|
+
END
|
106
|
+
end
|
107
|
+
|
108
|
+
def self.shuffle
|
109
|
+
buildScript String <<-END
|
110
|
+
tell application "Spotify"
|
111
|
+
set shuffling to not shuffling
|
112
|
+
return "Shuffle is now " & shuffling
|
113
|
+
end tell
|
114
|
+
END
|
115
|
+
end
|
116
|
+
|
117
|
+
def self.repeat
|
118
|
+
buildScript String <<-END
|
119
|
+
tell application "Spotify"
|
120
|
+
set repeating to not repeating
|
121
|
+
return "Repeat is now " & repeating
|
122
|
+
end tell
|
123
|
+
END
|
124
|
+
end
|
125
|
+
|
126
|
+
def self.info
|
127
|
+
buildScript String <<-END
|
128
|
+
set info to "Error."
|
129
|
+
tell application "Spotify"
|
130
|
+
set myTrack to name of current track
|
131
|
+
set myArtist to artist of current track
|
132
|
+
set myAlbum to album of current track
|
133
|
+
set tM to round (duration of current track / 60) rounding down
|
134
|
+
set tS to duration of current track mod 60
|
135
|
+
set myTime to tM as text & "min " & tS as text & "s"
|
136
|
+
set nM to round (player position / 60) rounding down
|
137
|
+
set nS to round (player position mod 60) rounding down
|
138
|
+
set nowAt to player position
|
139
|
+
set info to "{"
|
140
|
+
set info to info & "\\"artist\\": \\"" & myArtist & "\\","
|
141
|
+
set info to info & "\\"track\\": \\"" & myTrack & "\\","
|
142
|
+
set info to info & "\\"album\\": \\"" & myAlbum & "\\","
|
143
|
+
set info to info & "\\"URI\\": \\"" & spotify url of current track & "\\","
|
144
|
+
set info to info & "\\"duration\\": \\"" & duration of current track & "\\","
|
145
|
+
set info to info & "\\"player\\": \\"" & player state & "\\","
|
146
|
+
set info to info & "\\"position\\": \\"" & nowAt & "\\""
|
147
|
+
set info to info & "}"
|
148
|
+
end tell
|
149
|
+
return info
|
150
|
+
END
|
151
|
+
end
|
152
|
+
|
153
|
+
private
|
154
|
+
|
155
|
+
def self.buildScript(body)
|
156
|
+
header = "using terms from application \"Spotify\""
|
157
|
+
footer = "end using terms from"
|
158
|
+
osascript <<-END
|
159
|
+
#{header}
|
160
|
+
#{body}
|
161
|
+
#{footer}
|
162
|
+
END
|
163
|
+
end
|
164
|
+
|
165
|
+
def self.osascript(script)
|
166
|
+
system 'osascript', *script.split(/\n/).map { |line| ['-e', line] }.flatten
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
command = ARGV[0]
|
171
|
+
arg1 = ARGV[1]
|
172
|
+
case command.downcase
|
173
|
+
when 'play', 'start'
|
174
|
+
Command.play arg1
|
175
|
+
when 'play/pause'
|
176
|
+
Command.play_pause
|
177
|
+
when 'pause', 'stop'
|
178
|
+
Command.pause
|
179
|
+
when 'next'
|
180
|
+
Command.next
|
181
|
+
when 'previous'
|
182
|
+
Command.previous
|
183
|
+
when 'jump'
|
184
|
+
Command.jump arg1
|
185
|
+
when 'forward'
|
186
|
+
Command.forward arg1
|
187
|
+
when 'rewind'
|
188
|
+
Command.rewind arg1
|
189
|
+
when 'volume'
|
190
|
+
Command.volume arg1
|
191
|
+
when 'shuffle'
|
192
|
+
Command.shuffle
|
193
|
+
when 'repeat'
|
194
|
+
Command.repeat
|
195
|
+
when 'info'
|
196
|
+
Command.info
|
197
|
+
exit
|
198
|
+
end
|
199
|
+
|
200
|
+
Command.info
|
data/SpotifyControl.scpt
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
on run argv
|
2
|
+
if count of argv is equal to 0 then
|
3
|
+
set msg to "Use the following commands:\n"
|
4
|
+
set msg to msg & " start, play [uri] - Start playback / play uri\n"
|
5
|
+
set msg to msg & " pause, stop - Stop playback\n"
|
6
|
+
set msg to msg & " play/pause - Toggle playback\n"
|
7
|
+
set msg to msg & " next - Next track\n"
|
8
|
+
set msg to msg & " previous, prev - Previous track\n"
|
9
|
+
set msg to msg & " info - Print track info\n"
|
10
|
+
set msg to msg & " jump N - Jump to N seconds in the song\n"
|
11
|
+
set msg to msg & " forward N - Jump N seconds forwards\n"
|
12
|
+
set msg to msg & " rewind N - Jump N seconds backwards\n"
|
13
|
+
set msg to msg & " shuffle - Toggle shuffle\n"
|
14
|
+
set msg to msg & " repeat - Toggle repeat\n"
|
15
|
+
set msg to msg & " volume N - Set Volume to N (0...100)\n"
|
16
|
+
return msg
|
17
|
+
end if
|
18
|
+
set command to item 1 of argv
|
19
|
+
using terms from application "Spotify"
|
20
|
+
set info to "Error."
|
21
|
+
if command is equal to "play" or command is equal to "start" then
|
22
|
+
if count of argv is equal to 1 then
|
23
|
+
tell application "Spotify" to play
|
24
|
+
else
|
25
|
+
set uri to item 2 of argv
|
26
|
+
tell application "Spotify" to play track uri
|
27
|
+
end if
|
28
|
+
else if command is equal to "play/pause" then
|
29
|
+
tell application "Spotify" to playpause
|
30
|
+
return "Toggled."
|
31
|
+
|
32
|
+
else if command is equal to "pause" or command is equal to "stop" then
|
33
|
+
tell application "Spotify" to pause
|
34
|
+
return "Paused."
|
35
|
+
|
36
|
+
else if command is equal to "next" then
|
37
|
+
tell application "Spotify" to next track
|
38
|
+
|
39
|
+
else if command is equal to "previous" or command is equal to "prev" then
|
40
|
+
tell application "Spotify" to previous track
|
41
|
+
|
42
|
+
else if command is equal to "jump"
|
43
|
+
set jumpTo to item 2 of argv as real
|
44
|
+
tell application "Spotify"
|
45
|
+
set tMax to duration of current track
|
46
|
+
if jumpTo > tMax
|
47
|
+
return "Can't jump past end of track."
|
48
|
+
else if jumpTo < 0
|
49
|
+
return "Can't jump past start of track."
|
50
|
+
end if
|
51
|
+
set nM to round (jumpTo / 60) rounding down
|
52
|
+
set nS to round (jumpTo mod 60) rounding down
|
53
|
+
set newTime to nM as text & "min " & nS as text & "s"
|
54
|
+
set player position to jumpTo
|
55
|
+
return "Jumped to " & newTime
|
56
|
+
end tell
|
57
|
+
|
58
|
+
else if command is equal to "forward"
|
59
|
+
set jump to item 2 of argv as real
|
60
|
+
tell application "Spotify"
|
61
|
+
set now to player position
|
62
|
+
set tMax to duration of current track
|
63
|
+
set jumpTo to now + jump
|
64
|
+
if jumpTo > tMax
|
65
|
+
return "Can't jump past end of track."
|
66
|
+
else if jumpTo < 0
|
67
|
+
set jumpTo to 0
|
68
|
+
end if
|
69
|
+
set nM to round (jumpTo / 60) rounding down
|
70
|
+
set nS to round (jumpTo mod 60) rounding down
|
71
|
+
set newTime to nM as text & "min " & nS as text & "s"
|
72
|
+
set player position to jumpTo
|
73
|
+
return "Jumped to " & newTime
|
74
|
+
end tell
|
75
|
+
|
76
|
+
else if command is equal to "rewind"
|
77
|
+
set jump to item 2 of argv as real
|
78
|
+
tell application "Spotify"
|
79
|
+
set now to player position
|
80
|
+
set tMax to duration of current track
|
81
|
+
set jumpTo to now - jump
|
82
|
+
if jumpTo > tMax
|
83
|
+
return "Can't jump past end of track."
|
84
|
+
else if jumpTo < 0
|
85
|
+
set jumpTo to 0
|
86
|
+
end if
|
87
|
+
set nM to round (jumpTo / 60) rounding down
|
88
|
+
set nS to round (jumpTo mod 60) rounding down
|
89
|
+
set newTime to nM as text & "min " & nS as text & "s"
|
90
|
+
set player position to jumpTo
|
91
|
+
return "Jumped to " & newTime
|
92
|
+
end tell
|
93
|
+
|
94
|
+
else if command is equal to "volume" then
|
95
|
+
set newVolume to item 2 of argv as real
|
96
|
+
if newVolume < 0 then set newVolume to 0
|
97
|
+
if newVolume > 100 then set newVolume to 100
|
98
|
+
tell application "Spotify"
|
99
|
+
set sound volume to newVolume
|
100
|
+
end tell
|
101
|
+
return "Changed volume to " & newVolume
|
102
|
+
|
103
|
+
else if command is equal to "shuffle" then
|
104
|
+
tell application "Spotify"
|
105
|
+
set shuffling to not shuffling
|
106
|
+
return "Shuffle is now " & shuffling
|
107
|
+
end tell
|
108
|
+
|
109
|
+
else if command is equal to "repeat" then
|
110
|
+
tell application "Spotify"
|
111
|
+
set repeating to not repeating
|
112
|
+
return "Repeat is now " & repeating
|
113
|
+
end tell
|
114
|
+
|
115
|
+
else if command is equal to "info" then
|
116
|
+
my getInfo()
|
117
|
+
end if
|
118
|
+
|
119
|
+
tell application "Spotify"
|
120
|
+
if player state as text is equal to "playing"
|
121
|
+
my getInfo()
|
122
|
+
end if
|
123
|
+
end tell
|
124
|
+
|
125
|
+
|
126
|
+
end using terms from
|
127
|
+
end run
|
128
|
+
|
129
|
+
|
130
|
+
on getInfo()
|
131
|
+
set info to "Error."
|
132
|
+
tell application "Spotify"
|
133
|
+
set myTrack to name of current track
|
134
|
+
set myArtist to artist of current track
|
135
|
+
set myAlbum to album of current track
|
136
|
+
set tM to round (duration of current track / 60) rounding down
|
137
|
+
set tS to duration of current track mod 60
|
138
|
+
set myTime to tM as text & "min " & tS as text & "s"
|
139
|
+
set nM to round (player position / 60) rounding down
|
140
|
+
set nS to round (player position mod 60) rounding down
|
141
|
+
set nowAt to player position
|
142
|
+
set info to "{"
|
143
|
+
set info to info & "\n \"artist\": \"" & myArtist & "\","
|
144
|
+
set info to info & "\n \"track\": \"" & myTrack & "\","
|
145
|
+
set info to info & "\n \"album\": \"" & myAlbum & "\","
|
146
|
+
set info to info & "\n \"URI\": \"" & spotify url of current track & "\","
|
147
|
+
set info to info & "\n \"duration\": \"" & mytime & " ("& duration of current track & " seconds)" & "\","
|
148
|
+
set info to info & "\n \"player\": \"" & player state & "\""
|
149
|
+
set info to info & "\n \"position\": \"" & nowAt & "\"\n"
|
150
|
+
set info to info & "}"
|
151
|
+
end tell
|
152
|
+
return info
|
153
|
+
end getInfo
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "spotify_osx_controller"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require "spotify_osx_controller"
|
3
|
+
|
4
|
+
command = ARGV[0]
|
5
|
+
arg1 = ARGV[1]
|
6
|
+
case command.downcase
|
7
|
+
when 'play', 'start'
|
8
|
+
SpotifyOsxController.play arg1
|
9
|
+
when 'play/pause'
|
10
|
+
SpotifyOsxController.play_pause
|
11
|
+
when 'pause', 'stop'
|
12
|
+
SpotifyOsxController.pause
|
13
|
+
when 'next'
|
14
|
+
SpotifyOsxController.next
|
15
|
+
when 'previous'
|
16
|
+
SpotifyOsxController.previous
|
17
|
+
when 'jump'
|
18
|
+
SpotifyOsxController.jump arg1
|
19
|
+
when 'forward'
|
20
|
+
SpotifyOsxController.forward arg1
|
21
|
+
when 'rewind'
|
22
|
+
SpotifyOsxController.rewind arg1
|
23
|
+
when 'volume'
|
24
|
+
SpotifyOsxController.volume arg1
|
25
|
+
when 'shuffle'
|
26
|
+
SpotifyOsxController.shuffle
|
27
|
+
when 'repeat'
|
28
|
+
SpotifyOsxController.repeat
|
29
|
+
when 'info'
|
30
|
+
SpotifyOsxController.info
|
31
|
+
exit
|
32
|
+
end
|
33
|
+
|
34
|
+
SpotifyOsxController.info
|
@@ -0,0 +1,172 @@
|
|
1
|
+
require "spotify_osx_controller/version"
|
2
|
+
|
3
|
+
module SpotifyOsxController
|
4
|
+
def self.play(url)
|
5
|
+
if url
|
6
|
+
buildScript "tell application \"Spotify\" to play track \"#{url}\"\n"
|
7
|
+
else
|
8
|
+
buildScript "tell application \"Spotify\" to play\n"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.play_pause
|
13
|
+
buildScript String <<-END
|
14
|
+
tell application "Spotify" to playpause
|
15
|
+
END
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.stop
|
19
|
+
pause
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.pause
|
23
|
+
buildScript "tell application \"Spotify\" to pause"
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.next
|
27
|
+
buildScript "tell application \"Spotify\" to next track"
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.previous
|
31
|
+
buildScript "tell application \"Spotify\" to previous track"
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.jump seconds
|
35
|
+
return if !seconds
|
36
|
+
buildScript String <<-END
|
37
|
+
tell application "Spotify"
|
38
|
+
set jumpTo to #{seconds} as real
|
39
|
+
set tMax to duration of current track
|
40
|
+
if jumpTo > tMax
|
41
|
+
return "Can't jump past end of track."
|
42
|
+
else if jumpTo < 0
|
43
|
+
return "Can't jump past start of track."
|
44
|
+
end if
|
45
|
+
set nM to round (jumpTo / 60) rounding down
|
46
|
+
set nS to round (jumpTo mod 60) rounding down
|
47
|
+
set newTime to nM as text & "min " & nS as text & "s"
|
48
|
+
set player position to jumpTo
|
49
|
+
return "Jumped to " & newTime
|
50
|
+
end tell
|
51
|
+
END
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.forward seconds
|
55
|
+
return if !seconds
|
56
|
+
buildScript String <<-END
|
57
|
+
set jump to #{seconds} as real
|
58
|
+
tell application "Spotify"
|
59
|
+
set now to player position
|
60
|
+
set tMax to duration of current track
|
61
|
+
set jumpTo to now + jump
|
62
|
+
if jumpTo > tMax
|
63
|
+
return "Can't jump past end of track."
|
64
|
+
else if jumpTo < 0
|
65
|
+
set jumpTo to 0
|
66
|
+
end if
|
67
|
+
set nM to round (jumpTo / 60) rounding down
|
68
|
+
set nS to round (jumpTo mod 60) rounding down
|
69
|
+
set newTime to nM as text & "min " & nS as text & "s"
|
70
|
+
set player position to jumpTo
|
71
|
+
return "Jumped to " & newTime
|
72
|
+
end tell
|
73
|
+
END
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.rewind seconds
|
77
|
+
return if !seconds
|
78
|
+
buildScript String <<-END
|
79
|
+
set jump to #{seconds} as real
|
80
|
+
tell application "Spotify"
|
81
|
+
set now to player position
|
82
|
+
set tMax to duration of current track
|
83
|
+
set jumpTo to now - jump
|
84
|
+
if jumpTo > tMax
|
85
|
+
return "Can't jump past end of track."
|
86
|
+
else if jumpTo < 0
|
87
|
+
set jumpTo to 0
|
88
|
+
end if
|
89
|
+
set nM to round (jumpTo / 60) rounding down
|
90
|
+
set nS to round (jumpTo mod 60) rounding down
|
91
|
+
set newTime to nM as text & "min " & nS as text & "s"
|
92
|
+
set player position to jumpTo
|
93
|
+
return "Jumped to " & newTime
|
94
|
+
end tell
|
95
|
+
END
|
96
|
+
end
|
97
|
+
|
98
|
+
def self.volume volume
|
99
|
+
return if !volume
|
100
|
+
buildScript String <<-END
|
101
|
+
set newVolume to #{volume} as real
|
102
|
+
if newVolume < 0 then set newVolume to 0
|
103
|
+
if newVolume > 100 then set newVolume to 100
|
104
|
+
tell application "Spotify"
|
105
|
+
set sound volume to newVolume
|
106
|
+
end tell
|
107
|
+
return "Changed volume to " & newVolume
|
108
|
+
END
|
109
|
+
end
|
110
|
+
|
111
|
+
def self.shuffle
|
112
|
+
buildScript String <<-END
|
113
|
+
tell application "Spotify"
|
114
|
+
set shuffling to not shuffling
|
115
|
+
return "Shuffle is now " & shuffling
|
116
|
+
end tell
|
117
|
+
END
|
118
|
+
end
|
119
|
+
|
120
|
+
def self.repeat
|
121
|
+
buildScript String <<-END
|
122
|
+
tell application "Spotify"
|
123
|
+
set repeating to not repeating
|
124
|
+
return "Repeat is now " & repeating
|
125
|
+
end tell
|
126
|
+
END
|
127
|
+
end
|
128
|
+
|
129
|
+
def self.info
|
130
|
+
buildScript String <<-END
|
131
|
+
set info to "Error."
|
132
|
+
tell application "Spotify"
|
133
|
+
set myTrack to name of current track
|
134
|
+
set myArtist to artist of current track
|
135
|
+
set myAlbum to album of current track
|
136
|
+
set tM to round (duration of current track / 60) rounding down
|
137
|
+
set tS to duration of current track mod 60
|
138
|
+
set myTime to tM as text & "min " & tS as text & "s"
|
139
|
+
set nM to round (player position / 60) rounding down
|
140
|
+
set nS to round (player position mod 60) rounding down
|
141
|
+
set nowAt to player position
|
142
|
+
set info to "{"
|
143
|
+
set info to info & "\\"artist\\": \\"" & myArtist & "\\","
|
144
|
+
set info to info & "\\"track\\": \\"" & myTrack & "\\","
|
145
|
+
set info to info & "\\"album\\": \\"" & myAlbum & "\\","
|
146
|
+
set info to info & "\\"URI\\": \\"" & spotify url of current track & "\\","
|
147
|
+
set info to info & "\\"duration\\": \\"" & duration of current track & "\\","
|
148
|
+
set info to info & "\\"player\\": \\"" & player state & "\\","
|
149
|
+
set info to info & "\\"position\\": \\"" & nowAt & "\\""
|
150
|
+
set info to info & "}"
|
151
|
+
end tell
|
152
|
+
return info
|
153
|
+
END
|
154
|
+
end
|
155
|
+
|
156
|
+
private
|
157
|
+
|
158
|
+
def self.buildScript(body)
|
159
|
+
header = "using terms from application \"Spotify\""
|
160
|
+
footer = "end using terms from"
|
161
|
+
osascript <<-END
|
162
|
+
#{header}
|
163
|
+
#{body}
|
164
|
+
#{footer}
|
165
|
+
END
|
166
|
+
end
|
167
|
+
|
168
|
+
def self.osascript(script)
|
169
|
+
system 'osascript', *script.split(/\n/).map { |line| ['-e', line] }.flatten
|
170
|
+
end
|
171
|
+
|
172
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'spotify_osx_controller/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "spotify_osx_controller"
|
8
|
+
spec.version = SpotifyOsxController::VERSION
|
9
|
+
spec.authors = ["Daniel Swensson"]
|
10
|
+
spec.email = ["danielswensson@hotmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Ruby wrapper for Spotify applescript API}
|
13
|
+
spec.description = %q{Control Spotify running on a Mac}
|
14
|
+
spec.homepage = "https://github.com/DanielSwensson/SpotifyControl"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "bin"
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: spotify_osx_controller
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Swensson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Control Spotify running on a Mac
|
42
|
+
email:
|
43
|
+
- danielswensson@hotmail.com
|
44
|
+
executables:
|
45
|
+
- console
|
46
|
+
- setup
|
47
|
+
- spotify_osx_controller
|
48
|
+
extensions: []
|
49
|
+
extra_rdoc_files: []
|
50
|
+
files:
|
51
|
+
- ".DS_Store"
|
52
|
+
- ".gitignore"
|
53
|
+
- Gemfile
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- SpotifyControl.rb
|
57
|
+
- SpotifyControl.scpt
|
58
|
+
- bin/console
|
59
|
+
- bin/setup
|
60
|
+
- bin/spotify_osx_controller
|
61
|
+
- lib/spotify_osx_controller.rb
|
62
|
+
- lib/spotify_osx_controller/version.rb
|
63
|
+
- spotify_osx_controller.gemspec
|
64
|
+
homepage: https://github.com/DanielSwensson/SpotifyControl
|
65
|
+
licenses: []
|
66
|
+
metadata: {}
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 2.4.6
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: Ruby wrapper for Spotify applescript API
|
87
|
+
test_files: []
|