Skip to content

Instantly share code, notes, and snippets.

@boldfacedesign
Forked from JamieMason/watch.rb
Created January 21, 2014 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boldfacedesign/8539254 to your computer and use it in GitHub Desktop.
Save boldfacedesign/8539254 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'tempfile'
require 'fileutils'
# Signals
trap("SIGINT") { exit }
# Setup
TARGET_FOLDER = ARGV[0]
TARGET_URL = ARGV[1]
if TARGET_FOLDER.nil? || TARGET_URL.nil?
puts "Usage: #{$0} TARGET_FOLDER TARGET_URL"
exit
end
# Apple script wrapper class
class AppleScript
def initialize(content)
@file = Tempfile.new('applescript')
@file.write(content)
@file.close
FileUtils.chmod(0755, @file.path)
end
def run(*args)
%x[#{@file.path} #{args.join(" ")}]
end
end
# Watcher wrapper class
class Watcher
EXTENSIONS = %w( htm html css js jpg jpeg gif png php asp rb )
def initialize(path)
@directory = path
@file_list = build_file_list
@callbacks = {}
end
# Register callback to run on file change
def on_change(&block)
@callbacks[:change] = block
end
# Run the watcher event loop
def run!
loop do
new_file_list = build_file_list
unless (diff = @file_list - new_file_list).empty?
@file_list = new_file_list
sleep 0.5
diff.each do |meta|
@callbacks[:change].call(meta[0])
end
end
sleep 0.5
end
end
def build_file_list
list = EXTENSIONS.map do |ext|
Dir[File.join(@directory, "**", "*.#{ext}")]
end
list.flatten!
list.collect do |file|
[file, File.stat(file).mtime.to_i]
end
end
end
script_content = <<-APPLESCRIPT
#!/usr/bin/osascript
on run argv
set theUrl to "about:blank"
if (count of argv) > 0 then
set theUrl to item 1 of argv
if not (theUrl starts with "http://" or theUrl starts with "https://") then
set theUrl to "http://" & theUrl
end if
end if
tell application "Google Chrome"
set found to false
set theWindowIndex to 0
repeat with theWindow in every window
set theTabIndex to 0
repeat with theTab in every tab of theWindow
set theTabIndex to theTabIndex + 1
if theTab's URL starts with theUrl then
set found to true
tell application "Google Chrome" to set active tab index of theWindow to theTabIndex
tell theTab to reload
end if
end repeat
end repeat
if not found then
set URL of (active tab of (make new window)) to theUrl
end if
end tell
end run
APPLESCRIPT
# Run it
script = AppleScript.new(script_content)
watcher = Watcher.new(TARGET_FOLDER)
watcher.on_change do |path|
puts "- #{path} changed."
script.run(TARGET_URL)
end
watcher.run!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment