Skip to content

Automatically assign numerical shortcuts to your Activites in KDE

July 6, 2011

Yesterday I was quite happy about my shortcut to launch an internet search  that I couldn’t help but play around a little more with the scripting abilities of dbus.

When Activites were first introduced in KDE I was quickly missing shortcuts to change Activites quickly when I have my fingers on the keyboard.  I stumbled across a dbus method to assign shortcuts to a specific activity . It actually works well but my problem is that I have way to many activities than I’m willing to manually create shortcuts for and most of the time I only have 3 or 4 of them open anyways. I noticed in a video of a Ubuntu Unity that they introduced some numeric shortcuts to open programs; why not try something similar with Activities.
Like the last hack we again need a script file which will be called by a global shortcut.

Safe the script somewhere in your homedirectory and make it executable.


#shortcut =  Meta + $Counter

#read existing Activities into array
ActivityList=( `qdbus org.kde.kactivitymanagerd /ActivityManager ListActivities |tr '\n' ' '` )

#save Active Activities in array and add shortcut to name
for i in "${ActivityList[@]}"; do
	if [ `qdbus org.kde.kactivitymanagerd /ActivityManager ActivityState $i` = "2" ]; then
		#store activity id
		#Activate selected Activity for COUNTER
		if [ $1 = $COUNTER ]; then
			qdbus org.kde.kactivitymanagerd /ActivityManager SetCurrentActivity  ${ActiveActivityList[$1]}
		#get current activity name and delete previous shortcut
		ActivityName="`qdbus org.kde.kactivitymanagerd /ActivityManager ActivityName $i |sed -e 's/[ ](.*)//'`"
		#$echo $ActivityName
		#add shortcut indicator to activity name
		qdbus org.kde.kactivitymanagerd /ActivityManager SetActivityName $i "${ActivityName} (${COUNTER})"
		#echo `qdbus org.kde.kactivitymanagerd /ActivityManager ActivityName $i`
		let COUNTER+=1

Next we create some Global Shortcuts calling this script with a identifier 1, 2, 3 etc as an argument.

Global Activity Shortcuts

Notice the 3 behind the script’s path for which I have assigned the Trigger Meta+3, 4 for Meta+4 etc. After you have enabled the Global Shortcuts, that’s basically it.

After adding or removing an Activity you might have to invoke the script for the shortcuts to update.

A word of caution: The script will read the name of your active Activities and either delete the previous shortcut indicator and add the current one. Therefor you shouldn’t use brackets and only single words in your Activity names since I haven’t tested the regex extensivly and it might screw up your Activity names.

This way of adding shortcuts probably makes most (only?) sense if the Activities and their shortcut names are visible. Here is a screenshot how i setup my desktop. Unfortunately the Activities are added to that panel in a somewhat unpredictable way which I haven’t entirly figured out yet so while the shortcuts are corret the numbers they are not consecutive.

KDE Activities Desktop

Powered by Blogilo


From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: