21 Aug 2020 - The wiki is undergoing a major cleanup. If you arrived here from a search engine and cannot find the page, please use the wiki search instead (top right corner of page)
More info can be found on the forum

Difference between revisions of "Context Item Add-ons"

From Official Kodi Wiki
Jump to: navigation, search
(update to jarvis api)
Line 15: Line 15:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The first <menu> element defines which top level menu to attach to. In core, there are two available: <code>kodi.core.main</code> and <code>kodi.core.manage</code>. Add-on may attach to menu defined by other add-ons as well. Next, the <item> element defines the action to add to the menu. <code>library</code> is the file to execute, <label> a string or an ID from the language file to show in the context menu.
+
The first <menu> element defines which top level menu to attach to. In core, there are two available: <code>kodi.core.main</code> and <code>kodi.core.manage</code>. Add-on may attach to menu defined by other add-ons as well. Next, the <item> element defines the action to add to the menu. <code>library</code> is the file to execute, <label> a string or an ID from the language file to show in the context menu. Each menu need to have unique <code>library</code> specified. Duplicate items are ignored.
  
 
<visible> (required) should contain a [[Conditional visibility|visibility expression]] and will determine when the item is be visible in the [[context menu]].  
 
<visible> (required) should contain a [[Conditional visibility|visibility expression]] and will determine when the item is be visible in the [[context menu]].  
 
  
 
This feature requires at least version 2.23.0 of the Python API (Jarvis), which should be specified in the [[Addon.xml#<requires>]] element:
 
This feature requires at least version 2.23.0 of the Python API (Jarvis), which should be specified in the [[Addon.xml#<requires>]] element:
Line 25: Line 24:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
{{note|Kodi v19 Matrix and later.}}
 +
 +
In Kodi v19 Matrix and later, it is possible to specify an optional <code>args</code> attribute to a context item. The contents of this attribute will be passed as an argument to the script specified in the <code>library</code> attribute. It can be accessed via <code>sys.argv</code>. The combination of <code>args</code> and <code>library</code> need to be unique. Duplicate combinations are ignored.
 +
 +
<syntaxhighlight lang="xml">
 +
<extension point="kodi.context.item">
 +
  <menu id="kodi.core.main">
 +
    <item library="contextitem.py" args="command_arg">
 +
      <label>30000</label>
 +
      <visible>true</visible>
 +
    </item>
 +
  </menu>
 +
</extension>
 +
</syntaxhighlight>
  
 
{{Note|Keep in mind that there is limited space in the [[context menu]]. Attaching context menu to an add-on will force the user to have all of them enabled at the same time. Consider using [[#submenus]] or a creating a separate add-on.}}
 
{{Note|Keep in mind that there is limited space in the [[context menu]]. Attaching context menu to an add-on will force the user to have all of them enabled at the same time. Consider using [[#submenus]] or a creating a separate add-on.}}
Line 38: Line 51:
 
     <label>My submenu</label>
 
     <label>My submenu</label>
 
     <item library="contextitem1.py">
 
     <item library="contextitem1.py">
       <label>Item 1</label>
+
       <label>30001</label>
 
       <visible>true</visible>
 
       <visible>true</visible>
 
     </item>
 
     </item>
 
     <item library="contextitem2.py">
 
     <item library="contextitem2.py">
       <label>Item 2</label>
+
       <label>30002</label>
 
       <visible>true</visible>
 
       <visible>true</visible>
 
     </item>
 
     </item>
Line 48: Line 61:
 
       <label>Subsubmenu</label>
 
       <label>Subsubmenu</label>
 
       <item library="contextitem3.py">
 
       <item library="contextitem3.py">
         <label>Item 3</label>
+
         <label>30003</label>
 
         <visible>true</visible>
 
         <visible>true</visible>
 
       </item>
 
       </item>

Revision as of 17:51, 25 April 2020

Context item add-ons allows the context menu in Kodi to be extended. Add-ons must provide the kodi.context.item extension point and a <menu> definition.

Example:

<extension point="kodi.context.item">
  <menu id="kodi.core.main">
    <item library="contextitem.py">
      <label>30000</label>
      <visible>true</visible>
    </item>
  </menu>
</extension>

The first <menu> element defines which top level menu to attach to. In core, there are two available: kodi.core.main and kodi.core.manage. Add-on may attach to menu defined by other add-ons as well. Next, the <item> element defines the action to add to the menu. library is the file to execute, <label> a string or an ID from the language file to show in the context menu. Each menu need to have unique library specified. Duplicate items are ignored.

<visible> (required) should contain a visibility expression and will determine when the item is be visible in the context menu.

This feature requires at least version 2.23.0 of the Python API (Jarvis), which should be specified in the Addon.xml#<requires> element:

<import addon="xbmc.python" version="2.23.0"/>

Note: Kodi v19 Matrix and later.

In Kodi v19 Matrix and later, it is possible to specify an optional args attribute to a context item. The contents of this attribute will be passed as an argument to the script specified in the library attribute. It can be accessed via sys.argv. The combination of args and library need to be unique. Duplicate combinations are ignored.

<extension point="kodi.context.item">
  <menu id="kodi.core.main">
    <item library="contextitem.py" args="command_arg">
      <label>30000</label>
      <visible>true</visible>
    </item>
  </menu>
</extension>

Note: Keep in mind that there is limited space in the context menu. Attaching context menu to an add-on will force the user to have all of them enabled at the same time. Consider using #submenus or a creating a separate add-on.


1 Submenus

<menu> and <item> elements may be mixed and nested to created submenus. Example:

<menu id="kodi.core.main">
  <menu>
    <label>My submenu</label>
    <item library="contextitem1.py">
      <label>30001</label>
      <visible>true</visible>
    </item>
    <item library="contextitem2.py">
      <label>30002</label>
      <visible>true</visible>
    </item>
    <menu>
      <label>Subsubmenu</label>
      <item library="contextitem3.py">
        <label>30003</label>
        <visible>true</visible>
      </item>
    </menu>
</menu>


2 Accessing information about selected item

The sys.listitem attribute contains a copy of the item the context menu was opened on. It is an instance of xbmcgui.ListItem. See http://mirrors.xbmc.org/docs/python-docs/16.x-jarvis/xbmcgui.html#ListItem for available properties.

Example script:

import sys
import xbmc

if __name__ == '__main__':
    message = "Clicked on '%s'" % sys.listitem.getLabel()
    xbmc.executebuiltin("Notification(\"Hello context items!\", \"%s\")" % message)


3 v15 Isengard (deprecated)

Isengard, API version 2.20.0, does not support <menu> definition and can only define a single item as follows:

<extension point="kodi.context.item" library="addon.py">
  <item>
    <label>Hello World</label>
    <visible>!IsEmpty(ListItem.Genre)</visible>
    <parent>kodi.core.manage</parent>
  </item>
</extension>

This syntax is considered deprecated and will be removed in future versions.

Note: Both syntaxes are backwards and forward compatible. Add-ons may use both the old <item> and the new <menu> at the same time for comparability with v15 Isengard and v16 Jarvis.