Skip to content
Advertisement

Looking for a simple way to watch files, run a script, and refresh the browser

File is edited. File is saved. Bash script is run upon save. Browser refreshes itself to automatically show the changes.

Windows, OS X, Linux

Any suggestions? This seems like such an important thing that’s constantly overlooked, and I would greatly appreciate learning how to achieve this between all major OSes.

Thanks, in advance.

Advertisement

Answer

There are a few options. One is to use entr on Linux and OSX – website is here. Then your command is:

ls -d * | entr sh -c 'script.bash && reloadbrowser.bash'

An alternative to entr is to use fswatch or inotifywait – the syntax is pretty similar. I believe you can use fswatch on Windows too.

Then we come onto the question of the browser refresh. It is going to be dependent on the browser and the OS. On OSX, you can use Applescript like this for Safari:

#!/usr/bin/osascript
tell application "Safari"
  set currentURL to URL of current tab of front window
  set URL of current tab of front window to currentURL
end tell

And with Google Chrome on OSX, you could use:

#!/usr/bin/osascript
tell application "Google Chrome" to reload active tab of window 1

On Linux, you can use xdotool as described here.

By the way, on OSX, I would install entr and fswatch with homebrew using:

brew install fswatch
brew install entr
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement