【LUA】mac状态栏添加天气

基于网络上的版本修改的,找不到出处了。第一个摸索的lua脚本,调了很久。

主要修改:如果风速不大,就默认不显示,以及调整为了一些格式

lua 复制代码
local urlApi = 'http://..'
--这个urlApi去申请个免费的就可以了 然后打开对应的json数据修改

local menubar = hs.menubar.new()
local menuData = {}

local weaEmoji = {
   lei = '🌩️',
   qing = '☀️',
   shachen = '😷',
   wu = '🌫',
   xue = '❄️',
   yu = '🌧',
   yujiaxue = '🌨',
   yun = '☁️',
   zhenyu = '🌧',
   yin = '⛅️',
   default = ''
}

function updateMenubar()
	 menubar:setTooltip("Weather Info")
    menubar:setMenu(menuData)
end

function getWeather()
   hs.http.doAsyncRequest(urlApi, "GET", nil,nil, function(code, body, htable)
      if code ~= 200 then
         print('get weather error:'..code)
         return
      end
      rawjson = hs.json.decode(body)
      city = rawjson.city
      menuData = {}
      for k, v in pairs(rawjson.data) do
         if k == 1 then
            menubar:setTitle(weaEmoji[v.wea_img])
                     
            if v.win_speed == "<3级" then
                titlestr = string.format("%s  %s %s  🌡️%s-%s°C", city,weaEmoji[v.wea_img], v.wea, v.tem_night, v.tem_day)
            else
                titlestr = string.format("%s  %s 🌡️%s-%s°C  💨%s %s %s", city,weaEmoji[v.wea_img],v.tem_night, v.tem_day, v.win_speed,  v.win, v.wea)
            end

            
            item = { title = titlestr }
            table.insert(menuData, item)
            table.insert(menuData, {title = '-'})
            

         else
            
            if v.win_speed == "<3级" then
                titlestr = string.format("%s  %s 🌡️%s-%s°C     %s",  v.date, weaEmoji[v.wea_img], v.tem_night, v.tem_day, v.wea)
            else
                titlestr = string.format("%s  %s 🌡️%s-%s°C  💨%s %s %s",  v.date, weaEmoji[v.wea_img],v.tem_night, v.tem_day, v.win_speed, v.win, v.wea)
            end
         
            item = { title = titlestr }
            table.insert(menuData, item)
         end
      end
      updateMenubar()
   end)
end

menubar:setTitle('👀')
getWeather()
updateMenubar()
hs.timer.doEvery(3000, getWeather)
相关推荐
CZIDC6 小时前
MacOS字体看起来比在 Windows 上更好?
macos
利来利往8 小时前
【ai写代码】lua-判断表是否被修改
lua
陈天cjq8 小时前
Redis 实用型限流与延时队列:从 Lua 固定/滑动窗口到 Streams 消费组(含脚本与压测)
redis·junit·lua
Warren988 小时前
Lua 脚本在 Redis 中的应用
java·前端·网络·vue.js·redis·junit·lua
Cosmoshhhyyy14 小时前
linux远程部署dify和mac本地部署dify
linux·运维·macos
行星00814 小时前
mac 通过homebrew 安装和使用nvm
macos·npm·node.js
2501_9280946521 小时前
Mac电脑录屏工具 Omi录屏专家(Mac中文)
macos·mac·录屏工具·omi
森之鸟1 天前
Mac chrome浏览器下载DevEco Studio 6.0.0 Beta2失败
前端·chrome·macos
FairGuard手游加固1 天前
版本更新!FairGuard-Mac加固工具已上线!
macos