Is it possible to set up a script in order to minimize to tray an app in Plasma 5?

Duncan 1i5t5.duncan at cox.net
Thu Nov 8 07:28:21 GMT 2018


stakanov posted on Mon, 05 Nov 2018 19:06:35 +0100 as excerpted:

> I am using the nitrokey app. This is an app running in the tray and
> handling a cryptografic token. Unfortunately it opens full screen every
> time when I log in, and I would like it to start up at every session but
> to minimize to tray.
> In KDE4 there was an application specific setting but I think in Plasma5
> this is not the case anymore. Therefore I thought about a script...but I
> do not know if it is possible, were the script has to be place, if I can
> (given correct permissions) make it automatic for every user I wish and
> how to do this.
> If anybody could give me some more information I would appreciate.
> The app is in QT I think but I am not 100% sure. It is available in the
> opensuse security repo and the homepage is www.nitrokey.com
> 
> All works fine, it is just the behavior at startup.

I can confirm that as long as the app already places an icon in the tray, 
it's very possible to script closing whatever undesired window it may 
open at startup, as I've been doing this with various apps myself for 
quite some time.

AFAIK, in kde4, and certainly back in late kde3, it was possible to force 
an app that didn't normally have a tray icon to minimize to the tray as 
well, but that functionality was always a bit of a hack and didn't really 
work as well as a native tray app would have.  FWIW, I believe that, 
along with the fact that the notification interface is the new and 
supposedly better (YMMV, I'm not so sure myself, but at least gnome3 and 
kde4/5 have settled on a common solution, so it's better than it might 
have been) way to handle former tray apps, along with the changes for 
wayland and the fact that the plasma policy is "wayland first, no new X-
only features", are why plasma5 isn't supporting forcing to tray any 
longer.

If the app is *NOT* a native tray app on its own, then you will need 
something to force it to the tray.  I just happened across kdocker while 
searching for something else in gentoo's packages, and that looks to be a 
kde-based solution to the problem, tho I've not used it.  There should be 
others if that doesn't work for you.

If the app *IS* a native tray app, and all you want to do is 
automatically kill the full-blown GUI window it starts with, so it's only 
in the tray, until needed, that's what I do with my scripts here.

You need a CLI-based and thus scriptable window-management utility, of 
which there are several.  The one I use here is wmctrl.

wmctrl by default matches (with various options like case sensitivity, 
sub-string or exact match, etc) on window title, but also has the option 
to select the window manually, or to use the active window, both of which 
are handy for testing, or to use numeric X-window-id, if desired.  If 
more precise window matching is desired (title alone doesn't sufficiently 
isolate to only the target window) you can use xprop to retrieve window 
properties (including the window-id to feed to wmctrl) and use normal 
bash pattern matching against various properties (like the window class, 
etc, as kwin's window rules do, but there's exotic options such as icon 
properties you can match as well) as necessary.  FWIW, I don't use xprop 
for my init.popcloser script, but I do use it to retrieve window 
properties to match for some other utility scripts I run.

Once you have wmctrl correctly identifying the window, its -c option can 
be used to close the window.

As a rough example, here's the hacky hard-coded three-different-window, 
window-title match script I use, called init.popcloser.  (Actually, as 
I'm posting this I'm looking, and I think I don't actually need this any 
more as I don't use the apps any more as they were kde4-based apps and I 
replaced them, but it's still a reasonable example.)

>>>>>>>>>>>>>>>>>>>>>>>>>>>

#!/bin/bash
# sleep a few seconds to give things init time
sleep 5
# giveup after n seconds
seconds=30
# set actions flags, unset when completed
tracker=( superk_readonly superk_themes qtmpc )
debugfile="$TMPDIR/debug.init.popcloser"
exec &> "$debugfile"
echo init
#set -vx
# closeme tracksub window-title
closeme() {
	local tracksub="$1"
	echo @:$@ >> "$debugfile"
	shift
	[[ ${tracker[$tracksub]} ]] || return
	wmctrl -c "$*" && unset tracker[$tracksub]
}
for (( seconds=$seconds; $seconds; seconds-- )); do
	sleep 1
	echo $seconds
	closeme 0 "Information — KDialog"
	closeme 1 "superkaramba Themes"
	closeme 2 "QtMPC"
	[[ ${tracker[@]} ]] || break
done

<<<<<<<<<<<<<<<<<<<<<<<<<<

Obviously you'd set the tracker=() array to your own set of descriptors, 
and feed the closeme() function strings matching your window titles as 
appropriate.


As to where to put it...

Here I'm setting up only for my own user, and the settings in kde/plasma 
systemsettings under startup and shutdown, autostart, work as-is.  I'd 
suggest first setting things up for your own user with that, since it's 
easy enough, and there's a help button that loads the documentation for 
it in plasma's help center.  (Except that it doesn't seem to note that 
the location for the after plasma-session startup scripts appears to be 
~/.config/autostart-scripts, instead having a note that the feature for 
starting scripts after the session starts is bugged, but it does work 
here, on current as of a couple days ago live-git kde/plasma, with that 
directory (effectively, I've actually set some of the associated vars so 
it's not exactly that).)

Once you get the script running for your user, it should be possible to 
directly transfer the resulting user-specific settings to the appropriate 
system-level directories.  The documentation for this and more can be 
found in the kde system administration guide, here:

https://userbase.kde.org/KDE_System_Administration

In particular, the XDG hierarchy, KDE hierarchy (mostly kde4-, deprecated 
in plasma5 in favor of XDG), and environmental variables discussions 
linked from that page can be quite useful.

However, in general, if not set differently by your distro or site 
environmental variable settings, try /etc/plasma/startup (in which at 
least gentoo sticks a script with optional/disabled-by-default ssh-agent 
settings).  You might also try /etc/xdg/autostart (you'd need to create a 
*.desktop file for that one tho, but it or similar should be where the 
system-level autostart stuff, klipper, etc, is already stored), or
/etc/xdg/autostart-scripts (not sure if this works but it mirrors the 
user location), or /usr/share/plasma/autostart or autostart-scripts (may 
or may not work, /etc/xdg/* is more likely).

Of course it should also be possible, if necessary, to hack the startkde 
script or the like, if necessary, but that's messing with something 
shipped by kde via your distro, so changes there may be overwritten by 
updates and it's probably not a good idea to go changing that except as a 
last resort.

-- 
Duncan - List replies preferred.   No HTML msgs.
"Every nonfree program has a lord, a master --
and if you use the program, he is your master."  Richard Stallman





More information about the kde mailing list