Need to monitor for file changes

Overgaard

Registered
How can I receive signals, like in Linux with F_NOTIFY (etc etc) in Mac OS X to know when something has happened in a specific directory?
In Linux you would do someting like this:

#define _GNU_SOURCE
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>

static volatile int event_fd;

static void handler(int sig, siginfo_t *si, void *data) {
event_fd = si->si_fd;
}

int main(void) {
struct sigaction act;
int fd;

act.sa_sigaction = handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO;
sigaction(SIGRTMIN + 1, &act, NULL);

fd = open("./test", O_RDONLY);
fcntl(fd, F_SETSIG, SIGRTMIN + 1);
fcntl(fd, F_NOTIFY, DN_MODIFY|DN_CREATE|DN_MULTISHOT);
while(1) {
pause();
printf("Got event on fd=%d\n", event_fd);
}
}

Clearly this doesn't work on OS X.
Can anyone please help me? :)
 
I guess it depends what events you want to monitor, but AppleScript should be able to do this w/o any trouble.

There's a default AppleScript on your system that when activated will pop up a notification anytime a file has been added to a specific folder. it's very simplistic, but sounds like it's along the lines - or at least headed in the right direction - of what you want.

just CTRL click the folder, Enable Folder Actions.
CTRL click the folder again, Configure Folder Actions.
make sure the right folder is selected (and checked) in the left pane.
in the RIGHT pane, click the plus and add "add - new item alert.scpt"

that's really all there is.
but again, this may be too simplistic for what you really want. But you could probably use this scripts base to alter and enhance for your needs.

I'd give further pointers, but I'm no AppleScript guru. tho there are plenty around this site.
 
Back
Top