Opportunistic Plasma Scripting for Feature-Rich Kubuntu Deployments … and the like

In case you don’t know yet, but Plasma in KDE 4.4 supports scripting using ecmascript (aka javascript). The idea is to provide a flexible enough solution for administrators and distributors to script default setups etc.

So, now instead of patching KDE to get a modified default desktop for Kubuntu, we script it, which is of course much preferred over patching. And let me state right here, that I find javascript a rather nice language (for the Ubuntu planet readers: ECMAScript  is a very opportunistic programming language :P).

For intial setup we use 01-kubuntu-default-setup.js, I think this really shows how simple everything is. The script adds an activity and a panel, and then fills both with widgets.
Some of those get default settings right away, such as folderview, which shows the desktop by default:

folderview = activity.addWidget(“folderview”); 

folderview.writeConfig(“url”, “desktop:/”);

And others do not get added depending on conditions, like the twitter plasmoid, that only gets added if the screen is >= 1024×768 (otherwise the widgets would overlap).

if (!(screenrect.right < 1024 && screen.bottom < 768)) {



This script however already exhibits one particular problem: the systemtray supports embedding of widgets, but the API does not, so what we do is manually hack the applets in. A rather dirty approach as I’d like to think. But I find it very awesome that the scripting API already has useful properties like hasBattery, it always annoyed me that my desktop PC got a battery applet😉

For updates from 9.10 we use 01-kubuntu-10.04.js, this is a bit more magical so I’ll break the basic functionality down a bit:

  1. only use first panel – we concluded that people wouldn’t want us to mess with all their panels
  2. iterate over all widgets of the panel and check if we can find a systray and also if those widgets that we want to move to the systray are present, if so add them to an array of widgets
  3. if there is a systray, then move each of the found widgets there
  4. moving involes: checking if it is already present in the tray, adding the widget, manually migrating all settings, moving the widget

After running the script some of the default widgets from Kubuntu 9.10 should be migrated to the sytemtray, along with all their config settings.

Here’s another thing one should watch out for: variable scope is a bit of a monster!
Say you have a loop outside a function and use the counting variable “i”, then inside a function you use “i” again for another loop. What you might not know is that the i inside the function is indeed this.i (i.e. the same as i outside the function).

And finally something I was wondering about. The API documentation mentions that For security reasons, scripts located in the user’s home directory will not be run during this phase.”, which IMHO does not exactly add security:

mkdir -p /tmp/malware/share/apps/plasma-desktop/updates/

touch /tmp/malware/share/apps/plasma-desktop/updates/foo.js

export KDEDIR=/tmp/malware


and what do we get at plasma-desktop startup?:

plasma-desktop(4878)/plasma DesktopCorona::evaluateScripts: evaluating startup script: “/tmp/malware/share/apps/plasma-desktop/updates/foo.js”

But all in all is Plasma scripting great fun and actually made me learn javascript quite a bit🙂

8 thoughts on “Opportunistic Plasma Scripting for Feature-Rich Kubuntu Deployments … and the like

  1. Variable scoping becomes a bit more normal if you just prefix all local variable declarations with 'var'. Basically in your script currently you are assigning a bunch of global variables.JavaScript has some great implementations (QtScript!) and so I'm really thrilled to see it become an even more important part of KDE. But honestly the language itself sucks.🙂

  2. Using 'var' keyword for variable declaration will keep you out of the troubles of global variables.I try to declare as much as the language I'm using allows me (even if it isn't needed).

  3. "if (!(screenrect.right < 1024 && screen.bottom < 768))"Shouldn't it be if (!(screenrect.right < 1024 || screen.bottom < 768))some eeePc for example have a resolution of 1024×600 ..😉

  4. @Radio: Yes, but no. eeePc, being a netbook, gets its UI via plasma-netbook and the kubuntu netbook edition.So we are going for the save bet, which is anything >= 1024×768, even though plasma-desktop allegedly is able to squeeze the two plasmoids on the desktop even if only one constraint is meet.

  5. Javascript is a powerful language if used right, but it has some really ugly gotchas too. I'd recommend reading a good book on Javascript, like Pro Javascript Techniques by John Resig (the guy behind jQuery). Also the website of Douglas Crockford is full of useful info ( http://www.crockford.com/javascript/ ). I haven't read his book Javascript: The Good Parts, but it's probably good as well.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s