TransWikia.com

Can one see from other program if Gedit is running and contains unsaved text?

Unix & Linux Asked on November 21, 2021

I use Gedit as my favourite text editor. Is it possible to write a script for example in Python or Bash to check if Gedit is running and contains unsaved text?

One Answer

Gedit is running

pidof gedit should give you the answer

contains unsaved text

As far as I know Gedit doesn't support any DBus interface which makes this task relatively difficult but possible:

#! /bin/bash

for i in `pidof gedit`; do
    for j in `xdotool search --name --onlyvisible --pid $i`; do
        title=`xdotool getwindowname $j`
        case $title in
            **)
                echo "Found running Gedit with unsaved data"
            ;;
            *)
                echo "Gedit is open but there are no unsaved documents"
            ;;
        esac
    done
done

What this script does. It iterates over all found gedit processes, finds their visible windows, iterates over their titles and checks if the title contains * as the first character which indicates an unsaved document.

Bash/shell purists will probably want to replace `` with $( ).

Answered by Artem S. Tashkinov on November 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP