repeat #Repeat forever
try #Try to do this thing, and on error... (look at "on error" at the bottom)
tell application "Spotify"
#Setting variables
set cur_vol to sound volume
set trackk to track number of current track
set namme to name of current track
set pop to popularity of current track
set dur to duration of current track
set pos to player position
end tell
if pop is 0 and dur is less than 40 then #If popularity is 0 and duration of current track is less then 40
tell application "Spotify" #Do our thing
pause
set sound volume to 0
play
end tell
delay dur - pos + 1 #When an ad ends,
tell application "Spotify"
set sound volume to cur_vol #restore the volume to the level it was before
end tell
else
delay 0.5 #If nothing worked (it must be a music track, not an ad), try and run this script again in 0.5 seconds.
end if
on error errorMessage #On any error
delay 0.5 #Run this script again in 0.5 seconds
end try
end repeat