How to run on Debian a script on each new GNOME window?
Jan 24, 1:58pm

To run a script each time a new GNOME window opens on Debian, you can use a combination of GNOME's built-in tools and scripting. Here’s a step-by-step approach:


1. Use xdotool and wmctrl

These tools allow you to detect and interact with windows.

  • Install the tools:

    bash
    sudo apt update sudo apt install xdotool wmctrl
  • Create a script (e.g., ~/window_script.sh) that runs your desired actions when a new window opens. For example:

    bash
    #!/bin/bash # Your script logic here echo "New window opened: $(wmctrl -l -p | tail -n 1)" >> ~/window_log.txt
  • Make the script executable:

    bash
    chmod +x ~/window_script.sh

2. Detect New Windows

Use a loop with wmctrl to monitor for new windows:

bash
#!/bin/bash while true; do wmctrl -l sleep 1 done | while read -r line; do # Check if the window is new (you may need to track previous windows) ~/window_script.sh done

3. Run the Monitor Script at Startup

  • Save the monitor script (e.g., ~/window_monitor.sh) and make it executable:
    bash
    chmod +x ~/window_monitor.sh
  • Add it to your GNOME startup applications:
    • Open Startup Applications from the GNOME menu.
    • Add a new entry pointing to ~/window_monitor.sh.

4. Alternative: Use GNOME Extensions

If you prefer a GUI-based approach:

  • Install the GNOME Shell Extension "Window List" or similar.
  • Use extensions like "Run on Window Open" (if available) to trigger scripts.

5. Debugging

  • Check logs (~/window_log.txt) to ensure the script runs as expected.
  • Adjust the sleep interval in the monitor script for performance.

Note: This approach is not foolproof—it polls for new windows rather than using event-driven triggers. For a more robust solution, consider using a window manager hook or a dedicated tool like devilspie2.

Would you like help refining the script for a specific use case?

Jan 24, 1:58pm

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.

#!/bin/bash # Your script logic here echo "New window opened: $(wmctrl -l -p | tail -n 1)" >> ~/window_log.txt
#!/bin/bash while true; do wmctrl -l sleep 1 done | while read -r line; do # Check if the window is new (you may need to track previous windows) ~/window_script.sh done