Created
July 13, 2018 20:57
-
-
Save samdphillips/24baf6a5889369cfc13f6feaa9316afc to your computer and use it in GitHub Desktop.
Remote control of Spotify from Racket for Mac OSX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket/base | |
#| | |
A Hack to remote control Spotify from Racket. | |
It pretty reliably will crash DrRacket if run. | |
Works better if require'd from another module, or run from command line. | |
|# | |
(require ffi/unsafe | |
ffi/unsafe/objc | |
ffi/unsafe/nsstring) | |
;; Get started by loading the Cocoa scripting bridge bundle. | |
(import-class NSBundle) | |
(define sb-bundle | |
(tell NSBundle | |
bundleWithPath: | |
#:type _NSString | |
"/System/Library/Frameworks/ScriptingBridge.framework")) | |
(unless (tell #:type _BOOL sb-bundle load) | |
(error "failed to load Cocoa scripting bridge")) | |
;; Now import the scripting bridge class and connect to the target | |
(import-class SBApplication) | |
(define spotify | |
(tell SBApplication | |
applicationWithBundleIdentifier: | |
#:type _NSString "com.spotify.client")) | |
;; /Applications/Spotify.app/Contents/Resources/Spotify.sdef is your friend | |
(define (play url) | |
(tell spotify | |
playTrack: #:type _NSString url | |
inContext: #f)) | |
(define (playpause) | |
(tell spotify playpause)) | |
(provide (prefix-out spotify: | |
(combine-out play playpause))) | |
(let ([track (tell spotify currentTrack)]) | |
(values (tell #:type _NSString track artist) | |
(tell #:type _sint32 track popularity))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment