< Development‎ | Tutorials‎ | Plasma4
Revision as of 12:10, 24 June 2011 by Aseigo (talk | contribs)


Introduction to the Plasmoid JavaScript API

This document provides an overview/reference of the Simplified JavaScript API for Plasmoids. The "Simplified" refers to the fact that it isn't a full binding to all of Qt or KDE's libraries, but a highly focused set of bindings designed to make writing Plasmoids fast and easy, while remaining powerful.

The API in this documentation covers the JavaScript API as it appears in the KDE Software Compilation as of version 4.4, unless otherwise noted. This API ships as part of the KDE Base Runtime package, so can be relied on being there by any application that is Powered By KDE.

What Is A Simplified JavaScript Plasmoid?

This document describes the native Plasma API available to Simplified JavaScript Plasmoids. What makes them "Simplified" is that they do not have access to the entire C++ API in the Plasma, KDE and Qt libraries (let alone things lower in the API stack). This helps ensure that these Plasmoids are more likely to work properly between releases as changes in underlying API don't affect them as well as allowing Plasma to offer stronger security guarantees around them.

To denote that this Plasmoid is a Simplified JavaScript widget, ensure that in the metadata.desktop file there is this line:


What follows is a description of the runtime environment available to a Simplified JavaScript Plasmoid.


The Simplified JavaScript API is powered by Qt's QtScript system which provides access to a full featured ECMA Script interpreter. If it works in ECMA Script, it will work in a Simplified JavaScript Plasmoid. As an interesting implementation note, QtScript uses the high performance ECMA Script interpreter from WebKit and shares this code with QtWebKit.

On top of the ECMA Script language, QtScript provides Qt integration features. Probably the most useful one in this context is the use of signals and slots which is Qt's callback mechanism. Signals may be emitted in QtScript by calling the signal method in question, a signal can be connected to a slot by using the connect() method (and disconnected with disconnect()) and any function defined in the Plasmoid may be used as a slot. For example: function onClick() {

   print("We got clicked!")


