3

Background

For fine volume adjustment I use the following AppleScript:

try
    do shell script "killall afplay"
end try
set currentVolume to output volume of (get volume settings)
set volume output volume (currentVolume + 2)
    -- play volume changing sound at new volume
do shell script "afplay /System/Library/LoginPlugins/BezelServices.loginPlugin/Contents/Resources/volume.aiff"

This will increase the volume by 2% and play the new volume once like usual.

Problem

Using this script has disadvantages:

  • When I trigger the script quickly multiple times in a row, it does not respond as quickly as the 'normal' volume change would.
  • For every execution the volume sound is played at full length.

It appears that my problem is due to a limitation of the how the scripts are triggered. E.g. When I use FastScript, it will not execute the script again before it has been finished. The same goes for Quicksilver.

However, when I assign the "lower volume script" to Quicksilver and the "louder volume script" to FastScripts I can trigger both scripts very fast after each other - so: the script is fine. I believe that my problem is due to how the scripts are triggered.

Question

How can I successively trigger a script very fast?

2 Answers 2

2

Adding > /dev/null 2>&1 & to the end of the do shell script command should work. This redirects the stdout and stderr of the do shell script command to /dev/null and runs the shell script in the background, meaning that AppleScript no longer needs to wait for the command to return stdout or stderr before the AppleScript script can finish. That means that it finishes and can be run again much more quickly.

You also shouldn't need the try handler at the beginning. The following modified script should work:

set currentVolume to output volume of (get volume settings)
set volume output volume (currentVolume + 2)
-- play volume changing sound at new volume
do shell script "afplay /System/Library/LoginPlugins/BezelServices.loginPlugin/Contents/Resources/volume.aiff > /dev/null 2>&1 &" 

I got this tip from MacScripter (an excellent resource for AppleScripting) in a post about running multiple instances of a shell script.

0
1

How about adding an ampersand to the end of the afplay line so the scripted part plays in background? Like this

afplay /System/Library/LoginPlugins/BezelServices.loginPlugin/Contents/Resources/volume.aiff &
1
  • I did get the syntax of that slightly wrong (quotes in the wrong place). It should have been afplay /System/Library/LoginPlugins/BezelServices.loginPlugin/Contents/Resources/volume.aiff &. Not saying that will help, just wanting to correct myself :-)
    – binarybob
    Mar 2, 2012 at 19:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .