Skip to content

Instantly share code, notes, and snippets.

Last active Dec 31, 2015
What would you like to do?
The code below is taken entirely from a post by Brett Terpstra located at: This version below is modified to work better with Jekyll (see comment below)
#!/usr/bin/env ruby
# watch.rb by Brett Terpstra, 2011 <>
# with credit to Carlo Zottmann <>
trap("SIGINT") { exit }
if ARGV.length < 2
puts "Usage: #{$0} watch_folder keyword"
puts "Example: #{$0} . mywebproject"
dev_extension = 'dev'
filetypes = ['css','html','htm','php','rb','erb','less','js']
watch_folder = ARGV[0]
keyword = ARGV[1]
puts "Watching #{watch_folder} and subfolders for changes in project files..."
while true do
files = []
filetypes.each {|type|
files += Dir.glob( File.join( watch_folder, "**", "*.#{type}" ) )
# watch a single file only. useful in cases like Jekyll where every file is
# touched each time we generate the site
files += Dir.glob(File.absolute_path(watch_folder))
new_hash = files.collect {|f| [ f, File.stat(f).mtime.to_i ] }
hash ||= new_hash
diff_hash = new_hash - hash
unless diff_hash.empty?
hash = new_hash
diff_hash.each do |df|
puts "Detected change in #{df[0]}, refreshing"
tell application "Safari"
set windowList to every window
repeat with aWindow in windowList
set tabList to every tab of aWindow
repeat with atab in tabList
if (URL of atab contains "#{keyword}") then
tell atab to do javascript "window.location.reload()"
end if
end repeat
end repeat
end tell
sleep 1
Copy link

igable commented Dec 17, 2013

I modified this script from Brett's original to work better with Jekyll.

If only a single file is passed to the script then only watch that file for changes. This is useful in for static site generators like Jekyll where all the files are touched each time the site is updated.

To use in the single file mode for Jekyll do something like the following:

watch.rb _site/index.html localhost

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment