KDE System Administration/PlasmaDesktopScripting: Difference between revisions

From KDE Wiki Sandbox
No edit summary
Line 35: Line 35:


{{note|API compatibility is guaranteed from version to version starting with KDE Software Compilation v4.4.0.}}
{{note|API compatibility is guaranteed from version to version starting with KDE Software Compilation v4.4.0.}}
=== Version Numbers ===
Starting with KDE SC 4.5, the version number of both the scripting API and the application is available to the script via the following read-only properties:
* ''String'' '''applicationVersion''': the version of the application, e.g. 0.3
* ''number'' '''scriptingVersion''': the version of the scripting API; e.g. in KDE SC 4.5 this is 2


=== Activities ===
=== Activities ===
Line 77: Line 84:
* ''String'' '''formFactor''': returns the form factor of the activity, e.g. "planar" for most desktop activities,"mediacenter" for media centers and either "horizontal" or "vertical" for panels.
* ''String'' '''formFactor''': returns the form factor of the activity, e.g. "planar" for most desktop activities,"mediacenter" for media centers and either "horizontal" or "vertical" for panels.
* ''Array[number]'' '''widgetIds''': a list of integer ids of all the widgets in this Activity
* ''Array[number]'' '''widgetIds''': a list of integer ids of all the widgets in this Activity
* ''Array[String]'' '''configKeys''':  (since KDE SC 4.5) a list of all keys that are set in the current configuration
* ''Array[String]'' '''configKeys''':  (scriptingVersion >= 2) a list of all keys that are set in the current configuration group
* ''Array[String]'' '''configGroups''': (since KDE SC 4.5)  a list of all the groups in the current configuration
* ''Array[String]'' '''configGroups''': (scriptingVersion >= 2)  a list of all the groups in the current configuration group
* ''Array[String]'' '''globalConfigKeys''':  (scriptingVersion >= 2) a list of all keys that are set in the current global configuration group
* ''Array[String]'' '''globalConfigGroups''': (scriptingVersion >= 2)  a list of all the groups in the current global configuration group


as well as the following read/write properties:
as well as the following read/write properties:
Line 85: Line 94:
* ''number'' '''screen''': the screen this activity is associated with, or -1 for none
* ''number'' '''screen''': the screen this activity is associated with, or -1 for none
* ''String'' '''name''': the name of this activity
* ''String'' '''name''': the name of this activity
* ''String'' '''wallpaperPlugin''': (since KDE SC 4.5) the wallpaper plugin to use with the Activity
* ''String'' '''wallpaperPlugin''': (scriptingVersion >= 2) the wallpaper plugin to use with the Activity
* ''String'' '''wallpaperMode''': (since KDE SC 4.5) the wallpaper plugin mode to use with the Activity
* ''String'' '''wallpaperMode''': (scriptingVersion >= 2) the wallpaper plugin mode to use with the Activity
* ''Array[String]'' '''currentConfigGroup''': (since KDE SC 4.5) the current configuration group path, with each entry in the array representing a sub-group. This allows one to access trees of groups with code such as: widget.currentConfigGroup = new Array('topGroup', 'subGroupOfTopGroup'). An empty Array means the default (top-level) configuration group for the widget  
* ''Array[String]'' '''currentConfigGroup''': (scriptingVersion >= 2) the current configuration group path, with each entry in the array representing a sub-group. This allows one to access trees of groups with code such as: widget.currentConfigGroup = new Array('topGroup', 'subGroupOfTopGroup'). An empty Array means the default (top-level) configuration group for the widget  


