|
发表于 2025-7-17 13:43:02
|
显示全部楼层
在Mac上接收微信消息需要更复杂的方案,以下是两种可行方法(需结合使用):
### 方案一:PyAutoGUI + OCR 监控方案
- import pyautogui
- import pytesseract
- from PIL import Image
- import time
- # 设置监控区域 (左上x, 左上y, 宽度, 高度)
- MONITOR_AREA = (300, 200, 400, 300) # 需调整为聊天窗口位置
- def check_new_message():
- # 截取监控区域
- screenshot = pyautogui.screenshot(region=MONITOR_AREA)
-
- # 使用OCR识别文字
- text = pytesseract.image_to_string(screenshot, lang='chi_sim+eng')
-
- # 检查关键字 (示例)
- if "紧急" in text:
- print("发现紧急消息!")
- return True
- return False
- # 安装依赖:
- # pip3 install pytesseract pillow
- # brew install tesseract
复制代码
### 方案二:系统通知监听方案(更可靠)
- import os
- import re
- from datetime import datetime
- def parse_notifications():
- """解析系统通知日志"""
- cmd = 'log show --predicate \'subsystem == "com.apple.notificationcenter"\' --last 1m'
- logs = os.popen(cmd).read()
-
- wechat_msgs = []
- for line in logs.splitlines():
- if 'WeChat' in line and 'body' in line:
- # 提取消息内容
- match = re.search(r'body\" = \"(.+?)\"', line)
- if match:
- msg = match.group(1)
- wechat_msgs.append(msg)
-
- return wechat_msgs
- # 定时执行检查
- while True:
- new_msgs = parse_notifications()
- if new_msgs:
- print(f"{datetime.now()} 收到新消息:")
- for msg in new_msgs:
- print(f" - {msg}")
- time.sleep(10) # 每10秒检查一次
复制代码
### 必要设置步骤:
1. 开启系统权限:
系统设置 → 隐私与安全性 → 辅助功能 → 解锁 → 勾选终端
2. 安装OCR依赖:
- # 安装Homebrew(如未安装)
- /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
-
- # 安装Tesseract
- brew install tesseract
- brew install tesseract-lang # 中文包
-
- # Python库
- pip3 install pytesseract pillow
-
复制代码
### 注意事项:
1. OCR方案:
- 需要手动确定聊天窗口位置
- 识别准确率受字体/背景影响
- 使用前需校准区域坐标
2. 通知方案:
- 只能获取显示通知的消息
- 需开启微信的桌面通知(微信设置 → 新消息通知)
- 首次运行需在系统弹窗中允许终端访问通知
> 两种方案都只能获取新消息到达提醒,无法获取完整聊天记录。企业微信用户建议使用官方API:https://developer.work.weixin.qq.com
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|