Guest User

Untitled

a guest
Mar 17th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #!/usr/bin/ruby
  2.  
  3. # This script watches modifications on the given directory, using the new
  4. # FSEvents API in Leopard.
  5.  
  6.  
  7. # This is a modification from the original watch script provided by Apple's Developer Package
  8. # usage: watch [path/to/watch] [bash cmd to execute]
  9. # by Julius Eckert
  10.  
  11. require 'osx/foundation'
  12. OSX.require_framework '/System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework'
  13. include OSX
  14.  
  15. def die(s)
  16. $stderr.puts s
  17. exit 1
  18. end
  19.  
  20. @@last_exec = nil
  21. def exec_cmd(path)
  22. # execute given command only every 3 seconds
  23. if (@@last_exec.nil? || Time.new > @@last_exec + 3)
  24. # 2>&1 redirect STDERR into terminal output
  25. puts `#{ARGV[1]} #{path} 2>&1`
  26. @@last_exec = Time.new
  27. end
  28. end
  29.  
  30. die "Usage: #{__FILE__} <path> <command>" unless ARGV.size == 2
  31. exec_cmd(".")
  32.  
  33. fsevents_cb = proc do |stream, ctx, numEvents, paths, marks, eventIDs|
  34. paths.regard_as('*')
  35. numEvents.times do |n|
  36. puts "[watch] ==> #{paths[n]}"
  37. exec_cmd(paths[n])
  38. end
  39. end
  40.  
  41. path = ARGV.first
  42. stream = FSEventStreamCreate(
  43. KCFAllocatorDefault,
  44. fsevents_cb,
  45. nil,
  46. [path],
  47. KFSEventStreamEventIdSinceNow,
  48. 1.0,
  49. 0)
  50.  
  51.  
  52. die "Failed to create the FSEventStream" unless stream
  53.  
  54. FSEventStreamScheduleWithRunLoop(
  55. stream,
  56. CFRunLoopGetCurrent(),
  57. KCFRunLoopDefaultMode)
  58.  
  59. ok = FSEventStreamStart(stream)
  60. die "Failed to start the FSEventStream" unless ok
  61.  
  62. begin
  63. CFRunLoopRun()
  64. rescue Interrupt
  65. FSEventStreamStop(stream)
  66. FSEventStreamInvalidate(stream)
  67. FSEventStreamRelease(stream)
  68. end
Add Comment
Please, Sign In to add comment