Skip to content

Automatically assign numerical shortcuts to your Activites in KDE

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.

#!/bin/bash

#shortcut =  Meta + $Counter
COUNTER=1

#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
		ActiveActivityList[$COUNTER]=$i
		#Activate selected Activity for COUNTER
		if [ $1 = $COUNTER ]; then
			qdbus org.kde.kactivitymanagerd /ActivityManager SetCurrentActivity  ${ActiveActivityList[$1]}
		fi
		#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
	fi
done

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

Advertisements

Howto launch a internet search with a global shortcut in KDE

I often find myself high-lightening search terms in a program for which I want to perform an internet search. Usually I would open up a browser, open a new tab and paste in the search term and hit enter; pretty tedious. with a 3 liner bash script this can be automated using one dbus command and the global shortcut feature of KDE. I’m using here google and chromium but it should work for any browser which accepts a url as a command argument.
  1. create a shell script google_search.sh with the following content and save it somewhere in your home directory.
    #!/bin/bash
    searchterm=`qdbus org.kde.klipper /klipper getClipboardHistoryItem 0`
    chromium --new-tab "http://www.google.ca/search?&q=$searchterm"

     

  2. Next create a new global custom shortcut in KDE under Systemsettings->Shortcuts And Gestures->Custom Shortcuts. at the bottom click edit -> new -> Global Shortcut and fill in under:
    Action: browse to your newly created script
    Trigger: choose a shortcut of your liking, Mine is Meta+s
    Screenshot GlobalShortcuts in SystemsettingsMake sure the global shortcut is actually checked, if it’s in a folder the folder has to be checked for it to become available.
    If you now highlight anything in a window and press Meta+s a new tab will launch and perform a search for the highlighted searchterm.