How to script Yakuake with KDE

Yakuake is a drop-down terminal emulator based on KDE Konsole technology. It's also a very powerful and scriptable application we can configure to best suit our needs. In this article we'll see how to configure it such as it starts with an arbitrary number of tabs and split screens.

But before we dive in, I want to thank @_nielsvm for teaching me how to get the most out of Yakuake.

First, make sure the qdbus package is installed.

$ dpkg --get-selections | grep qdbus
qdbus              install
qdbus-qt5          install

Next, make sure the Autostart folder exists.

$ stat ~/.kde/Autostart/
  File: ‘/home/{USERNAME}/.kde/Autostart/’
  Size: 40        	Blocks: 0          IO Block: 4096   directory
Device: 27h/39d	Inode: 7921259     Links: 2
Access: (0775/drwxrwxr-x)  Uid: ( 1000/{USERNAME})   Gid: ( 1000/{USERNAME})
Access: 2016-09-23 10:16:43.634086087 +0200
Modify: 2016-09-23 10:16:43.634086087 +0200
Change: 2016-09-23 10:16:43.634086087 +0200
 Birth: -

Copy the default yakuake.desktop file to the Autostart directory and make it executable.

$ cp /usr/share/applications/kde4/yakuake.desktop ~/.kde/Autostart/yakuake.desktop && chmod +x ~/.kde/Autostart/yakuake.desktop

Next, create a new empty script and make it executable as well.

$ touch ~/.kde/Autostart/yakuake-setup && chmod +x ~/.kde/Autostart/yakuake-setup

Open the Autostart dialog either by the KDE menu, Krunner, or via the command line.

$ kcmshell5 autostart

The goal is to add the newly created script to the Script File header. If it says Enabled and is configured to run on Startup, then all is fine.

KDE's Automatically Started Applications

Now, the cool part. Open up the yakuake-setup script and add the below code.

#!/usr/bin/env bash

function instruct {
  cmd="qdbus org.kde.yakuake $1"
  eval $cmd &> /dev/null
  sleep 0.5
}

# Automatically open on startup
instruct "/yakuake/window org.kde.yakuake.toggleWindowState"

# TAB 0 (sess 1)
instruct "/yakuake/sessions org.kde.yakuake.addSession"
instruct "/yakuake/sessions org.kde.yakuake.splitSessionTopBottom 0"
# Memory
instruct "/yakuake/tabs org.kde.yakuake.setTabTitle 0 'RAM'"
# Inject command
instruct "/Sessions/1 org.kde.konsole.Session.sendText 'free -m'"
instruct "/Sessions/1 org.kde.konsole.Session.sendText \$'\n'" # hits enter

# TAB 1 (sess 2)
# System monitoring
instruct "/yakuake/tabs org.kde.yakuake.setTabTitle 1 'Monitoring'"
# Inject command
instruct "/Sessions/2 org.kde.konsole.Session.sendText 'top'"
instruct "/Sessions/2 org.kde.konsole.Session.sendText \$'\n'" # hits enter

This will allow Yakuake to start at boot with two sample 'RAM' and 'Monitoring' tabs and a split screen for demonstration purposes. Running arbitrary shell commands/scripts with Yakuake is very powerful and makes it for a really complete solution.

Aurelien Navarre

Senior Technical Solutions Analyst @Acquia - Drupalist by day, DevOps by night.

Lyon, France