How to switch KDE activities with a shell script / command line
If, by any weird reason :) you need to switch KDE activities with a shell script, that's how.
Why?
Investigation story
The script to switch activities
Why?
I wanted to have a script which could get me to a KDE activity by it's name. Useful for make panel shortcuts, or to use in other scripts.
Investigation story
First, how the script can find which activities I have in the system?
qdbus is the tool for communicating with KDE services. It's autocompletion is a useful way for browsing services and their methods.
$ qdbus org.kde.
Tab
returns a long list of services, which include some ActivityManager
. Let's check it.
$ qdbus org.kde.ActivityManager
Tab
gives me ActivityManager/Activities
$ qdbus org.kde.ActivityManager ActivityManager/Activities
Tab
gives me the list of methods, where I found ListActivities
, which in it's turn responds with the following
$ qdbus org.kde.ActivityManager /ActivityManager/Activities ListActivities
f88efd5a-be2b-412a-91f1-c0a2a06eb887
a30ed1cf-a6ca-47cf-afed-79eaaeb1b1cc
65688522-6e3b-414d-8e72-8ff109f50c4a
9baa0a26-52f1-42c4-9b12-4d851719022c
That's close, but not exactly what I need. We, humans, used to refer to things by their names.
Another method, ActivityName
returns the following
$ qdbus org.kde.ActivityManager /ActivityManager/Activities ActivityName f88efd5a-be2b-412a-91f1-c0a2a06eb887
Default
That's seems to be the way to determine activity name. And the name of yet another method, SetCurrentActivity
, supposes it can do the job I want to.
$ qdbus org.kde.ActivityManager /ActivityManager/Activities SetCurrentActivity f88efd5a-be2b-412a-91f1-c0a2a06eb887
true
The script to switch activities
#!/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
Enjoy :)