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.