Xfce Forum

Sub domains
 

You are not logged in.

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

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

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: 11,485

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: 6

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

Registered users online in this topic: 0, guests: 1
[Bot] ClaudeBot

Board footer

Powered by FluxBB
Modified by Visman

[ Generated in 0.009 seconds, 7 queries executed - Memory usage: 538.8 KiB (Peak: 562.02 KiB) ]