Last active
April 18, 2021 16:17
-
-
Save mjquinn21/953738cac970871db014ce6303aded2f to your computer and use it in GitHub Desktop.
Applescript to pause media from YouTube and Spotify. Works with Google Chrome for YouTube and Chrome and the desktop client for Spotify. Useful to run with Tooth Fairy to pause music when AirPods are disconnected.
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
How to Use This Script | |
1. Open Finder | |
2. Hit ⌘⇧L (Command + Shift + L) | |
3. Locate the Scripts folder | |
4. Move Stop_Media_When_AirPods_Disconnect_For_Tooth_Fairy.scpt to ~/Library/Scripts/ | |
5. Open Tooth Fairy preferences | |
6. Select your AirPods and click “Advanced” | |
7. Make sure that “Run shell script after disconnecting” is ticked | |
8. Paste this in there: | |
#!/bin/sh | |
osascript ~/Library/Scripts/Stop_Media_When_AirPods_Disconnect_For_Tooth_Fairy.scpt | |
9. When prompted to select a director just click OK | |
10. Click OK to save the advanced preferences | |
11. Click OK to exit Tooth Fairy preferences |
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
set volume with output muted | |
delay 1.5 | |
set spotifyBeenChecked to false | |
try | |
tell application "Finder" to get application "Spotify" | |
set spotifyExists to true | |
on error | |
set spotifyExists to false | |
end try | |
if spotifyExists is true and appIsRunning("Spotify") then | |
tell application "Spotify" | |
if player state is not stopped then | |
pause | |
end if | |
end tell | |
else | |
if appIsRunning("Google Chrome") then | |
tell application "Google Chrome" | |
set foundVideo to false | |
repeat with t in tabs of windows | |
tell t | |
if URL of t contains "open.spotify.com" then | |
execute javascript "var classes = document.getElementsByClassName('control-button spoticon-pause-16 control-button--circled'); | |
var Rate = classes[0]; | |
Rate.click();" | |
end if | |
end tell | |
end repeat | |
end tell | |
end if | |
end if | |
set spotifyBeenChecked to true | |
if appIsRunning("Google Chrome") then | |
if spotifyBeenChecked is true then | |
tell application "Google Chrome" | |
set foundVideo to false | |
repeat with t in tabs of windows | |
tell t | |
if the title contains "- YouTube" then | |
execute javascript " | |
var v = document.querySelector('video'); | |
if (v.paused) { | |
// do nothing | |
} | |
else { | |
v.pause(); | |
} | |
" | |
set foundVideo to true | |
exit repeat | |
end if | |
end tell | |
end repeat | |
end tell | |
end if | |
end if | |
delay 1.5 | |
set volume without output muted | |
on appIsRunning(appName) | |
tell application "System Events" to (name of processes) contains appName | |
end appIsRunning |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment