Difference between revisions of "Autoexec Service"

From Official Kodi Wiki
Jump to: navigation, search
Line 1: Line 1:
{{cleanup}}
+
= Automatically execute code when Kodi starts up. =
<section begin="intro" />autoexec.py is an optional python script that users can create in their [[userdata]] folders that will be executed when Kodi starts up.<section end="intro" />
+
If you wish to run some code automatically when Kodi starts, you can do so by creating a service add-on.<br>
  
== Examples ==
+
(This replaces the old way of adding a autoexec.py file in the [[userdata]] folder, this is deprecated.)
This script will open up the video files view focused on the root of C:\ when Kodi is started.
+
 
  import xbmc
+
== Create a folder for your autoexec add-on ==
  xbmc.executebuiltin('ActivateWindow(videofiles,C:\)')
+
Navigate to the '''''addons''''' folder in the [[Backup#Kodi_Data_Folder|Kodi Data Folder]]<br>
 +
Create a 'service.autoexec' folder in the '''''addons''''' folder.
 +
<gallery widths="367px" heights="189px">
 +
Autoexec.jpg|Folder structure
 +
</gallery>
 +
 
 +
== 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:
 +
<syntaxhighlight lang="XML" enclose="div">
 +
<?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>
 +
</syntaxhighlight>
 +
 
 +
== Enable your addon in kodi ==
 +
Navigate to the autoexec addon in the addonbrowser and enable it:<br>
 +
'''Settings > Add-ons > My add-ons > Services > Autoexec Service'''
 +
 
 +
= Examples =
 +
== Simple Examples ==
  
 
Here is an example that starts your favourite addon:
 
Here is an example that starts your favourite addon:
  import xbmc
+
<syntaxhighlight lang="python" enclose="div">
  xbmc.executebuiltin('RunScript(script.audio.spotimc)')
+
import xbmc
 +
xbmc.executebuiltin('RunScript(script.globalsearch)')
 +
</syntaxhighlight>
  
 
And this example takes you to the movie overview:
 
And this example takes you to the movie overview:
  import xbmc
+
<syntaxhighlight lang="python" enclose="div">
  xbmc.executebuiltin('ActivateWindow(myvideolibrary,movietitles)')
+
import xbmc
 +
xbmc.executebuiltin('ActivateWindow(videos,movietitles)')
 +
</syntaxhighlight>
 +
 
 +
== Example using profiles==
 +
<syntaxhighlight lang="python" enclose="div">
 +
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)')
 +
</syntaxhighlight>
 +
 
  
== References ==
+
= References =
 
[[Built-in scripting#Using Built-in Functions from python]]
 
[[Built-in scripting#Using Built-in Functions from python]]
  

Revision as of 20:43, 15 September 2020

1 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 old way of adding a autoexec.py file in the userdata folder, this is deprecated.)

1.1 Create a folder for your autoexec add-on

Navigate to the addons folder in the Kodi Data Folder
Create a 'service.autoexec' folder in the addons folder.

1.2 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.

1.3 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>

1.4 Enable your addon in kodi

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

2 Examples

2.1 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)')

2.2 Example using profiles

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)')


3 References

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

List of built-in functions