Created
June 5, 2012 08:00
-
-
Save mafice/2873466 to your computer and use it in GitHub Desktop.
ファイルに変更があったらSafariを勝手にリロードしてくれるやつ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # conding: utf-8 | |
| # | |
| # It works on Mac OS X only. | |
| # | |
| import os | |
| import time | |
| import pipes | |
| import glob | |
| import select | |
| import subprocess | |
| import threading | |
| script = 'tell application "Safari" to do JavaScript "location.reload(true);" in document 1' | |
| class watcher (threading.Thread): | |
| def __init__ (self, filepath): | |
| threading.Thread.__init__(self) | |
| self.setDaemon(True) | |
| self.path = filepath | |
| self.f = os.open(self.path, os.O_RDONLY) | |
| self.kq = select.kqueue() | |
| self.ke = select.kevent(self.f, filter=select.KQ_FILTER_VNODE, \ | |
| flags=select.KQ_EV_ADD | select.KQ_EV_ENABLE | select.KQ_EV_CLEAR, | |
| fflags=select.KQ_NOTE_WRITE) | |
| def __del__ (self): | |
| os.close(self.f) | |
| self.kq.close() | |
| def run (self): | |
| self.kq.control([self.ke], 0, None) | |
| while True: | |
| ev = self.kq.control([self.ke], 1, None) | |
| for e in ev: | |
| # reload the page | |
| subprocess.call('osascript -e %s' % (pipes.quote(script),), shell=True) | |
| if __name__ == "__main__": | |
| try: | |
| files = [] | |
| i=0 | |
| # the current directory | |
| dir = watcher(".") | |
| dir.start() | |
| # files | |
| for file in glob.glob("./*"): | |
| print "started watching %s" % (file,) | |
| files.append(watcher(file)) | |
| files[i].start() | |
| i+=1 | |
| while True: | |
| time.sleep(1000) | |
| except KeyboardInterrupt: | |
| exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment