My personal dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

spotify-apple.el 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. ;; spotify-apple.el --- Apple-specific code for Spotify.el
  2. ;; Copyright (C) 2014-2016 Daniel Fernandes Martins
  3. ;; Code:
  4. (defcustom spotify-osascript-bin-path "/usr/bin/osascript"
  5. "Path to `osascript' binary."
  6. :type 'string)
  7. ;; Do not change this unless you know what you're doing
  8. (defvar spotify-player-status-script "
  9. tell application \"Spotify\"
  10. set playerState to get player state as string
  11. # Empty data in order to avoid returning an error
  12. if (playerState = \"stopped\") then
  13. return \"\n\n\n\n\n\nstopped\n\"
  14. end if
  15. set trackId to id of current track as string
  16. set trackArtist to artist of current track as string
  17. set trackName to name of current track as string
  18. set trackNumber to track number of current track as string
  19. set trackDiscNumber to disc number of current track as string
  20. set trackDuration to duration of current track as string
  21. set playerPosition to get player position as string
  22. return trackId & \"\n\" & trackArtist & \"\n\" & trackName & \"\n\" & trackNumber & \"\n\" & trackDiscNumber & \"\n\" & trackDuration & \"\n\" & playerState & \"\n\" & playerPosition
  23. end tell")
  24. (defun spotify-apple-command-line (cmd)
  25. "Returns a command line prefix for any Spotify command."
  26. (format "%s -e 'tell application \"Spotify\" to %s'" spotify-osascript-bin-path cmd))
  27. (defun spotify-apple-command (cmd)
  28. "Sends the given command to the Spotify client and returns the resulting
  29. status string."
  30. (replace-regexp-in-string
  31. "\n$" ""
  32. (shell-command-to-string (spotify-apple-command-line cmd))))
  33. (defun spotify-apple-player-status-command-line ()
  34. "Returns the current Spotify player status that is set to the mode line."
  35. (format "echo %s | %s"
  36. (shell-quote-argument spotify-player-status-script)
  37. spotify-osascript-bin-path))
  38. (defun spotify-apple-set-mode-line-from-process-output (process output)
  39. "Sets the output of the player status process to the mode line."
  40. (spotify-replace-mode-line-flags output)
  41. (with-current-buffer (process-buffer process)
  42. (delete-region (point-min) (point-max))))
  43. (defun spotify-apple-player-status ()
  44. "Updates the mode line to display the current Spotify player status."
  45. (let* ((process-name "spotify-player-status")
  46. (process-status (process-status process-name)))
  47. (if (and (spotify-connected-p) (not process-status))
  48. (let ((process (start-process-shell-command process-name "*spotify-player-status*" (spotify-apple-player-status-command-line))))
  49. (set-process-filter process 'spotify-apple-set-mode-line-from-process-output))
  50. (spotify-update-mode-line ""))))
  51. (defun spotify-apple-player-state ()
  52. (spotify-apple-command "get player state"))
  53. (defun spotify-apple-player-toggle-play ()
  54. (spotify-apple-command "playpause"))
  55. (defun spotify-apple-player-next-track ()
  56. (spotify-apple-command "next track"))
  57. (defun spotify-apple-player-previous-track ()
  58. (spotify-apple-command "previous track"))
  59. (defun spotify-apple-toggle-repeat ()
  60. (spotify-apple-command "set repeating to not repeating"))
  61. (defun spotify-apple-toggle-shuffle ()
  62. (spotify-apple-command "set shuffling to not shuffling"))
  63. (defun spotify-apple-player-play-track (track-id context-id)
  64. (spotify-apple-command (format "play track \"%s\" in context \"%s\"" track-id context-id)))
  65. (defun spotify-apple-player-pause ()
  66. (spotify-apple-command "pause"))
  67. (provide 'spotify-apple)