Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
Great job! Thanks! –  gentmatt Mar 2 '12 at 21:17
add comment

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 &
share|improve this answer
    
Did not help :( But thanks! –  gentmatt Mar 2 '12 at 19:04
    
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 '12 at 19:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.