Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I need assistance in creating a script that does the following:

  1. Open Gedit
  2. Open a document with Gedit
  3. Save the document twice
  4. Close Gedit

I was wondering how can I make it happen with Gedit and xdotool, as xdotool can simulate the key combination to save a document Ctrl+S

share|improve this question
add comment

1 Answer 1

up vote 5 down vote accepted

The window must be focused because otherwise gedit will ignore X events sent by xdotool.

Here's what your script should be:

#!/bin/bash

gedit -s /path/to/document
# -s is standalone, meaning a new window will be opened instead of a tab in any existing gedit

sleep 5 # wait for gedit to open, adjust if necessary

GEDPID=$!  # save PID of gedit process

sleep 2 # wait for gedit to actually open document, adjust if necessary

GEDWINID=`xdotool search --pid $GEDPID | tail -1`
# gets window ID of gedit, the third "child" is the actual window

xdotool windowactivate --sync $GEDWINID key --clearmodifiers --delay 100 ctrl+s ctrl+s alt+F4
# focus gedit window, wait until focused, then save twice and exit
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.