Last active
December 9, 2018 16:53
-
-
Save joxl/008cc0a37a5fea65b7e5 to your computer and use it in GitHub Desktop.
Control Pandora Radio in Google Chrome via applescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/osascript | |
(* | |
This code is a modified version of `chrome.applescript` found in | |
[googlemusic-hotkeys][1] on github. | |
[chrome.applescript][2] by [Steven La][3], unlicensed | |
[1]: https://github.com/stevenla/googlemusic-hotkeys | |
[2]: https://github.com/stevenla/googlemusic-hotkeys/blob/c2f6f8c838df906f1d8170c3d723f403aaa8d598/chrome.applescript | |
[3]: https://github.com/stevenla | |
*) | |
-------------------------------------------------------------------------------- | |
(* NOTES: | |
-- javascript snippets: | |
// pandora: toggle play/pause | |
var buttons = ['playButton', 'pauseButton']; | |
var elem; | |
for (index=0; index < buttons.length; ++index) { | |
elem = document.getElementsByClassName(buttons[index])[0]; | |
if (elem.style.display != 'none') { | |
elem.click(); | |
break; | |
} | |
} | |
// pandora: pause | |
var elem = document.getElementsByClassName('pauseButton')[0]; | |
if (elem.style.display != 'none') { | |
elem.click(); | |
} | |
*) | |
-- Open new tab to `radio_url` | |
on openWindow(radio_url) | |
tell application "Google Chrome" | |
--activate | |
tell window 0 -- this is the active window? | |
set new_tab to make new tab | |
tell new_tab to set URL to radio_url | |
end tell | |
end tell | |
end openWindow | |
on clickScript(button_class) | |
return "var elem = document.getElementsByClassName('" & button_class & "')[0]; if (elem.style.display != 'none') { elem.click(); }" | |
end clickScript | |
-- Acts on first tab whose URL matches `radio_url` | |
-- Returns true if `radio_url` tab is found, false otherwise | |
on sendCommand(cmd, radio_url) | |
-- scripts | |
set script_playpause to "var buttons = ['playButton', 'pauseButton']; var elem; for (index=0; index < buttons.length; ++index) { elem = document.getElementsByClassName(buttons[index])[0]; if (elem.style.display != 'none') { elem.click(); break; } }" | |
set script_play to clickScript("playButton") | |
set script_pause to clickScript("pauseButton") | |
set script_skip to clickScript("skipButton") | |
set script_voteup to clickScript("thumbUpButton") | |
set script_votedown to clickScript("thumbDownButton") | |
tell application "Google Chrome" | |
-- iterate over windows | |
repeat with window_ in (every window) | |
-- iterate over tabs | |
repeat with tab_ in (every tab whose URL contains radio_url) of window_ | |
if cmd = "play" then | |
tell tab_ to execute javascript script_play | |
else if cmd = "playpause" then | |
tell tab_ to execute javascript script_playpause | |
else if cmd = "pause" then | |
tell tab_ to execute javascript script_pause | |
else if cmd = "skip" then | |
tell tab_ to execute javascript script_skip | |
else if cmd = "upvote" then | |
tell tab_ to execute javascript script_voteup | |
else if cmd = "downvote" then | |
tell tab_ to execute javascript script_votedown | |
end if | |
return true | |
end repeat | |
end repeat | |
end tell | |
return false | |
end sendCommand | |
on run argv | |
set usage to "USAGE: pandoractl.scpt {play,playpause,pause,skip,upvote,downvote}" | |
set commands to {"play", "playpause", "pause", "skip", "upvote", "downvote"} | |
set radio_url to "http://www.pandora.com/" | |
if count of argv is equal to 1 then | |
set cmd to item 1 of argv | |
else | |
-- returning a string prints to terminal? Whatever. | |
return usage & "\nerror: invalid arguments" | |
end if | |
if commands does not contain cmd then | |
return usage & "\nerror: invalid command: " & cmd | |
end if | |
if sendCommand(cmd, radio_url) is false and cmd is "play" then | |
openWindow(radio_url) | |
end if | |
end run |
To run this using global hotkeys, I'm using Spark.app (you can probably get it to work with whatever global hotkey app you prefer).
For example, in Spark.app I have a hotkey sequence titled "Play/Pause" which runs the following applescript:
if isAppLoaded("iTunes") then
tell application "iTunes" to playpause
else if isAppLoaded("Google Chrome") then
set pandoractl to alias ((path to home folder as text) & "Code:gist:pandoractl:pandoractl.scpt")
run script (pandoractl) with parameters {"playpause"}
end if
on isAppLoaded(app_name)
tell application "System Events" to (name of processes) contains app_name
end isAppLoaded
Change the Code:gist:pandoractl:pandoractl.scpt
path to wherever you store the script.
If you don't want iTunes to take precedence, just remove the if isAppLoaded("iTunes") then...
part :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is a modified version of
chrome.applescript
found in googlemusic-hotkeys on github.chrome.applescript by Steven La, unlicensed