惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
T
Tailwind CSS Blog
博客园_首页
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News
Hugging Face - Blog
Hugging Face - Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
V2EX
J
Java Code Geeks
量子位
D
DataBreaches.Net
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
C
Check Point Blog
V
Visual Studio Blog
H
Help Net Security
Recent Announcements
Recent Announcements
Engineering at Meta
Engineering at Meta

编程

开源人的心声,关于为什么花精力开源 kkRepo ,是不是很多人不理解 - V2EX 把入口路由注释掉了,然后让 AI 一直在修改路过的代码... - V2EX clion 禁用检查的问题 - V2EX 软件工程真的一直在进步吗? 我开始在代码中使用中文变量名了 软件工程里,最昂贵的成本到底是什么? [转载]信创全平台 C++17 交叉编译工具链 AI 正在淘汰小众编程语言 在 Codex 中可以直接调用 Claude 吗? 氛围编码生成的是代码,工程生成的是系统。氛围编程不是工程,大家怎么看? Tikrok8 新版本更新 被 composer 2.5 震撼了 在 AI 时代,现在的新项目大家怎么开始呢? [纯分享]怎么同时学会多个编程语言 现在 rust 是不是好起来了? 用 just 管理终端命令,使“命令即代码” Qoder teams 版本设计反人性 AI 写代码真香喷;以后只要产品经理就可以了 warp 有没有什么代码补全和格式化的方案,想代替 cursor 被 KIMI 玩弄的一天 这么 lj 我是没想到的 pyruns:把本地 Python 实验和 shell 任务真正整理起来的 Web UI AI 写的项目要怎么维护?都是一次性代码吗? claude-opus-4-6 大家实际使用感觉怎么样? 关于加强学习效率的讨论 想用下智谱的 GLM 和 Minmax 的编程模型,有没有用过的分享下使用效果 设计后台微服务还是 mq 发一个更新版的 rules Claude Opus 4.5 发布了,前端工程师的天,塌了又塌(AI 计量单位) codex 的一些小感受 agent 开发据说很有前途
请教 Hammerspoon 在获取 WIFI ssid 为 nil 问题
z1gui · 2025-12-10 · via 编程

最近痴迷用 Hammespoon 去简化自己的某些操作。用 chatgpt 写了个根据 Wifi ssid 切换网络位置的 lua 脚本。看代码是没啥问题的,但是在获取 Wifi ssid 时候为空,我以为是在切换过程中,可能先清空,然后再连接,但是用了 10s 轮询也不行。看了 chatgpt 回答的可能原因有权限问题,也可能是中国地区禁用 hs.wifi.currentNetwork() 获取 ssid 。请问有人遇到这个问题么,该如何解决

- WiFi SSID 与 macOS 网络位置的映射
local locationMap = {
    ["xxxxx"] = "office",
    ["xxxxx"]    = "home",
}

local defaultLocation = "Automatic"

---------------------------------------------------------------------
-- Debug 输出函数
---------------------------------------------------------------------
local function debugLog(msg)
    print("[WiFi-Location-Debug] " .. msg)
end

---------------------------------------------------------------------
-- 获取当前 macOS 网络位置
---------------------------------------------------------------------
local function getCurrentLocation()
    local output = hs.execute("scselect")
    debugLog("scselect 输出: \n" .. output)

    -- 只匹配带 * 的那一行,并从括号中取出名称
    local current = output:match("%*.-%((.-)%)")
    if current then
        debugLog("解析到当前位置: " .. current)
    else
        debugLog("未能从 scselect 中解析出当前位置")
    end
    return current or "Unknown"
end

---------------------------------------------------------------------
-- 切换网络位置
---------------------------------------------------------------------
local function switchLocation(newLocation)
    debugLog("准备切换网络位置到: " .. newLocation)
    local result = hs.execute('scselect "' .. newLocation .. '"')
    debugLog("执行 scselect 后返回: \n" .. result)
end

---------------------------------------------------------------------
-- WiFi 变化事件回调
---------------------------------------------------------------------
local function ssidChanged()
    local ssid = hs.wifi.currentNetwork()
    debugLog("检测到 WiFi 变化,当前 SSID: " .. tostring(ssid))

    local targetLocation

    if ssid and locationMap[ssid] then
        targetLocation = locationMap[ssid]
        debugLog("SSID 匹配映射表,将切换到位置: " .. targetLocation)
    else
        targetLocation = defaultLocation
        debugLog("SSID 未匹配映射表,切换到默认位置: " .. targetLocation)
    end

    -- 执行切换
    switchLocation(targetLocation)

    -- 延时读取切换后的实际位置
    hs.timer.doAfter(0.3, function()
        local loc = getCurrentLocation()
        hs.alert.show("当前网络位置:" .. loc)
        debugLog("最终确认当前网络位置:" .. loc)
    end)
end

---------------------------------------------------------------------
-- 启动 WiFi watcher
---------------------------------------------------------------------
wifiWatcher = hs.wifi.watcher.new(ssidChanged)
wifiWatcher:start()

debugLog("WiFi watcher 已启动")
hs.alert.show("WiFi 自动切换网络位置( Debug 版)已启动")