Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Configure media keys on a non-Apple keyboard to control Spotify via AppleScript and USB Overdrive on OSX.

Requirements

  1. USB Overdrive
  2. A non-Apple keyboard with media keys (or keys you want to make "media" keys). For reference, I'm using a Microsoft Sidewinder X4

Set Up

  1. Plug in your keyboard and install USB Overdrive.
  2. Open USB Overdrive. Click into the Settings tab.
  3. Click the dropdown and select "Any Keyboard, Any Application"
  4. Click the "Scan Next Track" item in the left-hand list. You can also select this key by focusing the USB Overdrive window and pressing the actual key on the keyboard.
  5. In the right-hand column click the dropdown and select "Execute AppleScript".
  6. Paste the following AppleScript:
    tell application "Spotify"
    next track
    end tell
    
  7. Repeat steps 4 and 5 but select "Scan Previous Track". Paste the following AppleScript:
    tell application "Spotify"
    previous track
    end tell
    
  8. Repeat steps 4 and 5 but select "Play/Pause". Paste the following AppleScript:
     tell application "Spotify"
        playpause
     end tell
     
@ludalex

This comment has been minimized.

Copy link

@ludalex ludalex commented Nov 8, 2013

Even better, these detect if iTunes or Spotify are running and take action accordingly:

Next:

on is_running(appName)
    tell application "System Events" to (name of processes) contains appName
end is_running

if is_running("iTunes") then
    tell application "iTunes"
        next track
    end tell
else
    tell application "Spotify"
        next track
    end tell
end if

Previous:

on is_running(appName)
    tell application "System Events" to (name of processes) contains appName
end is_running

if is_running("iTunes") then
    tell application "iTunes"
        previous track
    end tell
else
    tell application "Spotify"
        previous track
    end tell
end if

Play/Pause:

on is_running(appName)
    tell application "System Events" to (name of processes) contains appName
end is_running

if is_running("iTunes") then
    tell application "iTunes"
        playpause
    end tell
else
    tell application "Spotify"
        playpause
    end tell
end if
@phillipuniverse

This comment has been minimized.

Copy link

@phillipuniverse phillipuniverse commented Apr 21, 2014

👍 ludalex, great addition

@intel352

This comment has been minimized.

Copy link

@intel352 intel352 commented Aug 21, 2014

For Radiant Player (Google Music client), see below. Could likely just extend the original script to support Itunes, Radiant, and Spotify, but I don't use Spotify, wasn't a concern for me.

Next:

on is_running(appName)
    tell application "System Events" to (name of processes) contains appName
end is_running

if is_running("iTunes") then
    tell application "iTunes"
        next track
    end tell
else
    tell application "Radiant Player"
        next track
    end tell
end if

Previous: (note: 'back track' for Radiant)

on is_running(appName)
    tell application "System Events" to (name of processes) contains appName
end is_running

if is_running("iTunes") then
    tell application "iTunes"
        previous track
    end tell
else
    tell application "Radiant Player"
        back track
    end tell
end if

Play/Pause:

on is_running(appName)
    tell application "System Events" to (name of processes) contains appName
end is_running

if is_running("iTunes") then
    tell application "iTunes"
        playpause
    end tell
else
    tell application "Radiant Player"
        playpause
    end tell
end if
@nucatus

This comment has been minimized.

Copy link

@nucatus nucatus commented Aug 22, 2014

Is there an viable alternative to USB Overdrive? Sincerely, I wouldn't pay $20 to enable the media keys on non-Apple keyboards.

@psychoticpanda

This comment has been minimized.

Copy link

@psychoticpanda psychoticpanda commented Sep 5, 2014

@nucatus - There is no charge, it's shareware. Just click download instead of buy/purchase. Donations are not required but suggested if you enjoy the software! :)

@leeavital

This comment has been minimized.

Copy link

@leeavital leeavital commented Sep 24, 2014

I'm using quicksilver for binding to function keys. It's opensource and free :)

@NoobsArePeople2

This comment has been minimized.

Copy link
Owner Author

@NoobsArePeople2 NoobsArePeople2 commented Oct 10, 2014

@ludalex Just saw your update -- very nice!

@ebeeson

This comment has been minimized.

Copy link

@ebeeson ebeeson commented Nov 3, 2014

fwiw, these instructions also work great to get Keyboard Maestro controlling Vox (media player).

@vinkla

This comment has been minimized.

Copy link

@vinkla vinkla commented Dec 3, 2014

@leeavital do you have any example of the binding? It doesn't seem that the media keys have any key code?

@alexchantastic

This comment has been minimized.

Copy link

@alexchantastic alexchantastic commented Mar 17, 2015

FYI, there has been a bug in Spotify 1.0.1.x that breaks Applescript functionality. You'll need to edit a plist file in order to get it working again. Details here: https://community.spotify.com/t5/Help-Desktop-Linux-Mac-and/New-Mac-Update-AppleScript-support-removed/m-p/1044011#M112587

I also had to logout/restart before USB Overdrive/Spotify accepted the modified plist fix.

@Hugocorp974

This comment has been minimized.

Copy link

@Hugocorp974 Hugocorp974 commented Oct 29, 2015

As i'm using iTune, Spotify & Deezer :

PLAY/PAUSE
on is_running(appName)
    tell application "System Events" to (name of processes) contains appName
end is_running

if is_running("iTunes") then
    tell application "iTunes"
        playpause
    end tell

else if is_running("Spotify") then
    tell application "Spotify"
        playpause
    end tell

else
    tell application "Deezer"
        playpause
    end tell
end if

NEXT
on is_running(appName)
    tell application "System Events" to (name of processes) contains appName
end is_running

if is_running("iTunes") then
    tell application "iTunes"
        next track
    end tell

else if is_running("Spotify") then
    tell application "Spotify"
        next track
    end tell

else
    tell application "Deezer"
        next track
    end tell
end if

@amjames

This comment has been minimized.

Copy link

@amjames amjames commented Dec 30, 2015

@nucatus a bit late to the game, but if your still looking...

I am using better touch tool to run scripts like this. You can set the app up to use a keyboard shortcut, or lots of other gestures/stuff with apple touch-pads or mouses, to trigger a script or a ton of other built in functions. Very useful tool it's free, and it works with any keyboard.

@rabdill

This comment has been minimized.

Copy link

@rabdill rabdill commented Mar 22, 2017

Another brief note, because this was really helpful here in 2017: I was able to get this stuff working without installing anything that wasn't in stock OSX, but probably at least partially because I'm just using regular old keybindings to play/pause/etc. If you open the "Automator" tool, it will let you basically just create a "service" that runs arbitrary AppleScript. Once you open Automator, the "Utilities" library on the left side offers a "Run AppleScript" option -- clicking that gives you a little template where you can paste the code above. (Make sure in the "receives input in any application" drop-down is actually changed to "no input.")

Once the service is created, go to System Prefs > Keyboard > Shortcuts. In the "Services" section, your new one should show up at the bottom -- just give it a shortcut and you're all set.

@JSR-Gaming-x

This comment has been minimized.

Copy link

@JSR-Gaming-x JSR-Gaming-x commented Apr 29, 2019

This is great, and nice work ludalex! Perfect!

@bimawa

This comment has been minimized.

Copy link

@bimawa bimawa commented Aug 12, 2020

Cool, but how to like track and unlike? Or mark it like "Shitmusicforme"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.