emacs 同步微信消息数量
2024-10-05
2 min read
使用 emacs 由于太专注了,往往容易漏掉微信的消息。 如果能在 emacs 中显示微信的消息数量那就太棒了。
获取消息数量
macos 中获取微信消息数量使用 applescript 获取 dock 栏的通知数量。 需要开启通知权限
tell application "System Events"
try
-- 获取 Dock 中所有 UI 元素
set dockElements to (every UI element of list 1 of application process "Dock")
-- 查找微信图标
repeat with element in dockElements
if name of element is "微信" then
-- 获取角标属性
set badgeNumber to value of attribute "AXStatusLabel" of element
set resultText to badgeNumber
exit repeat
else
set resultText to "WeChat not found in Dock"
end if
end repeat
on error errMsg
set resultText to "0"
end try
end tell
set filePath to "/Users/van/.message"
if resultText is not missing value then
do shell script "echo " & quoted form of resultText & " > " & quoted form of filePath
else
do shell script "echo > " & quoted form of filePath
end if
设置定时任务
通过定时任务获取微信的消息数量。将文件放至 ~/Library/LaunchAgents 可以开启启动
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<!--put this file in ~/Library/LaunchAgents -->
<dict>
<key>Label</key>
<string>com.wechat.message.script</string>
<!-- 设置为每隔 10 秒执行一次 -->
<key>StartInterval</key>
<integer>10</integer>
<!-- 指定要执行的 AppleScript 文件 -->
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>/Users/van/.doom.d/modules/neo-emacs/modelinexp/getWechatMessageCount.scpt</string>
</array>
<!-- <key>StandardErrorPath</key> -->
<!-- <string>/Users/van/.doom.d/modules/neo-emacs/modelinexp/error.log</string> <!-\- 错误输出日志 -\-> -->
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
</dict>
</plist>
开启定时任务
launchctl load ~/.doom.d/modules/neo-emacs/modelinexp/com.wechat.message.script.plist
launchctl start ~/.doom.d/modules/neo-emacs/modelinexp/com.wechat.message.script.plist
launchctl list com.wechat.message.script
查看任务的状态
{
"LimitLoadToSessionType" = "Aqua";
"StandardErrorPath" = "/Users/van/.doom.d/modules/neo-emacs/modelinexp/error.log";
"Label" = "com.wechat.message.script";
"OnDemand" = true;
"LastExitStatus" = 0;
"Program" = "/usr/bin/osascript";
"ProgramArguments" = (
"/usr/bin/osascript";
"/Users/van/.doom.d/modules/neo-emacs/modelinexp/getWechatMessageCount.scpt";
);
};
增加 segment
- 从文件中获取微信消息的数量
- 增加 modeline 的 segment
- 将此 segment 添加至 modeline 中
(defun get-message-count ()
"Read the content of a specific file and return it as a string."
(let ((file-path "~/.message"))
(if (file-exists-p file-path)
(with-temp-buffer
(insert-file-contents file-path)
(string-trim (buffer-string)))
"File not found")))
(doom-modeline-def-segment wechat-msg-count
"A custom segment that reads content from a local file."
(concat "" (get-message-count)))
(doom-modeline-def-modeline 'main
'(modals matches buffer-info buffer-position parrot selection-info)
'(misc-info minor-modes wechat-msg-count input-method buffer-encoding my-major-mode process vcs time my-segment))