Here is a relatively simple solution to automatically refresh your active browser tab when any file in a directory tree changes. It works on MacOS but you need to have a little file monitoring program called fswatch installed via Homebrew.
Step 1: install fswatch with the following command.
brew install fswatch
Step 2: Create a folder to hold the scripts we’re going to create. I put mine ~/Code/autorefresh
.
Step 3: Create an AppleScript script which tells Chrome to reload the active tab. My version includes a comment with a timestamp so that I know it’s working, I called it reloadActiveChromeTab.scptd
.
tell application "Google Chrome"
reload active tab of window 1
end tell
# add current time to message
set message to (time string of (current date)) & " refreshed Chrome"
return message
Step 4: Change the permissions so that it’s executable and run it to test that the browser refreshes.
chmod +x reloadActiveChromeTab.scptd
osascript reloadActiveChromeTab.scptd
Step 5: Create a bash script that runs fswatch with the required parameters.
In my example I exclude the logs directory otherwise each browser refresh will trigger a file change that will trigger another browser refresh.
#! /bin/bash
# exclude the logs folder, watch for changes in project mysite.com and refresh active chrome tab
fswatch -o -e "logs" ~/Code/mysite.com | xargs -n1 -I{} osascript ~/Code/autorefresh/reloadActiveChromeTab.scptd
Step 6: Finally chmod this script and give it a test.
chmod +x watch-changes.sh
./watch-changes.sh
You should see a new line with the time and comment each time a file is saved in your project directory and of course the active browser tab should automatically refresh.
Kill it CTRL c
when you are done.