Kodi Community Forum

Full Version: Getting the current selection
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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?
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
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.
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....its/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?
I found the following works:

Code:
import xbmc

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

JR

(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?
I don't know of any way to do that. Sorry.

JR
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?
i *think* you need to use getFocusId:

Code:
win = xbmcgui.Window(xbmcgui.getCurrentWindowId())
curctl = win.getFocusId()
cursel = curctl.getSelectedItem()
(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'
hmm... what about this:

Code:
win = xbmcgui.Window(xbmcgui.getCurrentWindowId())
curctl = win.getFocusId()
cursel = win.getControl( curctl ).getSelectedItem()
(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

Thought I'd bump this. Any further thoughts ronie or anyone else per my previous post?
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?
I'm also interested in this..
Pages: 1 2