Determining when a file is no longer in use without relying on filelocks.

I am using openFile:withApplication: to open files in their respective editor.


I have behavior that needs to be triggered after the file is closed within the external Application (I have no control over the external application).

The Application itself may or may not be terminated after the file is closed, but in most instances it is not.


Previously I was using CGWindowListCopyWindowInfo to determine if a file is actively open or not by polling the Window names and looking for the file name, so that when that Window no longer existed I could clean up and finalize the file. This worked perfectly.


As of Catalina, CGWindowListCopyWindowInfo seems to be restricted under the new permission set called "Screen Recording".

Due to the new permission requirements this no longer viable due to customer confusion on what it means by "screen recording" which are scary words. (As a side note, I really wish Apple would put CGWindowListCopyWindowInfo under "Automation" instead of "Screen Recording").


I could of course check for locks, but they are not reliable as because many applications do not actively lock the file including TextEdit.

Likewise LSOF will only report the file being in use for the duration it is copied into TextEdit's memory.


If I poll for modifications to the file I cannot know if the user still has closed the file's window in their text editor so I can't reliably clean up the file without risking the file still being open (but unlocked).


Do you have any advice or know of any notifications I can listen for?


Thanks.


Replies

Can you explain more about the background to this issue? What sort of app are you creating? What sort of files are you passing along?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"