Scripting API
Besides the normal JavaScript API provided by QJSEngine, scripts can use the API below. Anything not listed as part of a type is in the global namespace.
Version numbers
Read-only properties:
string applicationVersion: the version of the shellnumber scriptingVersion: the version of the scripting API
Activities
Activities are similar to virtual desktops, except that each can have its own wallpaper and desktop widgets. All activities share the same panels.
For scripting, what matters is that each activity has a different desktop for each screen, and that each desktop can have its own wallpaper and widgets:
sonicshell
├── All activities
│ └── Panel for screen 0
│ └── Widgets
├── Activity "Default"
│ ├── Desktop for screen 0, with its wallpaper and widgets
│ └── Desktop for screen 1
└── Activity "Gaming"
├── Desktop for screen 0
└── Desktop for screen 1
Create activities with createActivity(). It returns a string ID; there is no Activity type.
const activityId = createActivity("Activity Name");
const desktop = desktopsForActivity(activityId)[0];
desktop.wallpaperPlugin = "org.kde.image";
desktop.currentConfigGroup = ["Wallpaper", "org.kde.image", "General"];
desktop.writeConfig("Image", "file:///path/to/image.png");
Read-only properties:
array<number> activityIds: the numeric IDs of the desktops of all activitiesarray<string> knownActivityTypes: the containment types an activity's desktop can have
Functions:
array<string> activities(): the IDs of all activitiesstring activityName(string activityId): the name of an activitysetActivityName(string activityId, string name): sets the name of an activitystring currentActivity(): the ID of the current activitysetCurrentActivity(string activityId): switches to an activitystring createActivity(string name[, string plugin]): creates an activity.pluginis the containment type of its desktops, one ofknownActivityTypes. Without it, the default type is used.
Desktops
Every screen has a desktop in each activity. A desktop is a containment that can have widgets and a wallpaper.
const desktop = desktops()[0];
desktop.wallpaperPlugin = "org.kde.image";
desktop.currentConfigGroup = ["Wallpaper", "org.kde.image", "General"];
desktop.writeConfig("Image", "file:///path/to/image.png");
Global functions:
array<Desktop> desktops(): all desktopsarray<Desktop> desktopsForActivity(string id): the desktops of an activity, one per screenDesktop desktopById(number id): the desktop with the given IDDesktop desktopForScreen(number screen): the desktop currently on a screen
Read-write properties, as well as those of containments:
-
string wallpaperPlugin: the wallpaper plugin. List the installed ones with:kpackagetool6 --type=Plasma/Wallpaper --list -
string wallpaperMode: the wallpaper plugin's mode. It has no use currently.
Panels
Panels can be created, listed, changed and removed. A panel object combines a containment with the view around it, so you control where it appears on the screen and how it hides.
const panel = new Panel;
new Panel takes an optional containment type, such as new Panel("org.kde.panel").
Global properties:
array<number> panelIds: the IDs of all panelsarray<string> knownPanelTypes: the panel types that can be created
Global functions:
Panel panelById(number id): the panel with the given IDarray<Panel> panels(): all panels
Read-write properties, as well as those of containments:
string location:top,bottom,leftorrightstring alignment:left,centerorright. On a vertical panel,leftis the top andrightthe bottom.number offset: how far the panel is moved from its alignment pointstring lengthMode:fill,fitorcustom.filltakes the available length andfitfollows the content.number length: the number of pixels used along the screen edgenumber minimumLength: the minimum of that lengthnumber maximumLength: the maximum of that lengthnumber height: the height of the panel, or the width of a vertical panelstring hiding:none,autohide,dodgewindowsorwindowsgobelow. Any other value meansnone. The property reads backnoneforwindowsgobelow.bool floating: whether the panel floats away from the screen edgestring opacity:adaptive,opaqueortranslucent
Containments
Desktops and panels are both containments. Containments have everything applets have, and more.
Read-only properties:
string formFactor:planarfor desktops,horizontalorverticalfor panelsarray<number> widgetIds: the IDs of all widgets in this containment
Read-write properties:
number screen: the screen this containment is on, or-1for nonebool locked: whether it is locked
Functions:
Widget addWidget(string name): adds a new widget by its IDWidget addWidget(string name, number x, number y, number w, number h): adds a new widget at a positionWidget addWidget(Widget widget): moves an existing widget to this containmentWidget widgetById(number id): the widget with the given IDarray<Widget> widgets([string type]): all widgets in the containment, or only those of one type
Widgets
Get widgets from a containment with widgets() or widgetById(), and create them with addWidget().
Check whether a widget is installed with the global knownWidgetTypes, which lists all widget types including containments such as org.kde.panel:
if (knownWidgetTypes.includes("someWidgetId")) {
print("It is installed on this system!");
} else {
print("It is not installed :(");
}
A widget has everything applets have, and these read-write properties:
object geometry: the geometry of the widget, as a QRectFstring globalShortcut: the shortcut associated with this widget, in the format of QKeySequence, such asAlt+F1number index: the position of the widget in its layout. In a panel this is the order the widgets appear in.string userBackgroundHints: the kind of background to use, if the widget lets the user configure it (see its background hints)bool locked: whether it is locked
Applets
Applet is the base of widgets and containments. The current groups let you reach nested configuration:
widget.currentConfigGroup = ["topGroup", "subGroupOfTopGroup"];
Read-only properties:
number id: the ID of this instancestring type: the plugin IDstring version: the version from its metadataarray<string> configKeys: the keys set in the current configuration grouparray<string> configGroups: the groups in the current configuration grouparray<string> globalConfigKeys: the keys set in the current global configuration grouparray<string> globalConfigGroups: the groups in the current global configuration group
Read-write properties:
array<string> currentConfigGroup: the current configuration group, each entry a sub-group. An empty array means the top level.array<string> currentGlobalConfigGroup: the same for the global configuration, which all instances of the same type share
Functions:
remove(): deletes it, and for a containment all the widgets in itshowConfigurationInterface(): shows its configuration windowreadConfig(string key[, any default]): reads a valuewriteConfig(string key, any value): sets a valuereadGlobalConfig(string key[, any default]): reads a value from the global configurationwriteGlobalConfig(string key, any value): sets a value in the global configurationreloadConfig(): makes it reload its configuration. Changes usually take effect when the script ends; this applies them earlier for one widget.
Screens
number screenCount: the number of screensQRectF screenGeometry(number screen): the geometry of a screennumber screenForConnector(string connector): the screen on a connector, or-1number gridUnit: the size of the grid unit, for sizing panels
Wallpaper plugins
object knownWallpaperPlugins(): an object with one key for each installed wallpaper plugin. The keys are the plugins' display names, such asImage, not their IDs. UseObject.keys()to list them.
Applications and paths
bool applicationExists(string name): searches thePATH, then the application menu by.desktopfile name, then byName=andGenericName=defaultApplication(string kind[, bool storageId = false]): the executable of the default application, or withstorageIdits.desktopfile name.kindisbrowser,mailer,filemanager,terminalorwindowmanager, or a MIME type such asapplication/pdf. Returnsfalseon failure.string applicationPath(string name): the full path of an application or.desktopfile, if it existsbool fileExists(string path): whether a file existsstring userDataPath([string type, string path]): with no arguments, the home directory.typeselects a standard directory, one ofdesktop,documents,music,video,downloads,picturesorconfig; any other value means the data directory.pathis looked up inside that directory.
const executable = "firefox";
if (applicationExists(executable)) {
print(`${executable} exists with this path: ${applicationPath(executable)}`);
print(`Its .desktop file is here: ${applicationPath("firefox.desktop")}`);
} else {
print(`${executable} does not exist`);
}
External configuration files
ConfigFile gives access to configuration files other than the shell's own:
ConfigFile(string file[, string group]): opens a file, optionally at a groupConfigFile(ConfigFile parent[, string group]): a new object whose groups are child groups ofparent
Functions:
writeEntry(string key, any value): writes a value in the current groupreadEntry(string key): reads a value from the current groupdeleteEntry(string key): deletes a key in the current group
Read-write properties:
file: the name of the file, an absolute path or a file name to find in the configuration directoriesgroup: the current group. To reach child groups, create a newConfigFilewith this one as its parent.
Read-only properties:
keyList: all keys in the current groupgroupList: all child groups in the current group
// open the kickoffrc file
const config = ConfigFile("kickoffrc");
// switch to the RecentlyUsed group
config.group = "RecentlyUsed";
// write an entry into it
config.writeEntry("MaxApplications", 25);
// now put Yes=20 into RecentlyUsed/Test
const config2 = ConfigFile(config, "Test");
config2.writeEntry("Yes", 20);
Other properties and functions
Read-write properties:
bool locked: whether the desktop shell and widgets are lockedstring theme: the name of the desktop theme
Read-only properties:
bool hasBattery: whether the system can run on battery powerstring locale: the current locale, such asen_AUstring language: the current language's namestring languageId: the current language's codebool multihead: alwaysfalse; kept for older scriptsnumber multiheadScreen: kept for older scripts
Functions:
print(any value): adds to the script's output, without a line breaksleep(number ms): pauses the scriptstring immutability():mutable,userImmutableorsystemImmutablesetImmutability(string immutability): sets itbool loadTemplate(string id): runs a panel templateloadSerializedLayout(object data): restores a layout from the data ofdumpCurrentLayoutJS, a method of the shell's D-Bus interface
QRectF
A rectangle type, used for widget and screen geometry.
Read-only properties:
bool empty: true if the width or height is 0 or less. An empty rectangle is also invalid.bool null: true if the width and height are both 0. A null rectangle is also empty and invalid.bool valid: true if the width and height are both greater than 0
Read-write properties:
number left,top,right,bottomnumber x,y,width,height
Constructors:
QRectFQRectF(number x, number y, number width, number height)
Functions:
adjust(number dx1, number dy1, number dx2, number dy2): adds to the existing coordinatesQRectF adjusted(number dx1, number dy1, number dx2, number dy2): returns a new rectangle with those addedtranslate(number dx, number dy): moves the rectangle by that muchsetCoords(number x1, number y1, number x2, number y2): sets the top-left and bottom-right cornerssetRect(number x, number y, number width, number height): sets the top-left corner and sizebool contains(number x, number y): whether the rectangle contains the pointmoveTo(number x, number y): moves the top left to the pointmoveLeft(number delta),moveRight(number delta),moveTop(number delta),moveBottom(number delta): move that edge