使用 BetterTouchTool 在触摸栏中显示 Magic Mouse 电池

问题描述 投票:0回答:3

我正在使用这个 BetterTouchToll 来让我的触摸栏变得更有趣,这非常酷。

他接受了一些Apple脚本以获得更多的动态,所以我开始研究这个脚本。

现在我想在触摸栏上显示我的魔术鼠标电池,为此我正在尝试此代码,但不起作用。

if application "Mouse" is running then
    tell application "Mouse"
        return (get Battery)
    end tell
end if
return "no mouse"

我的猜测是Mouse不是一个应用程序,但不知道该放什么地方

applescript
3个回答
4
投票

获取电池电量的传统方法是在命令行上使用

ioreg
。然而,至少从 macOS High Sierra/10.13.4 开始,传统的方法似乎不再有效;也就是说,它们不再允许选择仅显示单个蓝牙设备的电池百分比。

所以这是一个假设 Magic Mouse 始终是 ioreg 显示的最后一个设备的 hack。如果不是跨不同的 macOS 安装,那么跨不同的版本,这很可能会失败。

ioreg -c AppleDeviceManagementHIDEventService | grep BatteryPercent | tail -1 | sed 's/[^[:digit:]]//g'

在 AppleScript 中,这将是:

do shell script "ioreg -c AppleDeviceManagementHIDEventService | grep BatteryPercent | tail -1 | sed 's/[^[:digit:]]//g'"

您的代码设置还可以检测 Magic Mouse 未连接的情况。产品名称位于 ioreg 的“Product”属性中。例如:

ioreg -c AppleDeviceManagementHIDEventService | grep '"Product" ='

因此,要确保最终设备是鼠标,您可以这样做:

set finalDevice to do shell script "ioreg -c AppleDeviceManagementHIDEventService | grep '\"Product\" =' | tail -1"
if finalDevice contains "Magic Mouse" then
    set remaining to do shell script "ioreg -c AppleDeviceManagementHIDEventService | grep BatteryPercent | tail -1 | sed 's/[^[:digit:]]//g'"
    remaining & "%"
else
    "no mouse"
end if

基本逻辑:

  1. 使用
    ioreg
    获取所有产品的列表。
  2. 使用
    tail
    仅获取列表中的最终产品。
  3. 如果最终产品是 Magic Mouse,那么:
    1. 使用
      ioreg
      获取所有电池百分比的列表。
    2. 使用
      tail
      仅获取列表中的最终电池百分比。
    3. 使用
      sed
      仅获取该行的实际数字。
    4. 在数字后附加一个百分比符号。
  4. 否则没有鼠标。 (或者鼠标不是列表中的最后一项。)

有关使用

ioreg
的旧方法,请参阅:


0
投票

非常感谢您的解决方案。非常鼓舞人心。由于我有多个设备,我根据您的解决方案制作了自己的脚本。我想在这里分享它,以防对其他人有用。

由于我有多个蓝牙设备,因此它们在 ioreg 中的顺序基于它们连接的顺序。这意味着我不能假设鼠标是最后一个设备。

我大部分都是在 shell 中完成的,而不是在 applescript 中,因为我在 shell 方面更有经验,因此这样更快。使用 Applescript 过滤 ioreg 的输出本来应该是一个“更干净”的解决方案:p

警告:我知道这段代码非常糟糕,但它写起来很快并且完成了工作,不要认为这是一种正确做事的方法。

我的解决方案

以下脚本从 BTT 调用 shell 脚本

set devicename to "Magic Mouse 2" -- The name of the device in ioreg
set displayname to "Mouse" -- The name to display on the touchbar

set remaining to do shell script "~/.dotfiles/shell/device_battery_level.sh" & " " & quoted form of devicename

if remaining is "" then

    "" --No device present = no output to touchbar

else

    displayname & " " & remaining & "%" -- Show output on touchbar

end if

正如所见,代码几乎只是调用 shell 脚本。变量“devicename”中提供的名称用作脚本的参数,并且是脚本将在 ioreg 中查找的名称。如果 shell 脚本输出空脚本,则不会显示任何小部件。对我来说,这比显示“无设备”更受欢迎。

“~/.dotfiles/shell/device_battery_level.sh”中的脚本如下所示:

#!/bin/sh
DEVICES=$(ioreg -r -l -n AppleHSBluetoothDevice | egrep '"BatteryPercent" = |^  \|   "Bluetooth Product Name" = ') #Lets get a list of all bluetooth devices

DEVICELINE=$(grep -n "$1" <<< "$DEVICES") #$1 is the device that this script was called with. Lets extract only the line with that device
if [$DEVICELINE = ""] #If DEVICELINE is empty the name of the device was not in the output and it is properly not connected.
then

    echo "" #Device not present, lets give BTT an empty string

else

    LINENR="${DEVICELINE:0:1}" #Then we find out where the line of the device is located

    NEXTLINE=$(expr $LINENR + "1") #The battery level is at the next line therefore we increment the line number
    SEDCOMMAND="p"

    BATTERYLINE=$(echo "$DEVICES" | sed -n  $NEXTLINE$SEDCOMMAND) # Now we can extract the line with the battery percent

    echo $BATTERYLINE | sed 's/[^[:digit:]]//g' #Finally we just need to get the digit and echo that to BTT

fi

基本逻辑与上面的答案相同。除了使用 tailegrep 代替从 ioreg 抓取最后一行之外,egrep 用于仅输出相关行。 egrep 代码基于此处的另一篇文章:https://apple.stackexchange.com/questions/293502/how-can-i-define-the-battery-level-of-my-magic-mouse-from-the -命令行/293505#293505 基于此,找到提到设备名称的行。逻辑是,由于电池电量信息始终低于 ioreg 中的设备名称,因此提取列表中的下一行必须是电池电量。


0
投票

扩展@jerry-stratton的回复,对我来说,以下方法有效(这是一个旧的蓝牙苹果鼠标)

ioreg -r -l -n BNBMouseDevice| grep BatteryPercent | tail -1 | sed 's/[^[:digit:]]//g'
© www.soinside.com 2019 - 2024. All rights reserved.