and the following methods:
and the following methods:
Line 97: Line 106:
* ''Widget'' '''addWidget(Widget widget)''': adds an existing widget to this activity; useful for moving widgets between Activities and Panels
* ''Widget'' '''addWidget(Widget widget)''': adds an existing widget to this activity; useful for moving widgets between Activities and Panels
* '''showConfigurationInteface()''': shows the configuration user interface for this Activity or Panel on the screen
* '''showConfigurationInteface()''': shows the configuration user interface for this Activity or Panel on the screen
* '''readConfig(String key, any default)''': (since KDE SC 4.5) reads the value of key in the config with default for the default value
* '''readConfig(String key, any default)''': (scriptingVersion >= 2) reads the value of key in the config with default for the default value
* '''writeConfig(String key, any value)''': (since KDE SC 4.5) sets key to value in the config  
* '''writeConfig(String key, any value)''': (scriptingVersion >= 2) sets key to value in the config
* '''reloadConfig()''': (since KDE SC 4.5) causes the Activity or Panel to reload its configuration; reaction to configuration changes made using readConfig are usually activated on script exit, but this can be triggered earlier on a per-widget basis using this method
* '''readGlobalConfig(String key, any default)''': (scriptingVersion >= 2) reads the value of key in the global config with default
 
  for the default value
* '''writeGlobalConfig(String key, any value)''': (scriptingVersion >= 2) sets key to value in the global config
* '''reloadConfig()''': (scriptingVersion >= 2) causes the Activity or Panel to reload its configuration; reaction to configuration changes made using readConfig are usually activated on script exit, but this can be triggered earlier on a per-widget basis using this method
* ''Array[String]'' '''currentGlobalConfigGroup''': (scriptingVersion >= 2)  the current global configuration group path, with each entry in the array representing a sub-group, similar to currentConfigGroup. However, global configuration is shared by all instances of panels and activities of the same type.


In addition to all of the above properties and functions, Panel objects also provide the folowing read/write properties:
In addition to all of the above properties and functions, Panel objects also provide the folowing read/write properties:
Line 118: Line 130:
* ''Array[String]'' '''configKeys''': a list of all keys that are set in the current configuration
* ''Array[String]'' '''configKeys''': a list of all keys that are set in the current configuration
* ''Array[String]'' '''configGroups''': a list of all the groups in the current configuration
* ''Array[String]'' '''configGroups''': a list of all the groups in the current configuration
* ''Array[String]'' '''globalConfigKeys''':  (scriptingVersion >= 2) a list of all keys that are set in the current global configuration group
* ''Array[String]'' '''globalConfigGroups''': (scriptingVersion >= 2)  a list of all the groups in the current global configuration group
* ''number'' '''index''': the layout index of the widget; in a Panel this corresponds to the order in appears in
* ''number'' '''index''': the layout index of the widget; in a Panel this corresponds to the order in appears in


Line 125: Line 139:
* ''String'' '''globalShortcut''': the shortcut sequence (in the format used by QKeySequence, e.g. "Alt+F1") associated with this widget
* ''String'' '''globalShortcut''': the shortcut sequence (in the format used by QKeySequence, e.g. "Alt+F1") associated with this widget
* ''Array[String]'' '''currentConfigGroup''': the current configuration group path, with each entry in the array representing a sub-group. This allows one to access trees of groups with code such as: widget.currentConfigGroup = new Array('topGroup', 'subGroupOfTopGroup'). An empty Array means the default (top-level) configuration group for the widget
* ''Array[String]'' '''currentConfigGroup''': the current configuration group path, with each entry in the array representing a sub-group. This allows one to access trees of groups with code such as: widget.currentConfigGroup = new Array('topGroup', 'subGroupOfTopGroup'). An empty Array means the default (top-level) configuration group for the widget
* ''Array[String]'' '''currentGlobalConfigGroup''': (scriptingVersion >= 2)  the current global configuration group path, with each entry in the array representing a sub-group, similar to currentConfigGroup. However, global configuration is shared by all instances of widgets of the same type.


and the following methods:
and the following methods:
Line 132: Line 147:
   for the default value
   for the default value
* '''writeConfig(String key, any value)''': sets key to value in the config
* '''writeConfig(String key, any value)''': sets key to value in the config
* '''readGlobalConfig(String key, any default)''': (scriptingVersion >= 2) reads the value of key in the global config with default
  for the default value
* '''writeGlobalConfig(String key, any value)''': (scriptingVersion >= 2) sets key to value in the global config
* '''reloadConfig()''': causes the widget to reload its configuration; reaction to configuration changes made using readConfig are usually activated on script exit, but this can be triggered earlier on a per-widget basis using this method
* '''reloadConfig()''': causes the widget to reload its configuration; reaction to configuration changes made using readConfig are usually activated on script exit, but this can be triggered earlier on a per-widget basis using this method
* '''showConfigurationInteface()''': shows the configuration user interface for this widget on the screen
* '''showConfigurationInteface()''': shows the configuration user interface for this widget on the screen

