Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.6: Create global hotkeys to control Google Music playback Apps
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.]
    •    
  • Currently 3.00 / 5
  You rated: 2 / 5 (7 votes cast)
 
[19,009 views]  

10.6: Create global hotkeys to control Google Music playback | 11 comments | Create New Account
Click here to return to the '10.6: Create global hotkeys to control Google Music playback' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.6: Create global hotkeys to control Google Music playback
Authored by: adlr on Jun 24, '11 04:45:52PM

Here's one that seems to work for Grooveshark:

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) = "rooveshark" then set musicTab to currTab
end try
end repeat
tell musicTab to execute javascript "if(GS.player.isPaused) { GS.player.resumeSong(); } else if(GS.player.isPlaying) { GS.player.pauseSong(); } else { GS.player.playSong(); }"
end tell
end run



[ Reply to This | # ]
10.6: Create global hotkeys to control Google Music playback
Authored by: i5ao on Jul 15, '11 04:26:07PM
applescript tip: instead of looping, you can use something like this:

tell application "Google Chrome"
	get first tab of every window whose title begins with "Foo"
end tell
you can also say "contains" or "ends with" etc.

[ Reply to This | # ]
10.6: Create global hotkeys to control Google Music playback
Authored by: roboguy on Nov 27, '11 07:11:45PM
This works amazingly! The only thing is, now that it's officially launched, this script has an error. The line where it says
if ((characters -10 thru -1 of (title of currTab as string)) as string) = "Music Beta" then set musicTab to currTab now needs to be
if ((characters -12 thru -1 of (title of currTab as string)) as string) = "Google Music" then set musicTab to currTab, or else it won't work properly. (Technically, it could analyze any number of characters from the title, just as long as its consistent! If you wanted to keep it from -10 to -1, you would have to test for equality to "ogle Music".) Cheers, OP and good work on this! :)

[ Reply to This | # ]
10.6: Create global hotkeys to control Google Music playback
Authored by: scottlee on Jan 09, '12 01:19:59PM

Thanks for the update! This worked for me too.



[ Reply to This | # ]
10.6: Create global hotkeys to control Google Music playback
Authored by: regulus on Jan 10, '12 02:33:52AM
A comment about the applescript code (I understand you didn't write it)... a better way to do it would be the following. This removes most of the "as string" stuff which isn't necessary.

if text -12 thru -1 of (title of currTab) is "Google Music" then set musicTab to currTab

An even better way where you won't have to make changes every time the title spacing changes...

if (title of currTab) contains "Google Music" then set musicTab to currTab

---
Hank
http://www.hamsoftengineering.com


[ Reply to This | # ]
10.6: Create global hotkeys to control Google Music playback
Authored by: kcee on Mar 15, '12 04:23:34PM

This is not working anymore. The tabs in google chrome now contains name of the song title and does not contain "Google Music" nor "Google Beta" so it is not getting detected. I was thinking keeping the google music tab on the first tab position might solve it but I am not sure how to go about modifying the existing AppleScript code given in the original doc.

Here is what I have:
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) = "Google Music" then set musicTab to currTab
end try
end repeat
tell musicTab to execute javascript "SJBpost('playPause');"
end tell
end run


Can anyone modify the AppleScript please?



[ Reply to This | # ]
10.6: Create global hotkeys to control Google Music playback
Authored by: drumdbeat on Apr 21, '12 02:46:26AM

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 -11 thru -1 of (title of currTab as string)) as string) = "Google Play" then set musicTab to currTab
end try
end repeat
tell musicTab to execute javascript "SJBpost('playPause');"
end tell
end run



[ Reply to This | # ]
10.6: Create global hotkeys to control Google Music playback
Authored by: seanstar12 on Mar 27, '12 11:47:43PM
I am not an apple script guru, but I hacked this together to allow me to search and auto-play along with this. The script prompts for a search, then reloads the page (to 'stop the current playing song'), enters in your new search, and starts playing.

activate
(display dialog "Google Music: Search" default answer "")
set searchResult to text returned of result

set the searchResult to replace_chars(searchResult, " ", "%20")

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 (title of currTab) contains "Google Play" then set musicTab to currTab
		end try
	end repeat
	tell musicTab to execute javascript "document.location.reload();"
	delay 2
	set URL of musicTab to "https://play.google.com/music/listen?u=0#" & searchResult & "_sr"
	tell musicTab to execute javascript "SJBpost('playPause');"
end tell

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars


[ Reply to This | # ]
10.6: Create global hotkeys to control Google Music playback
Authored by: emale on Sep 26, '12 01:45:51AM
Exactly what I've searched for.

Safari useres also have to change another part of the code: It's "do javascript" instead of "execute javascript".
Also reduced the code a little bit and made it checking the URL instead to the tab title. Seems to be more reliable.
It looks like that:

tell application "Safari"
	set allTabs to (every tab of every window)
	set allTabs to item 1 of allTabs --don't ask me why but otherwise it didn't work. Maybe it's a problem with a extension for me
	repeat with currTab in allTabs
		set theURL to (URL of currTab) as string
		if "play.google.com/music" is in theURL then tell currTab to do JavaScript "SJBpost('playPause');" --or  nextSong or prevSong
	end repeat
end tell


[ Reply to This | # ]
10.6: Create global hotkeys to control Google Music playback
Authored by: Windgazer on Nov 26, '12 02:18:37PM

Since I've recently started gathering what scripts I make use of and try to clean them as much as I can I based some script for working with Google Play on the comments out here and published them on github.

If any of you find them of use, or can make improvements, fork away!! (BTW, I've just finished creating a growl script that shows song-title, artist and album-art...)



[ Reply to This | # ]
10.6: Create global hotkeys to control Google Music playback
Authored by: kabadisha on Apr 02, '13 12:38:30AM
Hey Guys,

I have picked this up, updated it and added support for The Hype Machine also - I use this along side Google Music.

You can find the updated AppleScripts and install guide here: charleswilkinson.co.uk/2013/04/01/google-music-hype-machine-keyboard-controls-mac-osx/

Sorry I can't seem to post an actual link - my comment get's deleted as spam automatically if I do :-(

Cheers,
Charlie



[ Reply to This | # ]