Newer
Older
PlexKeyboardPlus / remote.lua
local kb = libs.keyboard;
local win = libs.win;
local log = require("log");

events.detect = function ()
	if OS_WINDOWS then
		return libs.fs.exists("C:\\Program Files\\Plex\\Plex Media Player");
	elseif OS_OSX then
		return libs.fs.exists("/Applications/Plex Home Theater.app") or libs.fs.exists("/Applications/Plex.app") or libs.fs.exists("/Applications/Pley Media Player.app");
	end
end

--@help Focus Plex application
actions.switch = function()
	if OS_WINDOWS then
		local hwnd = win.window("PlexMediaPlayer.exe");
		if (hwnd == 0) then
            actions.launch();
            return false;
        else
            local active = win.active();
            if (active == hwnd) then return true; end
            win.switchtowait("PlexMediaPlayer.exe");
            return false;
        end
	end
end

--@help Launch Plex application
actions.launch = function()
	if OS_WINDOWS then
        log.info("LAUNCHING");
		pcall(function ()
			os.start("c:\\Program Files\\Plex\\Plex Media Player\\PlexMediaPlayer.exe"); 
		end);
		
	elseif OS_OSX then
		os.script("tell application \"Plex Home Theater\" to activate");
		os.script("tell application \"Plex\" to activate");
		os.script("tell application \"Plex Media Player\" to activate");
	end
    for i = 1,10,1
    do
        local hwnd = win.window("PlexMediaPlayer.exe");
		if (hwnd ~= 0) then break; end   
        os.sleep(1000);
    end
    log.info("LAUNCHED AND SWITCHING");
   	win.switchtowait("PlexMediaPlayer.exe");
end

--@help Navigate back
actions.back = function ()
	if (actions.switch()) then kb.press("esc"); end
end

--@help Play pause
actions.play_pause = function ()
	if (actions.switch()) then kb.press("space"); end
end

--@help Seek forward
actions.forward = function ()
	if (actions.switch()) then kb.press("right"); end
end

--@help Seek rewind
actions.rewind = function ()
	if (actions.switch()) then kb.press("left"); end
end

--@help Navigate home
actions.home = function ()
	if (actions.switch()) then kb.press("h"); end
end

--@help Stop playback
actions.stop = function ()
	if (actions.switch()) then kb.press("x"); end
end

--@help Play current item
actions.play_current = function ()
	if (actions.switch()) then kb.press("space"); end
end

--@help Play select item
actions.select = function ()
	if (actions.switch()) then kb.press("enter"); end
end

--@help Play volumeup item
actions.volumeup = function ()
	if (actions.switch()) then kb.press("oem_plus"); end
end

--@help Play volumeup item
actions.volumedown = function ()
	if (actions.switch()) then kb.press("oem_minus"); end
end

--@help Navigate left
actions.left = function ()
	if (actions.switch()) then kb.press("left"); end
end

--@help Navigate down
actions.down = function ()
	if (actions.switch()) then kb.press("down"); end
end

--@help Navigate right
actions.right = function ()
	if (actions.switch()) then kb.press("right"); end
end

--@help Navigate up
actions.up = function ()
	if (actions.switch()) then kb.press("up"); end
end

--@help Select
actions.select = function ()
	if (actions.switch()) then kb.press("return"); end
end

--@help Toggle Fullscreen
actions.fullscreen = function ()
	if (actions.switch()) then kb.stroke("alt", "return"); end
end