Kodi Community Forum
Getting the current selection - Printable Version

+- Kodi Community Forum (https://forum.kodi.tv)
+-- Forum: Development (https://forum.kodi.tv/forumdisplay.php?fid=32)
+--- Forum: Kodi Application (https://forum.kodi.tv/forumdisplay.php?fid=93)
+--- Thread: Getting the current selection (/showthread.php?tid=135205)

Pages: 1 2


Getting the current selection - jhsrennie - 2012-07-02

I want to get XBMC's current selection from a Python script so I can do various interesting things with the media file it points to. I've tried the following:

Code:
import xbmcgui

win = xbmcgui.Window(xbmcgui.getCurrentWindowId())

curctl = win.getFocus()
print "Type of curctl is " + type(curctl).__name__

cursel = curctl.getSelectedItem()
print "Type of cursel is " + type(cursel).__name__

label = cursel.getLabel()

But this prints to the log:

Code:
NOTICE: Type of curctl is ControlList
NOTICE: Type of cursel is NoneType

and the getLabel() call then generates an exception because obviously the type None has no getLabel() function. Why does the ControlList.getSelectedItem call return None? An item was selected when I ran the script.

Is there a different/better way for a script to get the currently selected item from the Video or Music navigation lists?


RE: Getting the current selection - pieh - 2012-07-03

ControlList.getSelectedItem() will only return items that are created from python. So it will return empty for content generated by xbmc.
If You use master You could use xbmc.getInfoLabel() and "ListItem.DbID", "ListItem.DbType" infolabels and then fetch additional info via json. Alternatively You could use other existing ListItem properties (see list here http://wiki.xbmc.org/index.php?title=InfoLabels) if they supply enough info for You


RE: Getting the current selection - jhsrennie - 2012-07-03

Thanks. I might have a browse through the code and see if I can modify getSelectedItem to return items from any list. It's long seemed odd to me that a script can't get info from XBMC except by by using remote interfaces.


RE: Getting the current selection - LehighBri - 2012-09-21

I'd like to bump this thread as I have the same question. As part of the SageTV video addon (source below), I do create listitems via python, but using the couple lines below, I am unable to get the currently selected item (cursel is None).

Code:
win = xbmcgui.Window(xbmcgui.getCurrentWindowId())
curctl = win.getFocus()
cursel = curctl.getSelectedItem()

Addon source: https://github.com/kricker/plugin.video.SageTV/commits/master

Any thoughts on how to do this? The win.getFocus() does return a controllist... is there a workaround perhaps where I can loop through the items in that and check a property somehow to see which listitem is selected? Other thoughts?


RE: Getting the current selection - jhsrennie - 2012-09-21

I found the following works:

Code:
import xbmc

path = xbmc.getInfoLabel('ListItem.FileNameAndPath')
xbmc.executebuiltin('Notification(Item, ' + path + ', 2)')

JR




RE: Getting the current selection - LehighBri - 2012-09-21

(2012-09-21, 16:31)jhsrennie Wrote: I found the following works:

Code:
import xbmc

path = xbmc.getInfoLabel('ListItem.FileNameAndPath')
xbmc.executebuiltin('Notification(Item, ' + path + ', 2)')

JR

That works to get the path so thanks for that. To take it a step further, is there a way to do something similar where I can get the ListItem object itself so I can then manipulate it?


RE: Getting the current selection - jhsrennie - 2012-09-21

I don't know of any way to do that. Sorry.

JR



RE: Getting the current selection - LehighBri - 2012-09-21

Bummer. There was an easy way in Boxee... I find it hard to believe there's a way to easily add listitems but not access/get them. Any other thoughts from anyone?


RE: Getting the current selection - ronie - 2012-09-21

i *think* you need to use getFocusId:

Code:
win = xbmcgui.Window(xbmcgui.getCurrentWindowId())
curctl = win.getFocusId()
cursel = curctl.getSelectedItem()



RE: Getting the current selection - LehighBri - 2012-09-21

(2012-09-21, 20:17)ronie Wrote: i *think* you need to use getFocusId:

Code:
win = xbmcgui.Window(xbmcgui.getCurrentWindowId())
curctl = win.getFocusId()
cursel = curctl.getSelectedItem()

Getting closer... but I get an error "AttributeError: 'long' object has no attribute 'getSelectedItem'"

When I use the code below, I get the output below:

Code:
Code:
currentWindow = xbmcgui.Window(xbmcgui.getCurrentWindowId())
print "currentWindow=" + str(currentWindow)
currentControlList = currentWindow.getFocusId()
print "currentControlList=" + str(currentControlList)
selectedListItem = currentControlList.getSelectedItem()
print "selectedListItem=" + str(selectedListItem)

Output:
Code:
14:37:37 T:5568  NOTICE: currentWindow=<xbmcgui.Window object at 0x0C5F8770>
14:37:37 T:5568  NOTICE: currentControlList=504
14:37:37 T:5568   ERROR: Error Type: <type 'exceptions.AttributeError'>
14:37:37 T:5568   ERROR: Error Contents: 'long' object has no attribute 'getSelectedItem'
14:37:37 T:5568   ERROR: Traceback (most recent call last):
                                              File "C:\Users\user\AppData\Roaming\XBMC\addons\plugin.video.sagetv\contextmenuactions.py", line 87, in <module>
                                                selectedListItem = currentControlList.getSelectedItem()
                                            AttributeError: 'long' object has no attribute 'getSelectedItem'



RE: Getting the current selection - ronie - 2012-09-21

hmm... what about this:

Code:
win = xbmcgui.Window(xbmcgui.getCurrentWindowId())
curctl = win.getFocusId()
cursel = win.getControl( curctl ).getSelectedItem()



RE: Getting the current selection - LehighBri - 2012-09-22

(2012-09-21, 23:19)ronie Wrote: hmm... what about this:

Code:
win = xbmcgui.Window(xbmcgui.getCurrentWindowId())
curctl = win.getFocusId()
cursel = win.getControl( curctl ).getSelectedItem()

unfortunately the same issue... "cursel = win.getControl( curctl ).getSelectedItem()" returns None




RE: Getting the current selection - LehighBri - 2012-10-02

Thought I'd bump this. Any further thoughts ronie or anyone else per my previous post?


RE: Getting the current selection - LehighBri - 2012-10-16

Would like to bump this one more time... ultimately trying to understand how to get the selected listitem in a list so I can do things like update it's InfoLabel, context menu items, etc. Thoughts?


RE: Getting the current selection - Popeye - 2012-11-27

I'm also interested in this..