Peter Hoffmann on Stackoverflow @peterhoffmann on twitter Peter Hoffmann on Facebook Contact me per email Subscribe to Atom Feed

Peter Hoffmann

Software Engineer
prev page next page

Refresh Browser on save with inotify and xdotool

Posted on August 17, 2010

The following code is a simple solution to refresh a browser window when you save a file. It could be used with any editor and browser. It is based on Inotify for file system monitoring and xdodool to send fake keyboard input events.



while [ 1 -eq 1 ]; do
    inotifywait -q $FILE >/dev/null
    echo "$(date --rfc-3339=seconds) Refresh: $FILE"
    CUR_WID=$(xdotool getwindowfocus)
    #gets the first $BROWSER window, if you have more than one
    #$BROWSER window open, it might not refresh the right one,
    #as an alternative you can search by the window/html title
    WID=$(xdotool search --onlyvisible --class $BROWSER|head -1)
    #TITLE="window/html file title"
    #WID=$(xdotool search --title "$TITLE"|head -1)
    xdotool windowactivate $WID
    xdotool key 'ctrl+r'
    xdotool windowactivate $CUR_WID

If you run Ubuntu, you need to install the following packages: