Get information about open files in a specified directory

This subroutine looks at the open files in a specified directory. One case where this is useful is when a macro needs to wait for an external activity (e.g., something downloading from the web). For example, if a browser is saving a file that file will appear as open until the save is complete.

Parameters
Path : The POSIX path to the directory/folder to be examined. This is required. If localPath is empty the routine returns an empty string. Only the immediate files in the specified directory are observed. Files further down in the directory tree do not appear.

Selector 1: An optional parameter to narrow down the files being examined. For example, setting Selector 1 to “pdf” will only count PDF files. Setting Selector 1 to “Chrome” will watch for files that are in use by the Chrome application.

Selector 2: A second optional criteria, applied in addition to the first. So setting Selector 1 to Chrome and Selector 2 to pdf will watch pdf files that are being used by Chrome.

Results are restricted to regular files (not directories) with names that do not begin with a period.

The returned value is a comma-delimited string that Keyboard Maestro can parse as an array. The first item in the array is the number of open files meeting the search criteria. What follows are triplets of elements for each open file containing the filename, the name of the application that has the file open, and the size of the file in Mb.

Before being returned, any commas in a filename are replace with underscores.

Files that might seem as though they should appear as open — for example an HTML file open in the browser — have sometimes been closed by their app and do not show up. For more information reference the “lsof” shell command.


Jeff Hester
Use as you like. O caveat emptor.

sub_check-for-open-files.kmmacros (36.1 KB)