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