10.6: Create global hotkeys to control Google Music playback

Jun 23, '11 07:30:00AM

Contributed by: nathanator11

Snow Leopard only hintI've been enjoying the Google Music Beta, but I hated having to go to my Web browser, to the Google Music tab, and click the button. So, I wrote some services to create global hotkeys to play/pause, skip forward/back, and toggle shuffle.

To create a service, open Automator. When asked to choose a template, select Service. Our service will have only one block: Run AppleScript. Type that into the search bar at the top left and drag the Run AppleScript item to the project area on the right.

Replace the code in the box already with this:
on run
  tell application "Google Chrome"
    set allWins to every window
    set allTabs to {}
    repeat with currWin in allWins
      set allTabs to allTabs & every tab of currWin
    end repeat
    repeat with currTab in allTabs
      try
        if ((characters -10 thru -1 of (title of currTab as string)) as string) = "Music Beta" then set musicTab to currTab
      end try
    end repeat
    tell musicTab to execute javascript "SJBpost('playPause');"
  end tell
end run
If you're using Google Chrome, leave the code as is. If you're using Safari, change the 'Google Chrome' in the first line to 'Safari,' change the word title (near the middle) to name, and change the word execute on the third-to-last line to do.

Now, change the Service Receives menu to no input. Save the service somewhere safe but out-of-the-way.

Now, go to System Preferences » Keyboard » Keyboard Shortcuts » Services. Scroll down to the General section and find the service we just made. Click on the right column and type a keyboard shortcut (make sure it isn't used for anything else).

Now, from where ever you are you can press that shortcut and Google Music will play or pause playback.

To create shortcuts for the other functions, repeat the above steps and change one line of the code: change playPause (near the end) to prevSong. Save that service and give it a unique shortcut. Do this again with nextSong. And again with toggleShuffle.

Now you can control Google Music playback with keyboard shortcuts from everywhere.

If you are so inclined, you could use apps like KeyRemap4MacBook to remap the built-in media keys to F-keys, and use an app like Butler to assign these scripts to those F-keys.

[crarko adds: I haven't tested this one.]

Comments (11)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20110622061755509