Get current script folder in layout script
In a global theme ./contents/layouts/org.kde.plasma.desktop-layout.js
it is currently not possible to get the current script folder, e.g. to apply a wallpaper which is packaged in the same folder. Common javascript things such as __filename
, __dirname
and path
are obviously not supported in the plasma theme scripting engine.
In order to apply a wallpaper file which is packaged in a global theme, one needs to resort to a very hacky way to get the current folder:
function getCurrentDirectory() {
try {
__filename
} catch (e) {
return e.stack.toString().split("file://")[1].split("org.kde.plasma.desktop-layout.js")[0]
}
throw "cannot get current directory"
}
// set wallpaper for all desktops
var desktopsArray = desktopsForActivity(currentActivity());
for( var j = 0; j < desktopsArray.length; j++) {
desktopsArray[j].wallpaperPlugin = 'org.kde.image';
desktopsArray[j].currentConfigGroup = Array("Wallpaper", "org.kde.image", "General")
desktopsArray[j].writeConfig("Image", getCurrentDirectory() + "/../../wallpapers/my-wallpaper.svg")
desktopsArray[j].writeConfig("FillMode", "1")
}
I'm creating this issue to document my workaround and ask for a cleaner way to reference files which are stored in the global theme folder alongside the plasma scripts.
Edited by Benjamin Flesch