Xfce Forum

Sub domains
 

You are not logged in.

#1 2023-05-12 19:46:45

chw9999
Member
Registered: 2022-01-07
Posts: 4

Start script when windows changes top/bottom/background position?

Hi there,

is there a possibility in xfce to catch the event when a window or app switches from foreground to background and vice versa, and then start a script? Reason is I use Virtualbox in seamless mode for a certain program, but don't need it running (consuming lots of CPU power) when switched into the background (=other window topping it). With a script being executed when the window is sent into the background, I could pause the VM, and restart when topped.

Googling "background" only finds tons of "how to run a command in background" topics hmm


Thanks!

Offline

#2 2023-05-13 01:29:52

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 10,990

Re: Start script when windows changes top/bottom/background position?

You could xdotool to return the currently focused window and act accordingly:

xdotool getactivewindow getwindowname

...in a script something like this:

#!/bin/bash

while true; do 
   if ! [[ "$(xdotool getactivewindow getwindowname)" == *"Mousepad" ]]; then 
      echo "Mousepad is no longer focused"
   else 
      echo "Mousepad is focused"
   fi

   sleep 1
done

Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#3 2023-05-13 07:44:03

chw9999
Member
Registered: 2022-01-07
Posts: 4

Re: Start script when windows changes top/bottom/background position?

Kudos! Thank you very much, works like a charm!

My resulting working code, VM window name for VBoxManage command retrieved with
VBoxManage list runningvms

#!/bin/bash

while true; do 
  activewindow=`xdotool getactivewindow getwindowname` # get full VM window name into variable
  echo 0 $activewindow
  activewindow7=${activewindow:0:7} # name of VM window changes when paused, so just focus on the important part of name
  echo 7 $activewindow7

  if ! [[ "$activewindow7" == *"VM_Win7" ]]; then 
    VBoxManage controlvm "VM_Win7" pause
  else 
    VBoxManage controlvm "VM_Win7" resume
  fi
  sleep 1
done

Offline

Board footer

Powered by FluxBB