A simple easy to use / extend File System watcher using Swift
Please file any bugs you may encounter in the newer or even the current older version!
import Cocoa
import SwiftFSWatcher
class ViewController: NSViewController {
// NOTE: - Any instance of `SwiftFSWatcher` must be class scoped, like below:
/* This will not crash your app. */
let fileWatcher = SwiftFSWatcher(["/var/www/html/", "/home/Downloads/"])
let anotherWatcher = SwiftFSWatcher()
override func viewDidLoad() {
super.viewDidLoad()
/* Using `localWatcher` will crash your app. */
// let localWatcher = SwiftFSWatcher()
fileWatcher.watch { changeEvents in
for ev in changeEvents {
print("eventPath: \(ev.eventPath), eventFlag: \(ev.eventFlag), eventId: \(ev.eventId)")
// check if this event is of a file created
if ev.eventFlag == (kFSEventStreamEventFlagItemIsFile + kFSEventStreamEventFlagItemCreated) {
print("created file at path: \(ev.eventPath)")
}
}
}
// setup and listen second watcher events on files only
anotherWatcher.watchingPaths = ["/home/myFile.txt", "/root/bash_session.txt"]
anotherWatcher.watch { changeEvents in
for ev in changeEvents {
print("eventPath: \(ev.eventPath), eventFlag: \(ev.eventFlag), eventId: \(ev.eventId)")
if ev.eventFlag == (kFSEventStreamEventFlagItemIsFile + kFSEventStreamEventFlagItemInodeMetaMod + kFSEventStreamEventFlagItemModified) {
print("file modified at: \(ev.eventPath)")
}
}
}
}
}
#import "ViewController.h"
#import <SwiftFSWatcher/SwiftFSWatcher-Swift.h>
@implementation ViewController
SwiftFSWatcher * s;
- (void)viewDidLoad {
[super viewDidLoad];
s = [[SwiftFSWatcher alloc] init];
s.watchingPaths = [@[@"/path/to/some/folder/", @"/path/to/myFile.txt"] mutableCopy];
[s watch:^(NSArray<FileEvent *> * aa) {
NSLog(@"changed paths: %@", aa);
}];
}
@end
-
Include
pod 'SwiftFSWatcher'
in your Podfile- You'll need to add
use_frameworks!
since the framework is built in Swift.
- You'll need to add
-
Grab the
SwiftFSWatcher.framework
and add it to your project or build the.framework
yoursleves by downloading this project. -
Use it in your app
Have a question? Feel free to email me.
Send it in right now! I can't wait to see what you've done!
Oh No! Send in a pull request with the patch (very much appreciated) or just contact me :D