Revision as of 19:39, 25 April 2010

ECMA Script Interaction With The Plasma Desktop Shell (plasma-desktop)

It is possible to control and interact with a plasma-desktop session using ECMA Script (aka JavaScript). This scripting mechanism exposes containments (Desktop Activities and Panels), widgets and various other aspects of plasma-desktop configuration using the widely known and used ECMA Script language. The QtScript engine is used for the runtime environment.

This document describes the API that is provided along with how to run such scripts in plasma-desktop.

Running Scripts

There are three ways that scripts can be executed in plasma-desktop:

  • on first run: when plasma-desktop is started without any pre-existing configuration, any scripts in $APPDATA/plasma-desktop/init/ with a ".js" suffix are run. If there is more than one script, they are run sequentially in the alphabetical order of the file names.
Note
For security reasons, scripts located in the user's home directory will not be run during this phase.


  • on update: when plasma-desktop is started, it will check in
 `kde4-config --path data`/plasma-desktop/updates/ 

with a ".js" suffix for scripts that have not yet been run. If there is more than one script which has not been run yet they will be executed serially in the alphabetical order of the file names.

A record of which update scripts have been run is kept in the application's config file in the [Updates] group. This means that if the plasma-desktop configuraiton file is removed, all the update scripts will be run again.

Note
For security reasons, scripts located in the user's home directory will not be run during this phase.


  • interactively: an interactive scripting dialog can be requested either via the KRunner window (Alt+F2, by default, or via the "Run Command" entry in various desktop menus) by entering "desktop console" as the search term. It can also be triggered directly via dbus with qdbus org.kde.plasma-desktop /MainApplication showInteractiveConsole
ECMA Script may be entered directly into this window for execution and output appears in the lower half of the window. Ctrl+E is a shortcut to run scripts, and scripts can be saved to and loaded from disk.
Scripts from files can also be loaded using KRunner with "desktop console /path/to/file" or via dbus with

qdbus org.kde.plasma-desktop /MainApplication loadScriptInInteractiveConsole /path/to/file

API

In addition to the normal ECMA Script API and the Qt-specific extensions (such as signal/slot support) provided by QtScript, the following API is provided for use by scripts.

All of the API below, unless otherwise noted with a version noticed, appear as below in the KDE Software Compilation v4.4.0 and later. API that is not noted as being part of a given class or object is part of the global namespace.

Note
API compatibility is guaranteed from version to version starting with KDE Software Compilation v4.4.0.


Version Numbers

Starting with KDE SC 4.5, the version number of both the scripting API and the application is available to the script via the following read-only properties:

  • String applicationVersion: the version of the application, e.g. 0.3
  • number scriptingVersion: the version of the scripting API; e.g. in KDE SC 4.5 this is 2

Activities

Activities are the desktop layer in a plasma-desktop session and may contain widgts. In sightly more technical terms, they are desktop containments. Activities can be created, enumerated, modified and destroyed.

New Activities can be created using the Activity constructor, like this:

   var activity = new Activity("folderview")

The string passed into the constructor maps to the X-KDE-PluginInfo-Name= entry in the plugin's .desktop file). See the documentation on the Containment object class below.

Read-only properties:

  • Array[number] activityIds: returns a list of integer ids of all existing Plasma activities

Functions:

  • Activity activityById(number id): return an object representing the activity with the given id
  • Activity activityForScreen(number screen[, number dekstop]): returns an object representing the activity currently associated with the given screen and, optionally, the given desktop.

Panels

Panels can be created, enumerated, modified and destroyed. A panel object combines both a containment as well as the container itself, allowing for full control of things such as where it appears on screen and the hiding features associated with them.

New Panels can be created using the Panel constructor, like this:

   var panel = new Panel("dock")

The string passed into the constructor maps to the X-KDE-PluginInfo-Name= entry in the plugin's .desktop file).

