KRunner Bridge

Write krunner python plugins the quick way.

the calc.py thing runs eval() on everything you type into krunner and returns the result with a nice icon.

it does not get simpler than that, given that the bridge actually compiles, which may or may not happen for you.


Plasma 5 stopped exposing KDE Framework API to script languages (except QML) for the purpose of so-called stability (but it is not at all stable, crashing from time to time). But this brings some horrible consequence that writing krunner plugins becomes time-consuming. You have to write long C++ code, configure your C++ building toolchain, and compile them each time you modify something.

No more pains with ONE plugin for all.


UPDATE: A KDE maintainer has described a way that was newly introduced in KDE 5.11, to make use of DBus to achieve this. See this post: http://blog.davidedmundson.co.uk/blog/cross-process-runners/

Installation

Requirements:

Clone this repository to local filesystem and cd into it.

mkdir build
cd build

cmake ..
make
make install

Quick Start

We will try to write a basic calculator using python’s eval function.

Follow the installation instructions and then edit ~/.local/share/kservices5/example_calc.py:

#!/usr/bin/env python

import krunner_bridge
from math import *

@krunner_bridge.match_handler
def match(query):
    try:
        res = eval(query)
        if isinstance(res, (int, float)):
            return krunner_bridge.datasource(
                text=str(res),
                icon="accessories-calculator",
                category="Python Calculator")
    except:
        pass

if __name__ == "__main__":
    krunner_bridge.exec()

Append one line to ~/.local/share/kservices5/krunner_bridge.desktop, which tells krunner bridge where to find your script.

echo X-KRunner-Bridge-Script-1=example_calc.py >> $HOME/.local/share/kservices5/krunner_bridge.desktop

Kill and restart krunner. (NOTE: It would be more debuggable to run it in terminal than using kstart5. All stderr output from your python script will be forwarded and displayed.) That’s what it will be like when all’s done:

For more practical examples, check out example_search.py and view the effects by running ./install_examples.sh. It searches in my useful little scripts and applications, with far better fuzzy match algorithm and selection mechanism. As what shows below, when you type one more key 5 to form a query kse5, the 5th choice KColorSchemeEditor will automatically be selected.

API

FAQs