1426298_51280144 copy.jpg

Blog

Running Applescript in the Background

Applescript usually runs in the main thread of an application. If you try to run a script with delay or a long run time, it'll hang the app. You can trick it into creating a new thread by running the Applescript in terminal shell.

do shell script "osascript -e 'line 1' -e 'line 2' -e 'line n'&> /dev/null&"

Here's a function that, given destination volume, increment (+1 or -1) and delay between increments, fades Spotify in the background.

on fadeInBackground(vol, inc, dly)
do shell script "osascript -e 'tell application \"Spotify\"' -e 'repeat with i from sound volume to " & vol & " by " & inc & "' -e 'if (sound volume - i is greater than 1) or (sound volume - i is less than -2) then exit repeat' -e 'set sound volume to i' -e 'delay " & dly & "' -e 'end repeat' -e 'end tell'&> /dev/null&"
end fadeInBackground
Alec Sparks