Skip to content
On this page

写个小工具转发系统的解锁屏幕事件,来尝试在解锁电脑的时候自动启动 Jitouch。

TL;DR

  1. 打开 Xcode 新建 Command Line Tool 项目,源码,修改脚本路径,编译出 Shell Notification Center。
  2. 启动 Shell Notification Center,或直接丢进启动项。
  3. 编写脚本 ~/.shnc/main.sh
sh
if [[ "$1" == "com.apple.screenIsUnlocked" ]]; then
  source ./jitouch.sh
  exit 0
fi
  1. 编写脚本 ~/.shnc/jitouch.sh
sh
lines=`ps -xw | grep Jitouch.app`

if [[ "$lines" == *"/Library/PreferencePanes/Jitouch.prefPane"* ]]; then
  exit 0
fi

nohup /Library/PreferencePanes/Jitouch.prefPane/Contents/Resources/Jitouch.app/Contents/MacOS/Jitouch > /dev/null 2>&1 &
  1. 完事,测试。

起因

macOS 更新到 big sur 之后就各种小问题,时不时的发现唤醒电脑之后鼠标手势失效,打开系统设置启动 Jitouch 插件就恢复了。 这样重复的次数多了也挺烦人的。想等它更新,但打开官网发现可能停止维护了。

仔细想想,只要写个脚本在解锁电脑的时候尝试启动 Jitouch 就可以了。

命令行启动 Jitouch

想想启动 Jitouch 的方式是打开系统设置然后点击 Jitouch 插件启动的,有点神奇...
然后在 reddit 上的帖子 看到有人在寻找 Jitouch 的替代品,虽然没有找到替代品,但找到了命令行启动方式。 可执行文件路径为 /Library/PreferencePanes/Jitouch.prefPane/Contents/Resources/Jitouch.app/Contents/MacOS/Jitouch

监听解锁事件

自动启动相关的功能 macOS 直接提供给普通用户的,只有登录用户时的启动项,而日常的解锁是不会触发的。 最开始的希望寄托与自带的 Automator 和 Apple Script,可惜没有任何事件监听功能。

搜了半天没发现可以直接监听系统事件的方法,倒是找到了 sleepwatch 和 EventScript 等工具,但不太满意... 后来找到了 DistributedNotificationCenter 相关的 API,提供了监听 com.apple.screenIsUnlocked 等事件的功能。 索性写了个命令行工具转发系统事件...

至于 com.apple.screenIsUnlocked 从何而来,则不得而知,网上搜到的都是没有贴出来源。苹果的开发者文档也找到相关的信息。 所以,也没办法找出更多的事件来提供更丰富的功能。后来倒是在 Console.app 里过滤找到了几个已知的。

com.apple.screenIsLocked
com.apple.screenIsUnlocked
com.apple.screensaver.didstart
com.apple.screensaver.willstop
com.apple.screensaver.didstop