Recently, I decided to deploy Hackintosh at the office. Everything worked out. The only thing I didn’t like was the lack of media keys on the office keyboard. I am used to managing them through music, including on the Yandex.Music service. This article is about how I corrected the situation.
We use Apple Script to control the player
AppleScript has the ability to "communicate" with applications that support it, for this it provides the construction tell. These applications include Google Chrome browser. Below is an example of how to ask Google Chrome to open a URL in a new tab:
tell application "Google Chrome" open location "https://music.yandex.ru" end tell
Now consider the example more complicated, let's ask Google Chrome to execute JavaScript in the active tab:
')
tell application "Google Chrome" execute front window's active tab javascript "alert('example');" end tell
As a result, we will see the following picture:
Now let's write a JavaScript event that triggers a click event on the desired element, for example, on the next button on the Yanded.Music player. To do this, use querySelector:
document.querySelector('.player-controls__btn_next').click();
It remains to ask Google Chrome to run this script on the tab with the open Yandex.Music. To do this, first find the necessary tab:
tell application "Google Chrome" -- repeat with _w in (every window) -- music.yandex.ru repeat with _t in (every tab whose URL contains "music.yandex.ru") of _w -- end repeat end repeat end tell
Then we ask to execute our script on the found tab:
tell application "Google Chrome" -- repeat with _w in (every window) -- music.yandex.ru repeat with _t in (every tab whose URL contains "music.yandex.ru") of _w. set s to "document.querySelector('.player-controls__btn_next').click();" tell _t to execute javascript s end repeat end repeat end tell
In this example, we are looking for a div whose class is '.player-controls__btn_next'. To perform other actions, such as pause playback, you need to create the same AppleScript, but with the '.player-controls__btn_pause' selector. It should be understood that if Yandex changes the name of the class, then it will also need to be changed in applescript.
That's all, in order to run AppleScript by pressing the hot keys, create a service in Automator. About how to do this, there are many articles on the Internet.
Ready workflow for Automator you can download
here :
PS In order for the above to work in Safari, you need to replace the word “execute” with the word “do” in the script.
PPS In players like iTunes everything is easier. They understand play / pause commands
tell application "iTunes" to playpause