Implementing keyboard volume control buttons in Applescript - setting volume inside loop doesn't work

一笑奈何 提交于 2019-12-05 11:10:39

I have no idea how to get the translucent overlay to show up, but this at least plays the system beep while turning the volume up:

set currentVolume to output volume of (get volume settings)
set newVolume to (currentVolume + (100 / 17)) as integer
set volume output volume newVolume
beep

Replace the + with a - in your volume-down script.

set volume output seems to be automatically adjust values outside the (0, 100) limit.

Update: You could use Growl's AppleScript support to show some kind of overlay:

tell application "GrowlHelperApp"

    register as application "Volume Change" ¬
        all notifications {"Volume Change"} ¬
        default notifications {"Volume Change"} ¬
        icon of application "Script Editor"

    notify with name "Volume Change" ¬
        title "Volume Up" ¬
        description "Volume is now " & output volume of (get volume settings) ¬
        application name "Volume Change"

end tell

You can also use the built in notification

set vol to ((output volume of (get volume settings)) + 5)
if (vol > 100) then set vol to 100
set volume output volume (vol)

display notification ((vol) as string)

With a paid nerd's suggestions for crafting a Growl notification, I made a Mute/Unmute version of this that displays a Growl notification, but only for a brief moment.

Since I couldn't find a way to specify a growl duration and style, I used applescript to read the current growl defaults, switch over to a simple Growl style (Smoke, since it shows just a little window in the upper right corner, though you could adapt this script to Bezel for behavior that more perfectly mirrors the system), drops the delay to 0.3 seconds, growls, then reverts growl's prefs.

The script is a bit verbose, as I have mostly forgotten applescript. I'm sure there's a way to simplify this much more.

set _muted to (get (output muted of (get volume settings)))
set str to "Muted"
if _muted is false then
    set volume with output muted
else
    set volume without output muted
    set str to "" & output volume of (get volume settings)
end if

set oldStyle to (do shell script "defaults read com.Growl.GrowlHelperApp GrowlDisplayPluginName")

do shell script "defaults write com.Growl.GrowlHelperApp GrowlDisplayPluginName Smoke"

set alpha to (do shell script "defaults read com.Growl.GrowlHelperApp com.growl.SmokeNotificationView | awk '$3 ~ /Alpha/' | sed -E 's/[^0-9.]*//g'")

set duration to (do shell script "defaults read com.Growl.GrowlHelperApp com.growl.SmokeNotificationView | awk '$3 ~ /Duration/' | sed -E 's/[^0-9.]*//g'")

set cmd to "defaults write com.Growl.GrowlHelperApp \"com.growl.SmokeNotificationView\" -dict "
set params to "\"Smoke - Alpha\" -float " & alpha & " \"Smoke - Duration\" -float 0.3"

do shell script cmd & params

tell application "GrowlHelperApp"
    register as application ¬
        "Volume Change" all notifications {"Volume Change"} ¬
        default notifications {"Volume Change"} ¬
        icon of application "Script Editor"

    notify with name ¬
        "Volume Change" title "Volume" description str application name ¬
        "Volume Change" identifier "MuteUnmute"
end tell

do shell script "defaults write com.Growl.GrowlHelperApp GrowlDisplayPluginName " & oldStyle

set params to "\"Smoke - Alpha\" -float " & alpha & " \"Smoke - Duration\" -float " & duration
do shell script cmd & params
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!