Some json commands for Autohotkey including a simple "What did he say?" function
#1
I've implemented a bunch of json commands in Autohotkey that I use with my Fire Stick 4K and IR remote. I use Kodi only for video, and the code doesn't try to deal with anything else. First code block below contains the commands to show how I call them, and the second block is the code itself. I've also written a simple "What did he say" function inspired by Siri on my Apple TV. Besides Kodi, I have "What did he say" working for Youtube TV running in a browser (not shown). I didn't have any luck getting the Event Server C++ example to work, or I might've gone that way, as sending keystrokes should have removed the context sensitivity of the arrow controls among others. OTOH, this way, doing everything on the PC, I don't have to mess with uploading keymaps to my Fire Stick, which I might have had to do for some of the commands. Hope this is useful to somebody.

Commands I use:
Code:
; I've bound all these to buttons on my Sony RM-VL610 IR remote using an MCE receiver,
; ReportMappingTable (F13-F24 key combinations), and Autohotkey.

KodiExecuteAction("playpause")
KodiExecuteAction("pause")
KodiExecuteAction("stop")
KodiExecuteAction("fastforward")
KodiExecuteAction("rewind")
KodiSeek("-10")
KodiExecuteAction("stepforward")
KodiExecuteAction("bigstepforward")
KodiExecuteAction("bigstepback")
KodiExecuteAction("contextmenu")
KodiActivateWindow("home")
KodiActivateWindow("videos", "movietitles")
KodiActivateWindow("videos", "files")
KodiActivateWindow("videos", "tvshowtitles")
KodiExecuteAction("fullscreen")
KodiExecuteAction("close")
KodiExecuteAction("info")
KodiExecuteAction("codecinfo")
KodiExecuteAction("audiodelay")
KodiExecuteAction("togglewatched")
KodiExecuteAction("showsubtitles")
KodiExecuteAction("subtitledelay")
KodiExecuteAction("jumpsms2")
KodiExecuteAction("jumpsms3")
KodiExecuteAction("jumpsms4")
KodiExecuteAction("jumpsms5")
KodiExecuteAction("jumpsms6")
KodiExecuteAction("jumpsms7")
KodiExecuteAction("jumpsms8")
KodiExecuteAction("jumpsms9")
KodiExecuteAction("close")
KodiExecuteAction((KodiIsFullScreenVideo()) ? "chapterorbigstepforward" : "pageup")
KodiExecuteAction((KodiIsFullScreenVideo()) ? "chapterorbigstepback" : "pagedown")
KodiExecuteAction((KodiIsFullScreenVideo()) ? "stepback" : "left")
KodiExecuteAction((KodiIsFullScreenVideo()) ? "stepforward" : "right")
KodiExecuteAction((KodiIsFullScreenVideo()) ? "chapterorbigstepforward" : "up")
KodiExecuteAction((KodiIsFullScreenVideo()) ? "chapterorbigstepback" : "down")
KodiExecuteAction((KodiIsFullScreenVideo()) ? "stop" : "back")
KodiExecuteAction((KodiIsFullScreenVideo()) ? "osd" : "select")
KodiWhatDidHeSay(true)
; This is how I wake Kodi from a remote macro that switches inputs on my TV and AVR.
; I've configured my Fire Stick to be a 24/7 Kodi device by disabling its ability to
; sleep as described here, and it stays on Kodi absent anything to switch it away:
;
; http://www.aftvnews.com/how-to-set-custom-sleep-or-screensaver-times-on-the-amazon-fire-tv-or-stick-without-root/
if (KodiIsScreenSaverActive())
    KodiExecuteAction("select")

The Autohotkey code:
Code:
; StdOutToVar copied from https://github.com/cocobelgica/AutoHotkey-Util/blob/master/StdOutToVar.ahk
;
; If you care about more than one Kodi target, you might look into doing this as a class. I don't, so I
; used globals for the IP address and timer state.

global KodiWdhsTimerActive := false
global FireStickIPAddress := my Fire Stick's IP address

KodiExecuteAction(action)
{
    cmd := "curl -s --data-binary "
        . """{\""jsonrpc\"":\""2.0\"", \""method\"":\""Input.ExecuteAction\"", "
        . "\""params\"":{\""action\"":\"""
        . action
        . "\""}, \""id\"":1}"" -H ""content-type: application/json;"" http://"
        . FireStickIPAddress
        . ":8080/jsonrpc"
    Runwait, %cmd%, , Hide
}

KodiSeek(secs)
{
    cmd := "curl -s --data-binary "
        . """{ \""jsonrpc\"": \""2.0\"", \""method\"": \""Player.Seek\"", "
        . "\""params\"": { \""value\"": { \""seconds\"": "
        . secs
        . " }, \""playerid\"": 1 }, \""id\"": 1 }"" -H ""content-type: application/json;"" http://"
        . FireStickIPAddress
        . ":8080/jsonrpc"
    Runwait, %cmd%, , Hide
}

KodiActivateWindow(name, section = "", doReturn = true)
{
    cmd := "curl -s --data-binary "
        . """{\""jsonrpc\"":\""2.0\"", \""method\"":\""GUI.ActivateWindow\"", "
        . "\""params\"": {\""window\"": \"""
        . name . "\"""
    if (section != "")
    {
        cmd .= ", \""parameters\"": [\""" . section . "\"""
        if (doReturn)
            cmd .= ", \""return\"""
        cmd .= "]"
    }
    cmd .= "}, \""id\"":1}"" -H ""content-type: application/json;"" http://"
        . FireStickIPAddress
        . ":8080/jsonrpc"
    Runwait, %cmd%, , Hide
}

KodiIsFullScreenVideo()
{
    cmd := "curl -s --data-binary "
        . """{\""jsonrpc\"":\""2.0\"", \""method\"":\""Gui.GetProperties\"", "
        . "\""params\"": {\""properties\"": [\""currentwindow\""]}, \""id\"":1}"" "
        . "-H ""content-type: application/json;"" http://"
        . FireStickIPAddress
        . ":8080/jsonrpc"
    res := StdoutToVar(cmd)
    return InStr(res, "Fullscreen video") != 0
}

KodiIsScreenSaverActive()
{
    cmd := "curl -s --data-binary "
        . """{\""jsonrpc\"": \""2.0\"", \""method\"": \""XBMC.GetInfoBooleans\"", "
        . "\""params\"": { \""booleans\"": [\""System.ScreenSaverActive\""] }, \""id\"": 1}"" "
        . "-H ""content-type: application/json;"" http://"
        . FireStickIPAddress
        . ":8080/jsonrpc"
    res := StdoutToVar(cmd)
    return InStr(res, """result"":{""System.ScreenSaverActive"":true") != 0
}

; Simple Siri-like "What did he say" function. Seeks back 10 seconds, toggles subs, and
; toggles them again after 15 secs. Doesn't account for subtitle state or try to keep track
; of repeated invocation.

KodiWhatDidHeSay(startTimer = false)
{
    if (startTimer)
    {
        if (KodiIsFullScreenVideo())
        {
            if (!KodiWdhsTimerActive)
            {
                KodiExecuteAction("showsubtitles")
                KodiWdhsTimerActive := true
            }
            KodiSeek("-10")
            SetTimer, KodiWhatDidHeSay, -15000
        }
    }
    else if (KodiWdhsTimerActive)
    {
        KodiExecuteAction("showsubtitles")
        KodiWdhsTimerActive := false
    }
}
 
Reply

Logout Mark Read Team Forum Stats Members Help
Some json commands for Autohotkey including a simple "What did he say?" function0