-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounsel-itunes.el
258 lines (225 loc) · 7.79 KB
/
counsel-itunes.el
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
;;; counsel-itunes.el - Itunes interaction with `ivy'.
;;
;; Copyright (c) 2012-2017 Jacob Chaffin
;;
;; Author: Jacob Chaffin <jchaffin@ucla.edu>
;; Keywords: itunes, macOS, counsel, ivy
;; Homepage: https://github.com/jchaffin/dotemacs
;; Package-Requires: ((emacs "25") (ivy "0.10.0"))
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
;;; Commentary:
;; An Emacs interface to Itunes, using ivy and counsel.
;; Based off of itunes.el
;; https://www.emacswiki.org/emacs/itunes.el
;;; End Commentary
;;; Code:
(require 'ivy)
(defvar counsel-itunes--text-item-delimiter ",,," )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Current Track Information
(defun counsel-itunes//current-track-info ()
(do-applescript
(concat
"local trackInfo\n"
"tell application \"iTunes\"\n"
" set trackInfo to get {name,artist,time,album} of current track\n"
"end tell\n"
"set text item delimiters to \"" counsel-itunes--text-item-delimiter "\"\n"
"get trackInfo as text")))
(defun counsel-itunes//current-track-time ()
(interactive)
(destructuring-bind (timeLeft trackDuration)
(split-string
(do-applescript
(concat
"tell application \"iTunes\"\n"
" set timeLeft to player position\n"
" set totalDuration to time of current track\n"
"end tell\n"
"set text item delimiters to \"" counsel-itunes--text-item-delimiter "\"\n"
"get {timeLeft,totalDuration} as text"))
counsel-itunes--text-item-delimiter
t)
(if (interactive-p)
(if (= (string-to-number timeLeft) 0.0)
(message "%s" trackDuration)
(message "%s of %s"
(format-seconds "%m:%.2s"
(string-to-number timeLeft))
trackDuration))
(list timeLeft trackDuration))))
;;;###autoload
(defun counsel-itunes-current-track ()
"Reports the name, artists, time, and album (if available)
to the mini-buffer. "
(interactive)
(destructuring-bind (name artist time &optional album)
(split-string
(counsel-itunes//current-track-info)
counsel-itunes--text-item-delimiter
t)
(if (interactive-p)
(message "%s by %s -- %s %s" name artist (or album "")
(funcall-interactively 'counsel-itunes//current-track-time))
(list name artist album time))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Playlists
(defun counsel-itunes//playlists-list ()
;; (interactive)
(do-applescript
(concat
"local itunesPlaylists\n"
"tell application \"iTunes\"\n"
" set itunesPlaylists to get name of playlists\n"
"end tell\n"
"set text item delimiters to \"" counsel-itunes--text-item-delimiter "\"\n"
"get itunesPlaylists as text")) )
(defun counsel-itunes//select-playlist (playlist-name)
;;(interactive)
(do-applescript
(concat
"tell application \"iTunes\"\n"
" play playlist \"" playlist-name "\"\n"
"end tell")))
(defun counsel-itunes//playlist-menu (&optional display-track-menu)
;; (interactive)
(ivy-read
"iTunes Playlists: "
(split-string
(counsel-itunes//playlists-list)
counsel-itunes--text-item-delimiter
t)
:action (lambda (playlist-name)
(if display-track-menu
(counsel-itunes//tracklist-menu playlist-name)
(counsel-itunes//select-playlist playlist-name))
(funcall-interactively 'counsel-itunes-current-track))))
;;;###autoload
(defun counsel-itunes-playlist ()
"Constructs an ivy selection menu consisting
of iTunes playlists."
(interactive)
(counsel-itunes//playlist-menu nil))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Tracklist
(defun counsel-itunes//get-tracklist (playlist-name)
(do-applescript
(concat
"local trackList\n"
"tell application \"iTunes\"\n"
" set trackList to get name of tracks in playlist \"" playlist-name "\"\n"
"end tell\n"
"set text item delimiters to \"" counsel-itunes--text-item-delimiter "\"\n"
"get trackList as text")))
(defun counsel-itunes//tracklist-from-playlist (playlist-name track-name)
(do-applescript
(concat
"tell application \"iTunes\"\n"
" play track \"" track-name "\" in playlist \"" playlist-name "\"\n"
"end tell")))
(defun counsel-itunes//tracklist-menu (playlist-name)
(interactive)
(ivy-read
(concat "Playlist '" playlist-name "' Tracklist: ")
(split-string
(counsel-itunes//get-tracklist playlist-name)
counsel-itunes--text-item-delimiter
t)
:action (lambda (track-name)
(counsel-itunes//tracklist-from-playlist playlist-name track-name))))
;;;###autoload
(defun counsel-itunes-tracklist ()
"Constructs an ivy menu for interactively selecting
a track to play from a given playlist."
(interactive)
(counsel-itunes//playlist-menu t))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Actions and State
;;;###autoload
(defun counsel-itunes-play-pause ()
"Toggles the play/paused state of the current track in iTunes."
(interactive)
(do-applescript
"tell application \"iTunes\" to playpause"))
;; Volume Adjustment
(defun counsel-itunes//adjust-volume-by (decinc n)
(do-applescript
(concat
"tell application \"iTunes\"\n"
" set sound volume to sound volume" decinc " " (number-to-string n) "\n"
"end tell")))
;;;###autoload
(defun counsel-itunes-volume-down (&optional n)
"Decrement the iTunes.app volume by 10.
If the optional prefix argument N is specified, then
the volume will be decreased by that amount instead."
(interactive "P")
(if (number-or-marker-p n)
(counsel-itunes//adjust-volume-by "-" n)
(counsel-itunes//adjust-volume-by "-" 10)))
;;;###autoload
(defun counsel-itunes-volume-up (&optional n)
"Increment the volume in Itunes.app by 10.
If the optional prefix argument N is specified, then
the volume will be increased by that amount instead."
(interactive "P")
(if (number-or-marker-p n)
(counsel-itunes//adjust-volume-by "+" n)
(counsel-itunes//adjust-volume-by "+" 10)))
;; Track Traversal
;;;###autoload
(defun counsel-itunes-next-track (&optional n)
"Go to the next track in the current playlist.
If the optional prefix argument N is specified, then
go the Nth next."
(interactive "P")
(do-applescript
(format
(concat
"tell application \"iTunes\"\n"
" repeat %d times\n"
" next track\n"
" end repeat\n"
"end tell")
(or n 1)))
(funcall-interactively 'counsel-itunes-current-track))
;;;###autoload
(defun counsel-itunes-previous-track (&optional n)
"Go to the previous track in the current playlist.
If the optional prefix argument N is specified, then
go to the Nth previous."
(interactive "P")
(do-applescript
(format
(concat
"tell application \"iTunes\"\n"
" repeat %d times\n"
" back track\n"
" end repeat\n"
"end tell")
(or n 1)))
(funcall-interactively 'counsel-itunes-current-track))
;;;###autoload
(defun counsel-itunes-shuffle ()
"Toggle shuffle mode of the current playlist."
(interactive)
(let ((toggle (do-applescript
(concat
"local shuffleMode\n"
"tell application \"iTunes\"\n"
" if shuffle enabled then\n"
" set shuffle enabled to false\n"
" else\n"
" set shuffle enabled to true\n"
" end if\n"
"set shuffleMode to shuffle enabled\n"
"end tell\n"
"get shuffleMode as text"))))
(message "Shuffle mode %s."
(if (string= toggle "true")
"enabled" "disabled"))))
(provide 'counsel-itunes)
;;; counsel-itunes.el ends here.