1

is it possible to detect if a file (e.g. a pdf file opened in preview) was closed?

The FSEvents API does not fire such an event.

Maybe I could observe the associated process or something like that?!

tshepang
  • 12,111
  • 21
  • 91
  • 136
Tobi Weißhaar
  • 1,617
  • 6
  • 26
  • 35
  • A file, or a document? If the latter, why do you need to know that the document was closed? There may be a better way to do what you really want. – Peter Hosey Oct 04 '13 at 20:34

1 Answers1

1

Yes there is the way that you can find out whether file has been closed or not. So below i tried with unix lsof -t yourFilePath command to determine the same and hence have implemented the same in cocoa. Please follow below here lsof -t yourFilePath will give you the process id of only open files. So that you can easily detect which files are closed:-

NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/bash"];
NSString *yourFilePath=@"/Users/Home/Desktop/test.doc";
[task setArguments:[NSArray arrayWithObjects: @"-c",[NSString stringWithFormat:@"%@ %@ %@",@"lsof",@"-t",yourFilePath],nil]];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];

NSFileHandle *file;
file = [pipe fileHandleForReading];

[task launch];

NSData *data;
data = [file readDataToEndOfFile];
NSString *response = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
if ([response length] > 0)
{
NSLog(@"test.doc file has been opened and process id is %@",response);
}
else
{
NSLog(@"test.doc file has been closed");
}
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
  • This will help you determine the open file as well – Hussain Shabbir Oct 04 '13 at 13:19
  • Why would you run bash for this?! Stapling together a “script” consisting of three strings joined by spaces is *asking* for “does not work with paths containing spaces or other ‘special characters’ ” bugs. There is no need for a shell script here—you can identify lsof directly by its own path and pass the two arguments directly in the array. – Peter Hosey Oct 04 '13 at 20:19
  • @Peter Hosey, I agree your point thats why i have mentioned path without any special characters up. But my point was, do cocoa provide any api to detect whether any file is open or closed? – Hussain Shabbir Oct 05 '13 at 00:50
  • No, you never mentioned that restriction in this answer. What you did do is present code that will fail when handed a path with spaces or other shell-special characters in it. Showing an example path that won't break your code is not the same thing as mentioning the cases that will break your code. – Peter Hosey Oct 05 '13 at 01:41
  • Thank you @hussainShabbir...Now I am able to detect if a file was closed...but I have a problem with PDF-files...the lsof command don't show when a PDF-file is open because when I open a PDF-file it is opened as root user...do u know why it is opened as root user? – Tobi Weißhaar Oct 05 '13 at 14:07
  • Thanks for indirectly introducing me to the wonders of the lsof command. +1. – ninehundreds Jun 25 '14 at 18:17
  • And how to do, if you are sandboxed? – Axel Zehden Jun 30 '15 at 09:07