Accessing Window IDs by name from Python Add-On
#1
There are a pile of Window IDs here:

https://kodi.wiki/view/Window_IDs

that could be used with built in functions:

https://kodi.wiki/view/List_of_built-in_functions

using
python:
xbmc.executebuiltin()

Am curious if the constants are available in the xmbc package somewhere. Haven't found them yet or an on-line example that demonstrates it.
Reply
#2
This would be better asked in addon development - moving it there.
|Banned add-ons (wiki)|Forum rules (wiki)|VPN policy (wiki)|First time user (wiki)|FAQs (wiki) Troubleshooting (wiki)|Add-ons (wiki)|Free content (wiki)|Debug Log (wiki)|

Kodi Blog Posts
Reply
#3
Have a look into https://kodi.wiki/view/Opening_Windows_and_Dialogs for examples. You can call activatewindow using xbmc.executebuiltin. eg:

xbmc.executebuiltin("ActivateWindow(Settings)")
Reply
#4
(2019-08-29, 11:22)enen92 Wrote: Have a look into https://kodi.wiki/view/Opening_Windows_and_Dialogs for examples. You can call activatewindow using xbmc.executebuiltin. eg:

xbmc.executebuiltin("ActivateWindow(Settings)")

I had done some more research into existing addon code and concluded:
python:
# It seems that we can refer to windows with Name from here:
#     https://kodi.wiki/view/Window_IDs
# unquoted. I see various examples on-line where that seems to be the case.

Though I have yet to test this, and admit I am super surprised and confused as to how that's even possible. I mean the executebuiltin function I assumed took that string as a piece of code to run, and so I sort of expect in your example Settings to be quoted, or the use of a variable (in your case WINDOW_SETTINGS_MENU), so that either of these:
python:
xbmc.executebuiltin("ActivateWindow('Settings')")
xbmc.executebuiltin("ActivateWindow(WINDOW_SETTINGS_MENU)")

would work. But I'm deeply surprised and perplexed as to how it works with those names unquoted (as literal strings) but as apparent variable references and remain case insensitive. Highly counter intuitive for a coder. Would certainly benefit from explicit examples.

This page: Opening_Windows_and_Dialogs (wiki) which I had read already BTW lacks clear Python code samples.
Reply
#5
In those built-in commands you have the commas and parenthesis as separators, so there's enough syntax in there to tell the names apart. 
It's not a Python line of code, it's a Kodi built-in line of code (it's got its own syntax).

To answer your original question, Window IDs don't seem to be accessible from Python, they are #define'd in C++. It should be easy enough to make a Python dictionary out of the wiki page.
- Go to: https://kodi.wiki/view/Window_IDs
- Open your browser dev tools (F12), go to the Javascript console and paste this and hit Enter:
javascript:
var table = document.getElementsByClassName("prettytable sortable jquery-tablesorter")[0];
var trs = table.children[1].children;
var pythonDict = '{';
var cells, dictRow, windowID;

for (var i = 0; i < trs.length; i++) {
    cells = trs[i].cells;
    if (cells.length != 4)
        continue;
    
    windowID = cells[2].innerText;
    if (windowID == '-')
        continue;
        
    dictRow = '';
    if (cells[0].innerText != '')
        dictRow += "'" + cells[0].innerText + "': " + windowID + ', ';
    dictRow += "'" + cells[1].innerText + "': " + windowID + ',\n';
    
    pythonDict += dictRow
}
pythonDict = pythonDict.substr(0, pythonDict.length-2) + '}';
console.log(pythonDict);
You'll get an easily copyable "{ . . . }" dictionary to use in your add-ons.
Reply
#6
(2019-08-30, 13:49)doko-desuka Wrote: In those built-in commands you have the commas and parenthesis as separators, so there's enough syntax in there to tell the names apart. 
It's not a Python line of code, it's a Kodi built-in line of code (it's got its own syntax).

To answer your original question, Window IDs don't seem to be accessible from Python, they are #define'd in C++. It should be easy enough to make a Python dictionary out of the wiki page.
- Go to: https://kodi.wiki/view/Window_IDs
- Open your browser dev tools (F12), go to the Javascript console and paste this and hit Enter:
javascript:
var table = document.getElementsByClassName("prettytable sortable jquery-tablesorter")[0];
var trs = table.children[1].children;
var pythonDict = '{';
var cells, dictRow, windowID;

for (var i = 0; i < trs.length; i++) {
    cells = trs.cells;
    if (cells.length != 4)
        continue;
    
    windowID = cells[2].innerText;
    if (windowID == '-')
        continue;
        
    dictRow = '';
    if (cells[0].innerText != '')
        dictRow += "'" + cells[0].innerText + "': " + windowID + ', ';
    dictRow += "'" + cells[1].innerText + "': " + windowID + ',\n';
    
    pythonDict += dictRow
}
pythonDict = pythonDict.substr(0, pythonDict.length-2) + '}';
console.log(pythonDict);
You'll get an easily copyable "{ . . . }" dictionary to use in your add-ons.

Thanks! That's neat. Methinks they should really be defined in the xbmc python package already. It's not ideal to define them in an add-on in case of change (which I expect is very unlikely as it would risk all sorts of other backward compatibility breaks). Still, the xbmc python package is where they should be defined.

Also, if the simple names work, I guess that's enough, and I'm not particularly fussed about the mechanism. What I would be keen on is to see it well documented on this page:

https://kodi.wiki/view/Opening_Windows_and_Dialogs

I can't fix it myself, bt requested wiki access offering to do so.
Reply
#7
It would be better to document such things in doxygen since docs are kept alongside the code:

https://codedocs.xyz/xbmc/xbmc/window_ids.html
Reply
#8
Ok, here's one for that doxygen page:

javascript:
var table = document.getElementsByClassName('doxtable')[0];
var trs = table.children[0].children;
var pythonDict = '{';
var cells, dictRow, windowID;

// Start at index 1 to skip the header row.
for (var i = 1; i < trs.length; i++) {
    cells = trs.cells;
    if (!cells || cells.length != 4)
        continue;

    windowID = cells[2].innerText;
    if (windowID == '-')
        continue;

    dictRow = '';
    console.log(cells[0].children);
    if (cells[0].innerText != '' && !cells[0].children.length)
        dictRow += "'" + cells[0].innerText + "': " + windowID + ', ';
    dictRow += "'" + cells[1].innerText + "': " + windowID + ',\n';

    pythonDict += dictRow
}
pythonDict = pythonDict.substr(0, pythonDict.length-2) + '}';
console.log(pythonDict);
Reply
#9
(2019-08-31, 10:09)enen92 Wrote: It would be better to document such things in doxygen since docs are kept alongside the code:

https://codedocs.xyz/xbmc/xbmc/window_ids.html

Methinks it belongs in both then. It's mildly frustrating to have a wiki page at all like: https://kodi.wiki/view/Opening_Windows_and_Dialogs if it lacks clear code samples. But concur that if there a docs at codedocs.xyz that should show some good code samples too. It's not a big thing to add and seems to be a a mild oversight, the kind typical when doc is written by someone who knows how to do stuff and isn't quite in a noob's shoes. Which is a a pretty common challenge to all of us when writing doc - to keep our noob shoes on ;-).
Reply

Logout Mark Read Team Forum Stats Members Help
Accessing Window IDs by name from Python Add-On0