Skip to content

Instantly share code, notes, and snippets.

@samdphillips
Created July 13, 2018 20:57
Show Gist options
  • Save samdphillips/24baf6a5889369cfc13f6feaa9316afc to your computer and use it in GitHub Desktop.
Save samdphillips/24baf6a5889369cfc13f6feaa9316afc to your computer and use it in GitHub Desktop.
Remote control of Spotify from Racket for Mac OSX
#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