If Safari download is finished, open it in default application

This is part of a larger macro, but I'm stuck here - I'm able to visit a website, go through the motion of getting to the download action.

However, I don't know what the best way to figure out when the download is complete + open. One thing to mention is the download will always have a different name but the same file extensions, will always be placed in the Downloads folder, and will be the most recent file at the time the macro would run.

Any thoughts?

Check for yourself but, for Safari at least, the "in progress" download is suffixed ".download".

Which suggests you could initiate the download, get the file name by checking your Downloads location for the file that ends with ".download", strip that suffix to get the "real" file name, pause until that "real" file name exists, then open it.

That -- and any other ideas -- may well depend on which browser you're using...

3 Likes

Using the work done by @Nige_S on the logic (waiting for .download to change to the true extension such as .png) this should give you the basis (in this example the file type we are looking for is .png, it has to be the latest file in the folder and we are opening it with Preview). These Actions would be inserted into your main Macro at the point you want it to happen. Or you might run it as a Subroutine.

EXAMPLE Open Latest Safari Download with Particular App.kmmacros (5.8 KB)

Click to Show Image of Macro

Okay, so what is going on?

First there is an Enclosing Action that will repeat what is inside it until a condition is met (in this case the file doesn't contain .download)

As this loop will run forever if it doesn't find the condition, I used the gear icon in its top right to stop it after 3 minutes. But you can change that to longer or shorter.

Click to Show Image

Within that Until Action are Actions to get the last added file and save it to a Variable called LOCAL__LatestFile. It has a Break From Loop Action as we only want it to look at one file (the latest) not all the files in the folder.

The following If Then Action will then open the file if it ends with .png (you would change this to your actual extension) and opens it with Preview (you would change this to the App you want to use to open the file).

So, surprisingly convoluted but it seems to work.

1 Like

Doesn't Safari have the option to do this automatically for "safe" files in preferences?

Yes, I was going to suggest that - but I think it depends what Safari thinks are safe files and it is an all or nothing option. I have this option off myself otherwise downloaded .zip files get automatically expanded.

1 Like

I'm grateful - isn't this conditional loop going to eat up quite a bit of resources?

I honestly don’t know. It will be continuously checking while the file is downloading. I suppose you could add a short pause in the loop (0.5 seconds?) so that it is checking every 0.5 seconds rather than checking as fast as the loop can run.

Click to Show Image

And it will only be running the check at the point in your Macro where you want the Macro to pause.

So, I would guess it will be fine. But you will need to check that. It is uploaded as an Example to answer your question and is not a routine I have used in a real task myself.