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

编程入门 行业动态 更新时间:2024-10-10 21:27:59
本文介绍了使用 BetterTouchTool 在触控栏中显示 Magic Mouse 电池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在使用这个 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

基本逻辑:

使用 ioreg 获取所有产品的列表.使用 tail 仅获取列表中的最终产品.如果最终产品是 Magic Mouse,则: Grab the list of all products using ioreg. Use tail to get only the final product in the list. If the final product is a Magic Mouse, then: 使用 ioreg 获取所有电池百分比的列表.使用 tail 仅获取列表中的最终电池百分比.使用 sed 仅从该行获取实际数字.在数字后附加一个百分比符号. Grab the list of all battery percentages using ioreg. Use tail to get only the final battery percentage in the list. Use sed to get only the actual number from that line. Append a percentage symbol to the number.

否则,就没有鼠标.(或者鼠标不是列表中的最后一项.)

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

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

报告蓝牙鼠标/键盘电池状态读取 Magic Mouse 和 Apple 无线键盘电池百分比

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

更多推荐

[db:关键词]

本文发布于:2023-04-17 13:50:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/915806.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:栏中   电池   触控   BetterTouchTool   Magic

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!