Giter Club home page Giter Club logo

filesystemevents's Introduction

README

2014/11/13 2015/01/17 2015/04/04 Hoon H.

Provides dead-simple access to FSEvents framework for Swift. Low level core is written in Objective-C due to lack of support from Swift. (lacks some flags, lacks C-callback function)

How To Use

First, you need to add this framework to your Xcode project. If you're not sure how to add this framework to your existing Xcode project, please see this video posting.

Here's a minimal example code which waits for events and prints them. This single statement does everything all needed jobs to setup. Just keep the created object as long as you want to receive the events.

	import Cocoa
	import EonilFileSystemEvents

	@NSApplicationMain
	class AppDelegate: NSObject, NSApplicationDelegate {

		@IBOutlet weak var window: NSWindow!
		
		let	s1		=	FileSystemEventMonitor(
			pathsToWatch: [
				"~/Documents".stringByExpandingTildeInPath, 
				"~/Temp".stringByExpandingTildeInPath],
			latency: 1,
			watchRoot: true,
			queue: dispatch_get_main_queue()) { (events:[FileSystemEvent])->() in
				println(events)
			}
	}

If you want to stop monitoring, just remove all the strong references.

	var m: FileSystemEventMonitor? = FileSystemEventMonitor( /* arguments omitted */ )
	// Now it started.

	m = nil 
	// Now it stopped. Because it's dead. 

Please take care that object reference can be shared in Swift, so you need to ensure no other object to reference the monitor object. If you're not familiar with reference-counting, please see this: Automatic Reference Counting

See TestdriveApp target for another fully fledged Swift example. The example app starts monitoring when it becomes active, and stops monitoring when it becomes inactive. Try it!

Explanation

Use FileSystemEventMonitor class. Required informations are all noted as comments. Create it, and monitoring will start immediately. Deallocate it to stop monitoring. (RAII semantics) Supplied callback block will be notified on specified event.

Unfortunately, as Swift does not support static library target, you cannot link this as a library to a command-line programs currently. So examples had to written as an AppKit application target.

For Objective-C programs, use EonilFileSystemEventStream class. Required informations are all noted as comments. This class follows strict Objective-C conventions, so should be straightforward. It is unclear how long I will keep the Objective-C version.

Note

This project tested on OSX 10.10 with Xcode Version 7.0.

License

This library is written by Hoon H. and licensed under "MIT License".

filesystemevents's People

Contributors

eonil avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.