在 Applescript 中实现键盘音量控制按钮 - 在循环内设置音量不起作用 [英] Implementing keyboard volume control buttons in Applescript - setting volume inside loop doesn't work

查看:22
本文介绍了在 Applescript 中实现键盘音量控制按钮 - 在循环内设置音量不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我有一个 MacAlly IceKey 键盘.此键盘具有需要驱动程序才能运行的音量按钮.这个驱动程序自 2006 年以来就没有更新过,我怀疑它是我最近在 Mac OS X 10.6.1 下遇到的反复出现的内核恐慌的根源.所以,它出去了;但我想要我的音量键!

I have a MacAlly IceKey keyboard. This keyboard has volume buttons that require a driver to function. This driver has not been updated since 2006, and I suspect it as the source of a recent spat of recurring kernel panics I've been experiencing under Mac OS X 10.6.1. So, out it goes; but I want my volume keys back!

使用出色的 ControllerMate,我可以对这些键进行编程以执行任何操作,包括运行 Applescript 脚本.所以,我正在尝试实现该功能.

Using the wonderful ControllerMate, I can program these keys to do anything, including run an applescript script. So, I'm trying to implement that functionality.

设置音量命令(Standard Additions 的一部分)允许您将音量设置为 0 到 100 之间的任何值.Apple 键盘音量键允许选择总共 17 个音量设置(包括 0).我认为复制这种行为的最简单方法是保留一个允许的音量设置列表,并从中获取下一个最大(或最小)的设置.

The set volume command (part of Standard Additions) allows you to set the volume to any value between 0 and 100. The Apple keyboard volume keys allow a total of 17 volume settings to be selected (including 0). I figure the simplest way to duplicate this behavior is to keep a list of allowed volume settings and grab the next largest (or smallest) one out of that.

问题

它不起作用.以下脚本:

It doesn't work. The following script:

set volumesList to {0, 6, 12, 18, 25, 31, 37, 43, 50, 56, 62, 68, 75, 81, 87, 93, 100}
set sysVolume to get volume settings

repeat with curVolume in volumesList
    if (curVolume > (output volume of sysVolume)) then
        set volume output volume (contents of curVolume)
        exit repeat
    end if
end repeat

get volume settings

...仅在系统音量小于 43 时才有效.系统似乎将50"解释为49";这与我的脚本的音量一样高.如果音量开始高于 50,则我的脚本无效.踢球者?如果删除退出重复"语句,系统音量将设置为 100 - 正如您所期望的那样.

...only works if the system volume level happens to be less than 43. The system seems to interpret "50" as "49"; that's as high as the volume will go with my script. If the volume starts higher than 50, my script has no effect. The kicker? If the "exit repeat" statement is removed, the system volume gets set to 100 - just as you'd expect.

(真遗憾,AppleScript 有时很奇怪.)

(Good grief, AppleScript is weird sometimes.)

有什么想法吗?

奖励积分

让它也能显示音量叠加会非常棒.有谁知道如何做到这一点?它甚至不需要通过 AppleScript;如果需要的话,我很高兴在命令行工具中粘贴一些 Cocoa 代码.

It would be super awesome to get this to display the volume overlay as well. Does anyone know how to accomplish that? It doesn't even need to be through AppleScript; I'm happy sticking some Cocoa code in a command-line tool if that's what it takes.

推荐答案

我不知道如何让半透明覆盖显示出来,但这至少在调高音量的同时播放系统提示音:

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 似乎是在 (0, 100) 限制之外自动调整值.

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

更新:您可以使用 Growl 的 AppleScript 支持来显示某种覆盖:

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

这篇关于在 Applescript 中实现键盘音量控制按钮 - 在循环内设置音量不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