Skip to content

Instantly share code, notes, and snippets.

@ponpoko1968
Created November 13, 2012 06:32
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 ponpoko1968/4064318 to your computer and use it in GitHub Desktop.
Save ponpoko1968/4064318 to your computer and use it in GitHub Desktop.
fseventを使ってディレクトリを監視する。変更があったらsh.system()の内容を実行。
# 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