8

I simply want to setup Emacs GUI to toggle (hide/show) visibility just like we see in guake, tilda or yakuake terminals(it is called drop down in these terminals). for example I am working with Emacs within the GUI and I have a browser window behind it, I want to hide Emacs GUI with a shortcut(e.g F12) and then see my browser (with focus) interact with or any other window, then again press F12 and I will have my Emacs GUI back(I am running KDE 5.13). Also if you know how to do this in XFCE or gnome please share your knowledge.

1

2 Answers 2

2

I have asked this question on emacs.stackexchange as well in this link. I will copy the anser here for reference. It relied on X11 to work and needs xdotool and wmctrl installed.

#!/bin/bash
######################################################################################################
# This script will toggle minimize/activate first window with specified class
# If window not found program will be launched
#
# window class can be found with next programs:
#   wmctrl -x -l
#   xprop
# No credit taken.......... Cannot read the original.....
# Found on http://blog.sokolov.me/2014/06/20/linuxx11-toggle-window-minimizemaximize/
# in Russian :) but works when adjusting the wrapping.
######################################################################################################
NEEDED_WINDOW_CLASS="emacs.Emacs"
LAUNCH_PROGRAM="emacs"
######################################################################################################
NEEDED_WINDOW_WINDOW_ID_HEX=`wmctrl -x -l | grep ${NEEDED_WINDOW_CLASS} | awk '{print $1}' | head -n 1`
NEEDED_WINDOW_WINDOW_ID_DEC=$((${NEEDED_WINDOW_WINDOW_ID_HEX}))
if [ -z "${NEEDED_WINDOW_WINDOW_ID_HEX}" ]; then
${LAUNCH_PROGRAM}
else
echo "Found window ID:${NEEDED_WINDOW_WINDOW_ID_DEC}(0x${NEEDED_WINDOW_WINDOW_ID_HEX})"
ACIVE_WINDOW_DEC=`xdotool getactivewindow`
if [ "${ACIVE_WINDOW_DEC}" == "${NEEDED_WINDOW_WINDOW_ID_DEC}" ]; then
    xdotool windowminimize ${NEEDED_WINDOW_WINDOW_ID_DEC}
else
    xdotool windowactivate ${NEEDED_WINDOW_WINDOW_ID_DEC}
    fi
fi
1
  • 1
    Not sure if it's something new in xdotool, but I was able to use xdotool search --sync --class FooBar to get the decimal window ID of target directly instead of calling wmctrl. Jun 15, 2022 at 11:05
0

Bit late but wanted to post an answer here that isn't KDE or gnome specific but instead relies on two apps that interact with the underlying display server/window managers (i.e. X11 or Wayland) to get the the job done.

Tested in Ubuntu, should work anywhere you can install wmctrl and xdotool. This example uses the konsole terminal app, but you could use any application you know the class name of. To figure out the class name, open the application and run wmctrl -lx.

#!/usr/bin/env bash

# if app is not open then launch it -- remove this if you don't want your
# shortcut to launch the application if it hasn't been launched yet
if [ -z "$(xdotool search --class konsole)" ]; then
  konsole
fi

# get current focused window and visible konsole window
CLASS="konsole"
ACTIVE_WINDOW="$(xdotool getactivewindow)"
APP_WINDOW="$(xdotool search --onlyvisible --class $CLASS)"

# if focused, minimize and hide the konsole, otherwise bring konsole to current desktop and open
if [ "$ACTIVE_WINDOW" = "$APP_WINDOW" ]; then
  xdotool getactivewindow windowminimize
else
  wmctrl -xR "$CLASS"
fi

This script assumes a few things, namely:

  1. that $(xdotool search --onlyvisible --class yourApp) returns a single match (you'll need to handle arrays/multiple matches)
  2. that you want to immediately switch to the raised (shown) GUI application
  3. that your target application isn't running purely as a daemon/service (won't be registered as a window inside X11 or Wayland otherwise)
  4. that you want the application to appear on whatever desktop space you are currently on

If you don't want 2 and 4 you can use xdotool alone, I just wanted to more closely emulate the behaviour of Guake terminal's show/hide behaviour, hence using wmctrl to bring and show the GUI application to the currently active desktop.

Since it's a bash script it's portable - i.e. you need only figure out how to set a custom shortcut inside your desktop application (KDE, Gnome, etc) and run the script using said shortcut.

KDE's docs on making custom keyboard shortcuts are here, Gnome docs on shortcuts are here.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .