V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
z1gui
0.12D
0.02D
V2EX  ›  编程

请教 Hammerspoon 在获取 WIFI ssid 为 nil 问题

  •  
  •   z1gui · Dec 10, 2025 · 1665 views
    This topic created in 146 days ago, the information mentioned may be changed or developed.

    最近痴迷用 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 版)已启动")  
    
    z1gui
        1
    z1gui  
    OP
       Dec 11, 2025
    结贴: https://github.com/Hammerspoon/hammerspoon/issues/3537#issuecomment-1743870568
    如果类似情况,可以看一下上面这个链接可以解决
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2519 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 05:22 · PVG 13:22 · LAX 22:22 · JFK 01:22
    ♥ Do have faith in what you're doing.