使用BetterTouchTool在触摸栏中显示魔术鼠标电池 [英] Display Magic Mouse Battery in Touch bar using BetterTouchTool

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

问题描述

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

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

他接受一些Apple脚本以提高动态性,所以我开始研究这些脚本.

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"

我的猜测是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'"

您已设置代码,还可以检测未连接魔术鼠标的时间.产品名称位于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. 如果最终产品是魔术鼠标,则:
  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在触摸栏中显示魔术鼠标电池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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