Phaengris.Art hand-drawn arts, city photos, linux / dev stories

Switch KDE Activities With a Bash Script

Yes, there’s this Activity Manager panel on the left, but I hate it there. On my 32:9 it is too far to reach. One may say that a true Linux person shouldn’t move hands away from keyboard at all 😄 but well, we all have our ways. I, for one, prefer a nice set of icons right on my autohiding panel in the middle of the screen. That’d be possible with a bunch of application links in ~/.local/share/applications/, making them refer to some script which would switch activity by name.

#!/usr/bin/env bash

if [ $1 == "" ]; then
  echo "Activity name required"
  exit 1
fi

target_activity_name=$1
target_activity_id=""

for activity_id in $(qdbus org.kde.ActivityManager /ActivityManager/Activities ListActivities); do
  activity_name=$(qdbus org.kde.ActivityManager /ActivityManager/Activities ActivityName $activity_id)
  if [ $activity_name == $target_activity_name ]; then
    target_activity_id=$activity_id
  fi
done

if [ $target_activity_id == "" ]; then
  echo "Activity not found"
  exit 2
fi

qdbus org.kde.ActivityManager /ActivityManager/Activities SetCurrentActivity $target_activity_id

Investigation Story

KDE has it’s own tool to rule them all called qdbus so let’s start from here.

Running qdbus without arguments gives us a looong list of available services and we have to look for something related to activities.

$ qdbus | grep -i 'activit'
 org.kde.ActivityManager
 org.kde.runners.activities

My bet is on the first one. Let’s dig into it.

$ qdbus org.kde.ActivityManager 
/
/ActivityManager
/ActivityManager/Activities
/ActivityManager/Features
/ActivityManager/Resources
/ActivityManager/Resources/Linking
/ActivityManager/Resources/Scoring
/SLC
/Templates
/runner
$ qdbus org.kde.ActivityManager /ActivityManager/Activities
...

The command right above throws at us a long list of methods, and you may explore it if you’re in the mood, but anyway let me just share my findings with you.

$ qdbus org.kde.ActivityManager /ActivityManager/Activities SetCurrentActivity Default
false

Nope, and it returns false, so it needs something different as an argument.

$ qdbus org.kde.ActivityManager /ActivityManager/Activities ListActivities
ace1dd62-730a-471d-b2d1-511eb6a74e16
fbed4f33-26fb-44f4-b509-16a989b13aa5
3d1e189f-6999-4ea4-840e-ece68f452cc5
fd9ba82f-c3cc-4cbb-b5ee-bc6ebc280f9a
bc6d0a8d-866b-4d1d-8de5-f51c8635848a

It returns ids, so that is probably what we need.

$ qdbus org.kde.ActivityManager /ActivityManager/Activities SetCurrentActivity ace1dd62-730a-471d-b2d1-511eb6a74e16
true

Yep! It switched me from the dedicated activity where I work on this site to the default activity.

And while we want to supply the script with an activity name, not an id, we need to find a way to match one to another.

$ qdbus org.kde.ActivityManager /ActivityManager/Activities ActivityName ace1dd62-730a-471d-b2d1-511eb6a74e16
Default

Good enough to write a script.