Skip to content

Instantly share code, notes, and snippets.

@NoobsArePeople2
Last active November 10, 2023 14:33
Show Gist options
  • Star 77 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save NoobsArePeople2/5121597 to your computer and use it in GitHub Desktop.
Save NoobsArePeople2/5121597 to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link

👍 ludalex, great addition

@intel352
Copy link

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
Copy link

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
Copy link

@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
Copy link

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

@NoobsArePeople2
Copy link
Author

@ludalex Just saw your update -- very nice!

@ebeeson
Copy link

ebeeson commented Nov 3, 2014

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

@vinkla
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

This is great, and nice work ludalex! Perfect!

@bimawa
Copy link

bimawa commented Aug 12, 2020

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

@misha-tgshv
Copy link

Update. Apple Music, Spotify

Play/Pause

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

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

else if is_running("Spotify") then
    tell application "Spotify"
        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("Music") then
	tell application "Music"
		next track
	end tell
	
else if is_running("Spotify") then
	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("Music") then
	tell application "Music"
		previous track
	end tell
	
else if is_running("Spotify") then
	tell application "Spotify"
		previous track
	end tell
end if

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment