请教 Hammerspoon 在获取 WIFI ssid 为 nil 问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
z1gui
0.12D
0.02D
V2EX    编程

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

  •  
  •   z1gui 6 天前 390 次点击

    最近痴迷用 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
       5 天前
    结贴: https://github.om/Hammerspoon/hammerspoon/issues/3537#issuecomment-1743870568
    如果类似情况,可以看一下上面这个链接可以解决
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     973 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 30ms UTC 22:40 PVG 06:40 LAX 14:40 JFK 17:40
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86