V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
z1gui
0.12D
0.02D
V2EX  ›  编程

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

  •  
  •   z1gui · 10 小时 12 分钟前 · 97 次点击

    最近痴迷用 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 版)已启动")  
    
    目前尚无回复
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   3077 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 11:48 · PVG 19:48 · LAX 03:48 · JFK 06:48
    ♥ Do have faith in what you're doing.