Pause Until... Download is complete

I'd like to start a file download and then wait until it's done downloading to do something else.

How can I do this in KM?

One possible way might be to wait for the file by looking at it. My instinct says there is a hidden file being used while a file is downloading, and that the file is deleted when the download is complete.

So I'd be waiting for that file to disappear before doing anything else. Something like this:

Is this how it works? If so, what's the file naming convention? If not, how can I do this in KM?

Turns out you can just wait for the file to exist if you know the path.

The file won't officially exist until it's done downloading.

This depends on the behavior of the app that is downloading the file. So, you may have to adapt your macro if you use a different app for downloading.

You can use the shell command lsof (list open files) to watch the download directory and see if the browser has any open files there. If it does, there are ongoing downloads. If not, all downloads are complete.

In the example below, lsof -c safari generates a list of all files that are currently open by processes with names beginning with "safari." Pipe that list to grep and if the path of the download folder doesn't turn up, there are no ongoing downloads. The return variable lsof_out will be empty.

The three second wait after the check may or may not be critical to your application. It gives the browser plenty of time to do its post-download housekeeping before you start mucking with stuff.

This is easily modified to wait until there are fewer than some number of ongoing downloads before you start the next.

2 Likes