2

I use saavn.com for listening to bollywood music. It's a great service but they don't have native mac app so I can't control it using keyboard media keys.

There must be a way to use keyboard media keys to control all music.

Karan
  • 55,947
  • 20
  • 119
  • 191
Amit Vaghela
  • 123
  • 5
  • 1
    It's generally difficult/impossible to intercept the special KB media keys. Using a custom [userscript](http://userscripts.org) you should be able to inject JS into the page that will assign keyboard shortcuts to the various play/pause etc. functions. I doubt such a script already exists for this site, but you never know. There might also be scripts for similar sites that can be suitably modified. – Karan May 16 '13 at 02:03

2 Answers2

6

You could assign a shortcut to a script like this:

tell application "Safari"
    repeat with t in documents
        tell t
            if URL starts with "http://www.saavn.com/" then
                do JavaScript "e = document.querySelectorAll('#pause:not(.hide)')[0] || document.querySelectorAll('#play:not(.hide)')[0]; e.click()"
                exit repeat
            end if
        end tell
    end repeat
end tell

A similar script for YouTube and Chrome:

tell application "Google Chrome"
    repeat with t in tabs of windows
        tell t
            if URL starts with "http://www.youtube.com" then
                execute javascript "player = document.querySelectorAll('#player embed')[0]
if (player) {
    player.getPlayerState() == 1 ? player.pauseVideo() : player.playVideo()
} else { // if youtube.com/html5 is enabled
    document.querySelectorAll('.html5-player-chrome > button:first-child')[0].click()
}"
                exit repeat
            end if
        end tell
    end repeat
end tell
Lri
  • 40,894
  • 7
  • 119
  • 157
  • the query selector string no longer works. use `embed#movie_player`. – fent Oct 14 '13 at 18:42
  • @thisisadeadend Thanks, I replaced `#watch7-player embed` with `#player embed`. – Lri Oct 14 '13 at 18:51
  • 1
    I've made a few more scripts for some common youtube actions and put them up here https://gist.github.com/fent/7763775 enjoy – fent Dec 06 '13 at 17:55
  • Can't upvote this enough. – dev Mar 03 '15 at 22:31
  • I couldn't get this script to work for Safari nor the one you posted in AskDifferent. But the script in your github page worked fine. You might want to update the answer. – Alex B Jun 16 '15 at 17:29
0

For the keyboard part I would use keyremap4macbook - this will allow you to intercept media keys. I can also launch scripts for keypresses (@lauri-rantas answer covers that nicely). Unfortunately it also needs some coding to adjust it to ones needs.

What I like about it is:

  • you will even be to override the media keys and "caps-lock"
  • one can also differenciate between left-shift, control, command keys and the corresponding right ones
  • the remappings can depend on which app is in foreground - so your mapping won't interfere with other media players
bdecaf
  • 468
  • 1
  • 5
  • 14