Read-only properties:

  • Array[number] panelIds: returns a list of integer ids of all existing Plasma panels

Functions:

  • Panel panelById(int id): returns an object representing the Panel that matches the given id


Activities and Panels

Activity and Panel objects, once created by the script, or as returned by activityById, activityForScreen, or panelById) provide the following read-only properties:

  • number id: the integer id of this activity
  • String formFactor: returns the form factor of the activity, e.g. "planar" for most desktop activities,"mediacenter" for media centers and either "horizontal" or "vertical" for panels.
  • Array[number] widgetIds: a list of integer ids of all the widgets in this Activity
  • Array[String] configKeys: (scriptingVersion >= 2) a list of all keys that are set in the current configuration group
  • Array[String] configGroups: (scriptingVersion >= 2) a list of all the groups in the current configuration group
  • Array[String] globalConfigKeys: (scriptingVersion >= 2) a list of all keys that are set in the current global configuration group
  • Array[String] globalConfigGroups: (scriptingVersion >= 2) a list of all the groups in the current global configuration group

as well as the following read/write properties:

  • number desktop: the virtual desktop this activity is associated with, or -1 for none
  • number screen: the screen this activity is associated with, or -1 for none
  • String name: the name of this activity
  • String wallpaperPlugin: (scriptingVersion >= 2) the wallpaper plugin to use with the Activity
  • String wallpaperMode: (scriptingVersion >= 2) the wallpaper plugin mode to use with the Activity
  • Array[String] currentConfigGroup: (scriptingVersion >= 2) the current configuration group path, with each entry in the array representing a sub-group. This allows one to access trees of groups with code such as: widget.currentConfigGroup = new Array('topGroup', 'subGroupOfTopGroup'). An empty Array means the default (top-level) configuration group for the widget

and the following methods:

  • remove(): deletes this activity and all widgets inside of it
  • Widget widgetById(number id): returns an object representing the widget with the given id
  • Widget addWidget(String name): adds a new widget to the activity; the name maps to the X-KDE-PluginInfo-Name= entry
 in the widget's .desktop file
  • Widget addWidget(Widget widget): adds an existing widget to this activity; useful for moving widgets between Activities and Panels
  • showConfigurationInteface(): shows the configuration user interface for this Activity or Panel on the screen
  • readConfig(String key, any default): (scriptingVersion >= 2) reads the value of key in the config with default for the default value
  • writeConfig(String key, any value): (scriptingVersion >= 2) sets key to value in the config
  • readGlobalConfig(String key, any default): (scriptingVersion >= 2) reads the value of key in the global config with default
 for the default value
  • writeGlobalConfig(String key, any value): (scriptingVersion >= 2) sets key to value in the global config
  • reloadConfig(): (scriptingVersion >= 2) causes the Activity or Panel to reload its configuration; reaction to configuration changes made using readConfig are usually activated on script exit, but this can be triggered earlier on a per-widget basis using this method
  • Array[String] currentGlobalConfigGroup: (scriptingVersion >= 2) the current global configuration group path, with each entry in the array representing a sub-group, similar to currentConfigGroup. However, global configuration is shared by all instances of panels and activities of the same type.

In addition to all of the above properties and functions, Panel objects also provide the folowing read/write properties:

  • number length: the number of pixels along the screen edge used
  • number height: the height (or for vertical panels, the width) of the panel
  • String hiding: the hiding mode of the panel, one of "none" (for no hiding), "autohide", "windowscover" or "windowsbelow"
  • String alignment: right, left or center alignment of the panel (for vertical panels, right corrsponds to top and left to bottom)
  • String location: returns the location of the activity (only relevant for Panels); valid values include "top", "bottom", "left", "right" and "floating"

Widgets

Widgets may be enumerated by calling the widgetIds property on a Activity or Panel object. With a widget id in hand, a Widget object can be retrieved by calling widgetById(id) on an Activity or Panel object. New Widgets can be created with add addWidget(String) function provided by Activity and Panel objects.

