User Tools

Site Tools


doc:appunti:software:kodi_execute_script

Executing a script from a Kodi plugin add-on

In trying to present my collection of images with a custom slideshow from Koid, I have tried different approaches. The first was to define an external player, the second was to create a specific plugin that would launch an external program and finally I created a plugin that launches a Kodi add-on script.

The first two methods failed because it is not possible to run a graphics program while Kodi is running.

Executing something from a plugin add-on

As explained into the article How to write a Kodi Addon it is possible to create script add-ons or plugin add-ons using Python. A plugin add-on is simply a container of media elements generally organized as a directory, without specific playing capabilities. The play of a content is generally delegated to the native Kodi players (video, audio or pictures).

Starting with Kodi 19 Matrix it is possible tu use the embedded Python 3 as the programming language for plugins and scripts.

When a plugin add-on is invoked, it produces a directory listing where some elements are sub-folders and others are playable items. If the user selects a sub-folder, the plugin is called again and it produces another listing. If instead the user selects a playable item, the appropriate Kodi player is called.

It is possibile to customize the action associated to a playable ListItem (the playable media element shown into the plugin directory), overriding the default player; so it is possible to launch a generic external Python program or run a Kodi script add-on. To obtain this you must tag the ListItem as IsPlayable = false and define its url as pointing to the plugin add-on itself. When the user select that item, the plugin is called again (call-bak) and you can execute the custom action on it.

Executing an external Python program

This statement executes a Python script (it must be Python) that resides on the filesystem. The special://home prefix actually refers to the home directory of the Kodi program. In my case it means the /home/kodi/.kodi/ directory.

xbmc.executescript('special://home/bin/my-script.py')

The following example adds a context menu item to a ListItem contained into a plugin directory. Selecting that menu item will will launch the script passing it an argument. Also in this case the script must be a Python script:

li = xbmcgui.ListItem(label='Item title')
li.addContextMenuItems([('Run myScript', 'RunScript(special://home/bin/my-script.py,arg1)')])

Executing a Kodi script add-on

Because an external program cannot use the graphic display, I had to rely on a Kodi script add-on to execute the slideshow.

The Python 3 code that I used into the plugin add-on is the following:

from urllib.parse import urlencode
query = urlencode({'context': '/path/to/playlist.m3u'})
run_addon = 'RunAddon(script.picture.my-slideshow,?%s)' % (query,)
xbmc.executebuiltin(run_addon)

Web References

doc/appunti/software/kodi_execute_script.txt · Last modified: 2023/06/05 12:26 by niccolo