r/applescript • u/ProfessorPartyFetus • Dec 07 '21
A script to fade Spotify to certain volumes?
I'm trying to write an application that when executed will fade Spotify to a certain volume. If the current volume is greater than the desired volume, Spotify will decrease in increments of 1 until the desired volume is reached. It should work in reverse if the current volume is lower.
Here's what I have:
--
tell application "Spotify"
launch
repeat 100 times
if sound volume is less than 20 then
set sound volume to (sound volume + 1)
delay 0.2
end if
if sound volume is greater than 20 then
set sound volume to (sound volume - 1)
delay 0.2
end if
end repeat
end tell
--
If the current volume is greater than the desired volume, the app works perfectly! However, if the current volume is lower than the desired volume, the app will run for approximately 20 seconds after doing nothing. I'm very curious as to why it works in one direction, but not the other even when leaving in only the "if less than" loop. Any tips on how to fix this or clean up the app are welcome!
1
u/ProfessorPartyFetus Dec 09 '21
Partial Solution:
Code works great when using increments of 2 rather than 1. Not exactly sure why, but I’m sure some of the bugs explained in other comments could be it!
1
u/[deleted] Dec 07 '21
I can’t answer your question about why it works in one direction but not the other. However, if your aim is to get the volume to 20, why don’t you just do:
or even just
1
u/copperdomebodha Dec 07 '21 edited Dec 07 '21
There is a serious bug in Spotify's AppleScript implementation. It always sets the volume to one less than requested, unless the current sound volume is 40, 60, or 80 in which case it works as intended. This leaves it at 0 every time your script tries to increase it from 0 by 1. Edge cases at 0 and 100 seem problematic as well, so I just coerced it off those extremes to avoid any fuss.
This code works. Hopefully Spotify will correct their implementation. You might want to alert them to this issue. I can't be bothered as I don't use Spotify.