使用 BetterTouchTool 在触控栏中显示 Magic Mouse 电池 [英] Display Magic Mouse Battery in Touch bar using BetterTouchTool

查看:35
本文介绍了使用 BetterTouchTool 在触控栏中显示 Magic Mouse 电池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using this BetterTouchToll for make my touch bar more interesting, what is very cool.

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

He accept some Apple Scripts for more dynamic, so I start to study this scripts.

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

Now I wanna to display my Magic Mouse Battery on my touch bar, for this I was trying this code, but is not working.

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

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

My guess is that Mouse is not a application, but don't know what to put in the place

推荐答案

获取电池电量的传统方法是在命令行上使用 ioreg.但是,至少从 macOS High Sierra/10.13.4 开始,执行此操作的传统方法似乎不再有效;也就是说,他们不再允许选择只显示单个蓝牙设备的电池百分比.

The traditional means of getting the battery level is to use ioreg on the command line. However, the traditional means of doing this no longer seem to work as of at least macOS High Sierra/10.13.4; that is, they no longer allow choosing to display just the battery percentage of a single bluetooth device.

所以这是一个假设魔术鼠标始终是 ioreg 显示的最后一个设备的黑客.这很可能会失败,如果不是跨 macOS 的不同安装,那么跨不同版本.

So this is a hack that assumes that the Magic Mouse is always the last device displayed by ioreg. This is likely to fail, if not across different installations of macOS, then across different versions.

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

在 AppleScript 中,这将是:

In an AppleScript, this would be:

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

您的代码设置还可以检测何时未连接 Magic Mouse.产品名称在 ioreg 的产品"属性中.例如:

You have your code setup to also detect when the Magic Mouse is not connected. The product name is in the property "Product" in ioreg. For example:

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

因此,要确保最终设备是鼠标,您可以执行以下操作:

So to make sure that this final device is the Mouse, you could do:

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. Grab the list of all products using ioreg.
  2. Use tail to get only the final product in the list.
  3. If the final product is a Magic Mouse, then:
  1. 使用 ioreg 获取所有电池百分比的列表.
  2. 使用 tail 仅获取列表中的最终电池百分比.
  3. 使用 sed 仅从该行获取实际数字.
  4. 在数字后附加一个百分比符号.
  1. Grab the list of all battery percentages using ioreg.
  2. Use tail to get only the final battery percentage in the list.
  3. Use sed to get only the actual number from that line.
  4. Append a percentage symbol to the number.

  • 否则,就没有鼠标.(或者鼠标不是列表中的最后一项.)
  • 有关使用 ioreg 的旧方法,请参见,例如:

    For the older means of using ioreg, see, for example:

    这篇关于使用 BetterTouchTool 在触控栏中显示 Magic Mouse 电池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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