Getting the current selection
#1
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?
Reply
#2
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
Always read the XBMC online-manual, FAQ and search the forums before posting.
Do NOT e-mail Team-XBMC members asking for support. Read/follow the forum rules.
For troubleshooting and bug reporting, make sure you read this first

My previous forum/trac nickname: grajen3
Reply
#3
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.
Reply
#4
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?
Reply
#5
I found the following works:

Code:
import xbmc

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

JR

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

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

Code:
win = xbmcgui.Window(xbmcgui.getCurrentWindowId())
curctl = win.getFocusId()
cursel = curctl.getSelectedItem()
Do not PM or e-mail Team-Kodi members directly asking for support.
Always read the Forum rules, Kodi online-manual, FAQ, Help and Search the forum before posting.
Reply
#10
(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'
Reply
#11
hmm... what about this:

Code:
win = xbmcgui.Window(xbmcgui.getCurrentWindowId())
curctl = win.getFocusId()
cursel = win.getControl( curctl ).getSelectedItem()
Do not PM or e-mail Team-Kodi members directly asking for support.
Always read the Forum rules, Kodi online-manual, FAQ, Help and Search the forum before posting.
Reply
#12
(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

Reply
#13
Thought I'd bump this. Any further thoughts ronie or anyone else per my previous post?
Reply
#14
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?
Reply
#15
I'm also interested in this..
Pneumatic | SABnzbd | XBMC that just works - openelec
Reply

Logout Mark Read Team Forum Stats Members Help
Getting the current selection0