Created
November 13, 2012 06:32
-
-
Save ponpoko1968/4064318 to your computer and use it in GitHub Desktop.
fseventを使ってディレクトリを監視する。変更があったらsh.system()の内容を実行。
This file contains 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
# This script watches modifications on the given directory, using the new | |
# FSEvents API in Leopard. | |
require 'osx/foundation' | |
require 'shell' | |
OSX.require_framework '/System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework' | |
include OSX | |
def die(s) | |
$stderr.puts s | |
exit 1 | |
end | |
die "Usage: #{__FILE__} <path>" unless ARGV.size == 1 | |
fsevents_cb = proc do |stream, ctx, numEvents, paths, marks, eventIDs| | |
paths.regard_as('*') | |
numEvents.times do |n| | |
puts "Event on path `#{paths[n]}' ID `#{eventIDs[n]}'" | |
sh = Shell.cd("./") | |
sh.system("**** Do your own command.*****") | |
end | |
end | |
path = ARGV.first | |
stream = FSEventStreamCreate( | |
KCFAllocatorDefault, | |
fsevents_cb, | |
nil, | |
[path], | |
KFSEventStreamEventIdSinceNow, | |
1.0, | |
0) | |
die "Failed to create the FSEventStream" unless stream | |
FSEventStreamScheduleWithRunLoop( | |
stream, | |
CFRunLoopGetCurrent(), | |
KCFRunLoopDefaultMode) | |
ok = FSEventStreamStart(stream) | |
die "Failed to start the FSEventStream" unless ok | |
begin | |
CFRunLoopRun() | |
rescue Interrupt | |
FSEventStreamStop(stream) | |
FSEventStreamInvalidate(stream) | |
FSEventStreamRelease(stream) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment