Changing system volume and changing it back to previous level

I need to turn up system volume temporarily and then set it back to the value it was before. I'm getting an error:

error "Can’t make {output volume:29, input volume:58, alert volume:46, output muted:false} into type real." number -1700 from {output volume:29, input volume:58, alert volume:46, output muted:false} to real

Does anyone know the correct way to do this? Here is my code.

tell application "Finder"
    set myVolume to get volume settings
    tell application "Finder" to set volume 5
    tell application "Finder" to set volume myVolume
end tell

The get volume settings and set volume are a part of Standard Additions in AppleScript and Finder is not needed.

The following example AppleScript code is what you want:

set myVolume to output volume of (get volume settings)
set volume output volume 5
set volume output volume myVolume

Note that you may need/want to set a short delay between the two set volume output ... lines of code.



Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.