A Widget object provides the following read-only properties:

  • number id: the id of the widget
  • String type: the plugin type of this widget
  • Array[String] configKeys: a list of all keys that are set in the current configuration
  • Array[String] configGroups: a list of all the groups in the current configuration
  • Array[String] globalConfigKeys: (scriptingVersion >= 2) a list of all keys that are set in the current global configuration group
  • Array[String] globalConfigGroups: (scriptingVersion >= 2) a list of all the groups in the current global configuration group
  • number index: the layout index of the widget; in a Panel this corresponds to the order in appears in

as well as the following read-write properties:

  • QRectF geometry: the geometry of the widget (settable)
  • String globalShortcut: the shortcut sequence (in the format used by QKeySequence, e.g. "Alt+F1") associated with this widget
  • Array[String] currentConfigGroup: the current configuration group path, with each entry in the array representing a sub-group. This allows one to access trees of groups with code such as: widget.currentConfigGroup = new Array('topGroup', 'subGroupOfTopGroup'). An empty Array means the default (top-level) configuration group for the widget
  • Array[String] currentGlobalConfigGroup: (scriptingVersion >= 2) the current global configuration group path, with each entry in the array representing a sub-group, similar to currentConfigGroup. However, global configuration is shared by all instances of widgets of the same type.

and the following methods:

  • remove(): deletes this widget
  • readConfig(String key, any default): reads the value of key in the config with default
 for the default value
  • writeConfig(String key, any value): sets key to value in the config
  • readGlobalConfig(String key, any default): (scriptingVersion >= 2) reads the value of key in the global config with default
 for the default value
  • writeGlobalConfig(String key, any value): (scriptingVersion >= 2) sets key to value in the global config
  • reloadConfig(): causes the widget to reload its configuration; reaction to configuration changes made using readConfig are usually activated on script exit, but this can be triggered earlier on a per-widget basis using this method
  • showConfigurationInteface(): shows the configuration user interface for this widget on the screen

Screen Geometry

Read-only properties:

  • number screenCount: returns the number of screens connected to the computer

Functions:

  • QRectF screenGeometry(number screen): returns a rect object representing the geometry of a screen

Misc. Global Properties and Functions

Read-write properties:

  • bool locked: whether the desktop shell and widgets are locked or not (settable)

Read-only property:

  • boolean hasBattery: whether or not the system has the ability to run on battery power, e.g. a laptop or mobile device

Functions:

  • sleep(number ms): sleeps the script for the specified number of millseconds

QRectF

A rectangle class is also provided for use with Widget, Panel and screen geometry properties and functions.

Read-only properites:

  • boolean empty: true if the rectangle's width or height is less than, or equal to, 0; an empty rectangle is also invalid
  • boolean null: true if the rectangle has both the width and the height set to 0; a null rectangle is also empty and not valid
  • boolean valid: true if the rectangle has a width > 0 and height 0.

Read-write properties:

  • number left
  • number top
  • number bottom
  • number right
  • number height
  • number width
  • number x
  • number y

Constructors:

  • QRectF
  • QRectF(number x, number y, number width, number height): Sets the coordinates of the rectangle's top-left corner to (x, y), and its size to the given width and height.

Functions:

  • adjust(number dx1, number dy1, number dx2, number dy2): adds dx1, dy1, dx2 and dy2 respectively to the existing coordinates of the rectangle
  • QRectF adjusted(number dx1, number dy1, number dx2, number dy2): returns a new QRectF with dx1, dy1, dx2 and dy2 added respectively to the existing coordinates of the rectangle
  • translate(number dx, number dy): translates the rect by dx, dy
  • setCoords(number x1, number y1, number x2, number y2): sets the coordinates of the rectangle's top-left corner to (x1, y1), and the coordinates of its bottom-right corner to (x2, y2).
  • setRect(number x, number y, number width, number height): sets the coordinates of the rectangle's top-left corner to (x, y), and its size to the given width and height.
  • boolean contains(number x, number y): returns true if the rect contains the point (x, y)
  • 'moveBottom(number delta): moves the bottom by delta pixels
  • moveLeft(number delta): moves the left by delta pixels
  • moveRight(number delta): moves the right by delta pixels
  • moveTo(number x, number y): moves the top left of the rect to point (x, y)
  • moveTop(number delta): moves the top by delta pixels