function onFirstClick() {

   print("First click!")


button = new PushButton button.clicked.connect(onClick) button.clicked.connect(onFirstClick) button.clicked() This will print out:

We got clicked! First click!

on the console when the Plasmoid starts, and the "We got clicked!" again whenever the button is clicked by the user.

The object that emitted the signal that caused a slot to be called can be retrieved using the QObject sender read-only property of the global plasmoid object.


Addons (API V3)

Plasmoids may also have plugins of their own, also written in Javascript, and which are shipped separately to the Plasmoid. These are referred to as "Addons" and are packaged similarly to a Plasmoid. For more information on creating Javascript Addons, visit the Javascript Addons tutorial.

It is possible to list, load and be notified of new Addons having been installed for your Plasmoid.

  • Array[AddonInformation] listAddons(string type): an array of available addons of the provided type. The type name maps to the X-KDE-PluginInfo-Category entry in the Addon's metadata.desktop file.
  • boolean loadAddon(String type, String id): load the addon with the given id and type, return true on success. In order to be notified when the addon is successfully created, add an event listener to the "addCreated" event.

The following are the Addon events which are recognized by the Plasmoid along with the type of event objects (if any) that are passed to registered event listeners that are registered with addEventListener:

  • addonCreated: Object addOn
  • newAddonsAvaiable

AddonInformation (API V3)

The AddonInformation object contains the following read-only properties:

  • String id: the id of the Addon. Can be used with loadAddon
  • String name: a string suitable for showing the user, such as in a configuration dialog


An API extension is a security controlled set of functions and objects that are loaded on demand. These extensions are requested by the widget by listing the required and the optional extensions (if any) it wants loaded in its metadata.desktop file. This way, even prior to the widget being loaded, Plasma can know what it will want.

Required extensions are requested using the X-Plasma-RequiredExtensions key, and optional extensions with the X-Plasma-OptionalExtensions. For example:

X-Plasma-RequiredExtensions=FileDialog,MyCustomQScriptExtension X-Plasma-OptionalExtensions=LaunchApp,HTTP

The Simplified Javascript Engine then decides if the widget will actually get that extension or not. Failure to load a required extension will result in script execution being aborted.

Each of the built-in extensions provided are described below.


Provides access to open and save dialog classes: OpenFileDialog and SaveFileDialog. Both are non-modal and run asynchronously, so the signals must be used. Other than the name difference (and resulting UI variance) the API for each is identical:

  • Constructors
    • OpenFileDialog
    • SaveFileDialog
  • Properties
    • Read Only
      • array(Url) urls: the selected file, as a Url object
      • Url baseUrl, the current path (minus filename) as a Url
      • string file: the selected file, as a string
      • array(string) files: selected files (plural), as an array of strings
    • Read/Write
      • Url url: the current Url, can be read from when the user is done or assigned before to set the starting path
      • string filter: a string representing the mimetype filter; e.g. "*.cpp|C++ Source Files\n*.h|Header files" or "*.cpp" or "*.cpp|*h"
      • boolean localOnly: true to show only local files, false if network locations are Ok as well
      • boolean directoriesOnly: true to only allow selection of a directory (not a file)
      • boolean existingOnly: true if only existing files/directories may be selected
  • Functions
    • show(): when called, the dialog will be shown to the user
  • Signals
    • accepted(FileDialogProxy)': emitted when the file dialog has been successfully accepted by the user with one or more files/directories.
    • finished(FileDialogProxy): emitted when the file dialog closes, included when cancelled/closed without being accepted


This extension allows access to local files.


  • IOJob getUrl(Url url): attempts to fetch the file using an IOJob
  • IOJob getUrl(String url): attempts to fetch the file using an IOJob
  • String userDataPath([String type, String path]): (scripting version >= 4) returns the default path for user data. Called with no parameters, it returns the user's home directory. If only one string is passed in, the standard directory for that type of data in the user's home directory will be located; the following values are recognized:
    • documents
    • music
    • video
    • downloads
    • pictures
    • autostart
    • desktop (should be considered deprecated for Plasma workspaces)

If a second string is passed in, it is considered a request for a specific path and the following types are recognized:

    • apps - Applications menu (.desktop files).
    • autostart - Autostart directories (both XDG and kde-specific)
    • cache - Cached information (e.g. favicons, web-pages)
    • cgi - CGIs to run from kdehelp.
    • config - Configuration files.
    • data - Where applications store data.
    • emoticons - Emoticons themes
    • exe - Executables in $prefix/bin. findExe() for a function that takes $PATH into account.
    • html - HTML documentation.
    • icon - Icons, see KIconLoader.
    • kcfg - KConfigXT config files.
    • lib - Libraries.
    • locale - Translation files for KLocale.
    • mime - Mime types defined by KDE-specific .desktop files.
    • module - Module (dynamically loaded library).
    • qtplugins - Qt plugins (dynamically loaded objects for Qt)
    • services - Services.
    • servicetypes - Service types.
    • sound - Application sounds.
    • templates - Templates for the "Create new file" functionality.
    • wallpaper - Wallpapers.
    • tmp - Temporary files (specific for both current host and current user)
    • socket - UNIX Sockets (specific for both current host and current user)
    • xdgconf-menu - Freedesktop.org standard location for menu layout (.menu) files.
    • xdgdata-apps - Freedesktop.org standard location for application desktop files.
    • xdgdata-dirs - Freedesktop.org standard location for menu descriptions (.directory files).
    • xdgdata-mime - Freedesktop.org standard location for MIME type definitions.
    • xdgdata-icon - Freedesktop.org standard location for icons.
    • xdgdata-pixmap - Gnome-compatibility location for pixmaps.

The second parameter should be a specific resource to find the path to. An example might be userDataPath("data", "plasma-desktop").


This extensions allows access to network addresses.


  • IOJob getUrl(Url url): attempts to fetch the file using an IOJob
  • IOJob getUrl(String url): attempts to fetch the file using an IOJob


This extension allows access to data and files via http and https.


  • IOJob getUrl(Url url): attempts to fetch the file using an IOJob
  • IOJob getUrl(String url): attempts to fetch the file using an IOJob
  • bool openUrl([string|Url] url): (API v4) Opens the url in the default application (or asks the user if there is no default application for the file). The url parameter may be either a string or a Url. Returns true on success, false on failure.


Adds methods to the global plasmoid object that allow launching applications, running commands and opening files and urls.

  • bool runApplication(string application[, array files])
    Runs an application by name (can reference an installed .desktop file as well as an executable in the user's $PATH) with an optional array of files. The file array may contain either Urls or strings. Returns true on success, false on failure.
  • bool runCommand(string exe[, array args])
    Runs the executable with the given arguments. Returns true on success, false on failure.
  • bool openUrl([string|Url] url):
    Opens the url in the default application (or asks the user if there is no default application for the file). The url parameter may be either a string or a Url. Returns true on success, false on failure.
  • boolean applicationExists(String name): (scripting version >= 4) searches $PATH first, then tries in the application menu system by application storage name (aka the .desktop file name), then Name= entries for apps with installed .desktop files, then GenericName= entries for same
  • mixed defaultApplication(String kind [, boolean storageId = false]): (scripting version >= 4) returns the executable (or if storageId is true, then the app menu system id, e.g. its .desktop file name) of the default app. The "kind" parameter may be a well-known application type including "browser", "mailer", "filemanager", "terminal", "imClient" and "windowmanager" (or any other entry in share/apps/kcm_componentchooser/kcm_*.desktop); it may also be a mimetype (e.g. "application/pdf"). On failure, it returns false.
  • String applicationPath(String name): (scripting version >= 4) returns the full local path to a given application or .desktop file if it exists.

This page was last edited on 11 September 2014, at 23:27. Content is available under Creative Commons License SA 4.0 unless otherwise noted.