MacOS下连接特定WiFi时如何自动切换Slack状态?
基于MacOS Ventura的WiFi触发Slack状态自动切换方案
一、旧脚本失效的常见原因及调试步骤
Slack API权限/Token变化:旧教程的Token可能已过期或权限不足。先单独测试Slack API调用,确认能正常工作:
curl -X POST "https://slack.com/api/users.profile.set" \ -H "Authorization: Bearer YOUR_SLACK_USER_TOKEN" \ -H "Content-Type: application/json" \ -d '{"profile": {"status_text": "测试状态", "status_emoji": ":test:", "status_expiration": 0}}'检查返回的
ok字段是否为true,若为false,根据error字段排查(如token无效、权限不足)。WiFi检测命令路径变更:Ventura中
airport工具路径已调整,建议用更稳定的networksetup命令替代:networksetup -getairportnetwork en0 # en0为WiFi接口,可通过ifconfig确认launchd配置问题:
- 确保plist文件放在
~/Library/LaunchAgents/(用户级),权限设为644:chmod 644 ~/Library/LaunchAgents/com.yourname.slackstatus.plist - 加载配置:
launchctl load ~/Library/LaunchAgents/com.yourname.slackstatus.plist - 查看加载状态:
launchctl list | grep slackstatus - 查看日志定位错误:
log show --predicate 'subsystem == "com.apple.launchd" AND label == "com.yourname.slackstatus"' --info --last 1h
- 确保plist文件放在
二、适配Ventura的完整脚本+launchd配置
1. Bash脚本(slack_status.sh)
#!/bin/bash # 配置参数 SLACK_TOKEN="xoxp-你的用户Token" WORK_SSID="你的办公WiFi名称" WORK_STATUS="在办公室" WORK_EMOJI=":office:" AWAY_STATUS="不在办公室" AWAY_EMOJI=":house:" # 获取当前WiFi SSID CURRENT_SSID=$(networksetup -getairportnetwork en0 | awk -F': ' '{print $2}') # Slack状态设置函数 set_status() { local text=$1 local emoji=$2 curl -s -X POST "https://slack.com/api/users.profile.set" \ -H "Authorization: Bearer $SLACK_TOKEN" \ -H "Content-Type: application/json" \ -d "{ \"profile\": { \"status_text\": \"$text\", \"status_emoji\": \"$emoji\", \"status_expiration\": 0 } }" } # 逻辑判断 if [ "$CURRENT_SSID" = "$WORK_SSID" ]; then set_status "$WORK_STATUS" "$WORK_EMOJI" else set_status "$AWAY_STATUS" "$AWAY_EMOJI" fi
给脚本加执行权限:chmod +x /path/to/slack_status.sh
2. launchd配置文件(com.yourname.slackstatus.plist)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.yourname.slackstatus</string> <key>ProgramArguments</key> <array> <string>/bin/bash</string> <string>/path/to/slack_status.sh</string> </array> <key>WatchPaths</key> <array> <string>/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist</string> </array> <key>RunAtLoad</key> <true/> <key>StandardErrorPath</key> <string>/tmp/slackstatus.err</string> <key>StandardOutPath</key> <string>/tmp/slackstatus.out</string> </dict> </plist>
加载配置后,切换WiFi即可触发脚本执行。
三、更简便的无脚本工具方案
1. 系统自带Shortcuts(捷径)
- 打开「快捷指令」APP,创建「自动化」,选择「个人自动化」
- 触发条件选「WiFi」,设置「连接到指定网络」或「断开指定网络」
- 添加「运行Shell脚本」动作,填入上述curl命令(替换Token和参数)
- 关闭「运行前询问」,保存后即可自动触发
2. Hammerspoon
用Lua脚本实时监听WiFi变化,比launchd响应更及时:
wifiWatcher = hs.wifi.watcher.new(function() local currentSSID = hs.wifi.currentNetwork() local slackToken = "xoxp-你的用户Token" local setStatus = function(text, emoji) hs.http.post( "https://slack.com/api/users.profile.set", hs.json.encode({ profile = { status_text = text, status_emoji = emoji, status_expiration = 0 } }), { Authorization = "Bearer " .. slackToken }, nil ) end if currentSSID == "你的办公WiFi名称" then setStatus("在办公室", ":office:") else setStatus("不在办公室", ":house:") end end) wifiWatcher:start()
将代码保存为~/.hammerspoon/init.lua,重启Hammerspoon即可生效。
内容的提问来源于stack exchange,提问作者fusio
相关产品推荐
相关产品推荐

