Autoexec Service: Difference between revisions

From Official Kodi Wiki
Jump to navigation Jump to search
m (Ronie moved page Autoexec.py to Autoexec Service: Autoexec.py is deprecated)
No edit summary
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
= Automatically execute code when Kodi starts up. =
= Automatically execute code when Kodi starts up. =
If you wish to run some code automatically when Kodi starts, you can do so by creating a service add-on.<br>
If you wish to run some code automatically when Kodi starts, you can do so by creating a service add-on.<br>


(This replaces the old way of adding a autoexec.py file in the [[userdata]] folder, this is deprecated.)
(This replaces the now deprecated method of adding an autoexec.py file in the [[userdata]] folder.<ref>https://github.com/xbmc/xbmc/pull/18356</ref>)
 


== Create a folder for your autoexec add-on ==
== Create a folder for your autoexec add-on ==
Navigate to the '''''addons''''' folder in the [[Backup#Kodi_Data_Folder|Kodi Data Folder]]<br>
# Navigate to the '''''addons''''' folder in the [[Backup#Kodi_Data_Folder|Kodi Data Folder]]
Create a 'service.autoexec' folder in the '''''addons''''' folder.
#Create a folder named '''service.autoexec'''.
 
<gallery widths="367px" heights="189px">
<gallery widths="367px" heights="189px">
Autoexec.jpg|Folder structure
Autoexec.jpg|Folder structure
</gallery>
</gallery>


== Add an autoexec.py file ==
== Add an autoexec.py file ==
Move your existing autoexec.py file to this folder, or if you don't have one yet, create it and check out the code examples below.
Move your existing autoexec.py file to this folder, or if you don't have one yet, create it and check out the code examples below.


== Add an addon.xml file ==
== Add an addon.xml file ==
Line 32: Line 37:
</addon>
</addon>
</syntaxhighlight>
</syntaxhighlight>


== Enable your addon in kodi ==
== Enable your addon in kodi ==
Navigate to the autoexec addon in the addonbrowser and enable it:<br>
Navigate to the autoexec add-on in the addonbrowser and enable it:<br>
'''Settings > Add-ons > My add-ons > Services > Autoexec Service'''
'''Settings > Add-ons > My add-ons > Services > Autoexec Service'''


= Examples =
= Examples =
== Simple Examples ==
== Simple Examples ==


Line 43: Line 52:
<syntaxhighlight lang="python" enclose="div">
<syntaxhighlight lang="python" enclose="div">
import xbmc
import xbmc
xbmc.executebuiltin('RunScript(script.globalsearch)')
xbmc.executebuiltin('RunScript(script.globalsearch)')
</syntaxhighlight>
</syntaxhighlight>
Line 49: Line 59:
<syntaxhighlight lang="python" enclose="div">
<syntaxhighlight lang="python" enclose="div">
import xbmc
import xbmc
xbmc.executebuiltin('ActivateWindow(videos,movietitles)')
xbmc.executebuiltin('ActivateWindow(videos,movietitles)')
</syntaxhighlight>
</syntaxhighlight>


== Example using profiles==
== Example using profiles==
If you are using profiles and wish to run specific code based on the user who logs in, it can be done like this:
<syntaxhighlight lang="python" enclose="div">
<syntaxhighlight lang="python" enclose="div">
import xbmc
import xbmc
if xbmc.getInfoLabel('System.ProfileName') == 'Master user':
if xbmc.getInfoLabel('System.ProfileName') == 'Master user':
     xbmc.executebuiltin('RunScript(script.globalsearch)')
     xbmc.executebuiltin('RunScript(script.globalsearch)')
elif xbmc.getInfoLabel('System.ProfileName') == 'your username':
elif xbmc.getInfoLabel('System.ProfileName') == 'your username':
     xbmc.executebuiltin('ActivateWindow(videos,movietitles)')
     xbmc.executebuiltin('ActivateWindow(videos,movietitles)')
</syntaxhighlight>
</syntaxhighlight>




Line 67: Line 83:
[[List of built-in functions]]
[[List of built-in functions]]


[[Category:Manual]]
<References />
 
{{updated|21}}
[[Category:Python]]
[[Category:Python]]
[[Category:Settings]]
[[Category:Settings]]
[[Category:Advanced topics]]

Latest revision as of 05:00, 19 May 2024

Automatically execute code when Kodi starts up.

If you wish to run some code automatically when Kodi starts, you can do so by creating a service add-on.

(This replaces the now deprecated method of adding an autoexec.py file in the userdata folder.[1])


Create a folder for your autoexec add-on

  1. Navigate to the addons folder in the Kodi Data Folder
  2. Create a folder named service.autoexec.


Add an autoexec.py file

Move your existing autoexec.py file to this folder, or if you don't have one yet, create it and check out the code examples below.


Add an addon.xml file

Create an addon.xml file in this folder and copy and paste the following code to it:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="service.autoexec" name="Autoexec Service" version="1.0.0" provider-name="your username">
	<requires>
		<import addon="xbmc.python" version="3.0.0"/>
	</requires>
	<extension point="xbmc.service" library="autoexec.py">
	</extension>
	<extension point="xbmc.addon.metadata">
		<summary lang="en_GB">Automatically run python code when Kodi starts.</summary>
		<description lang="en_GB">The Autoexec Service will automatically be run on Kodi startup.</description>
		<platform>all</platform>
		<license>GNU GENERAL PUBLIC LICENSE Version 2</license>
	</extension>
</addon>


Enable your addon in kodi

Navigate to the autoexec add-on in the addonbrowser and enable it:
Settings > Add-ons > My add-ons > Services > Autoexec Service


Examples

Simple Examples

Here is an example that starts your favourite addon:

import xbmc

xbmc.executebuiltin('RunScript(script.globalsearch)')

And this example takes you to the movie overview:

import xbmc

xbmc.executebuiltin('ActivateWindow(videos,movietitles)')


Example using profiles

If you are using profiles and wish to run specific code based on the user who logs in, it can be done like this:

import xbmc

if xbmc.getInfoLabel('System.ProfileName') == 'Master user':
    xbmc.executebuiltin('RunScript(script.globalsearch)')

elif xbmc.getInfoLabel('System.ProfileName') == 'your username':
    xbmc.executebuiltin('ActivateWindow(videos,movietitles)')


References

Built-in scripting#Using Built-in Functions from python

List of built-in functions