new version
- improved notification system: watcher for track changes. notifications pops even no key is pressed.
- efficiency: watcher for Media Center status.
token = "TOKEN"
mcIcon = "/Applications/Media Center 24.app/Contents/Resources/AppIcon.icns"
trackKey = 0
mcIsOn = false
mcNotifyLoopSeconds = 5
function systemKey(key)
hs.eventtap.event.newSystemKeyEvent(string.upper(key), true):post()
hs.eventtap.event.newSystemKeyEvent(string.upper(key), false):post()
end
function xmlParse(data,item)
result = data:match("<Item Name=\""..item.."\">(.-)</Item>")
if result == nil then result = "" else string.format(result) end
return result
end
function mcNotify()
hs.timer.doAfter(
1,
function()
hs.http.asyncGet("http://localhost:52199/MCWS/v1/Playback/Info?Token="..token.."", {}, function(code,body,headers)
if (code == 200 and trackKey ~= xmlParse(body, "FileKey")) then
trackKey = xmlParse(body, "FileKey")
notif = hs.notify.new({title=xmlParse(body, "Name"), informativeText=xmlParse(body, "Artist")})
notif:contentImage(hs.image.imageFromURL("http://localhost:52199/"..xmlParse(body, "ImageURL").."&Token="..token))
notif:setIdImage(mcIcon)
notif:send()
end
end)
end
)
end
function mcNotifyLoop()
if mcIsOn == true then mcNotify() end
end
hs.timer.doEvery(mcNotifyLoopSeconds, mcNotifyLoop)
hs.application.watcher.new(function(name, event, app)
if name == "Media Center 24" then
if event == 1 then mcIsOn = true elseif event == 2 then mcIsOn = false end
end
end):start()
-- Keyboard Shortcuts
hs.hotkey.bind({}, "f1", function() systemKey("BRIGHTNESS_DOWN") end, nil, function() systemKey("BRIGHTNESS_DOWN") end)
hs.hotkey.bind({}, "f2", function() systemKey("BRIGHTNESS_UP") end, nil, function() systemKey("BRIGHTNESS_UP") end)
hs.hotkey.bind({}, "f5", function() systemKey("ILLUMINATION_DOWN") end, nil, function() systemKey("ILLUMINATION_DOWN") end)
hs.hotkey.bind({}, "f6", function() systemKey("ILLUMINATION_UP") end, nil, function() systemKey("ILLUMINATION_UP") end)
hs.hotkey.bind({}, "f10", function() systemKey("MUTE") end)
hs.hotkey.bind({}, "f11", function() systemKey("SOUND_DOWN") end, nil, function() systemKey("SOUND_DOWN") end)
hs.hotkey.bind({}, "f12", function() systemKey("SOUND_UP") end, nil, function() systemKey("SOUND_UP") end)
-- Play/Pause
hs.hotkey.bind({}, "f8", function()
if mcIsOn == true then hs.http.get("http://localhost:52199/MCWS/v1/Playback/PlayPause?Token="..token) end
end)
-- Previous
hs.hotkey.bind({}, "f7", function()
if mcIsOn == true then
hs.http.get("http://localhost:52199/MCWS/v1/Playback/Previous?Token="..token)
mcNotify()
end
end)
-- Next
hs.hotkey.bind({}, "f9", function()
if mcIsOn == true then
hs.http.get("http://localhost:52199/MCWS/v1/Playback/Next?Token="..token)
mcNotify()
end
end)