使用更好的触摸工具在触摸栏中显示魔术鼠标电池



我正在使用这个BetterTouchToll让我的触摸栏更有趣,这很酷。

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

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

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

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

获取电池电量的传统方法是在命令行上使用ioreg。但是,至少在macOS High Sierra/10.13.4中,这样做的传统方法似乎不再有效;也就是说,它们不再允许选择仅显示单个蓝牙设备的电池百分比。

所以这是一个黑客,假设魔术鼠标始终是ioreg显示的最后一个设备。如果不是在 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'"

您具有代码设置,以检测妙控鼠标何时未连接。产品名称位于 ioreg 中的属性"产品"中。例如:

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. 如果最终产品是魔术鼠标,则:
    1. 使用ioreg获取所有电池百分比的列表。
    2. 使用tail仅获取列表中的最终电池百分比。
    3. 使用sed仅从该行获取实际数字。
    4. 将百分比符号附加到数字。
  4. 否则,没有鼠标。(或者鼠标不是列表中的最后一项。

有关使用ioreg的旧方法,请参阅,例如:

  • 报告蓝牙鼠标/键盘电池状态
  • 读取妙控鼠标和苹果无线键盘电池百分比

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

由于我有多个蓝牙设备,因此它们在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

基本逻辑与上述答案相同。除了不是用尾巴从 ioreg 抓取最后一行之外,egrep 仅用于输出相关行。egrep代码基于这里的另一个帖子:https://apple.stackexchange.com/questions/293502/how-can-i-determine-the-battery-level-of-my-magic-mouse-from-the-command-line/293505#293505 基于此,找到提及设备名称的行。逻辑是,由于电池电量信息始终位于ioreg中的设备名称下方,因此提取列表中的下一行必须是电池电量。

最新更新