Finding the most recently downloaded file and opening it

Hi - I would like a simple workflow that finds the most recently downloaded file and open it.
Is there such a program?
In one particular case - the file will always have an extension *.ica
Thank you so much in advance

Howdy, welcome to the forum! As you might have already figured out, this is a great community full of people who love to help.

Question: most recently downloaded file where? I’m assuming you have a standard “Downloads” directory to where you always download files.

Below is a shell script I use to reveal the most recently downloaded file in my Dropbox/Downloads directory. It is easily modifiable to open the file instead. I can share how to do that if you’re able to answer my above question :wink:

Shell Script (click to expand/collapse)
# set directory path to variable
dirPath="/Users/$USER/Library/CloudStorage/Dropbox/Downloads"

# build list of most recently added files
itemList=$(find $dirPath \( ! -name ".DS_Store" \) -type f -print0 | xargs -0 stat -f "%m%t%N" | sort -nr | sed "/.*\/\..*/d")

# get most recently added file from list
file=$(head -n "1" <<< "$itemList" | cut -f2- -d$'\t')

# open directory and reveal most recent file
open $dirPath && open -R "$file"

# close Recents folder if open
osascript -e 'tell application "Finder"' -e 'if exists window "Recents" then' -e 'close window "Recents"' -e 'end if' -e 'end tell'

-Chris

Hi yes - it is downloaded in the downloads folder on the MacBook.
I am so sorry but I am not that advanced in computers - and don't know how to use the shell script. I would be grateful for any help ! Thank you

No problem! Try this macro on for size... see if it works for you, and if it doesn’t, let me know and we’ll go from there. I’m a little busy right now, but when a I have a few spare minutes I’ll explain the basics of the shell script.

Open most recently downloaded file.kmmacros (7.5 KB)

Macro Screenshot (click to expand/collapse)

EDIT: Ok, here’s a basic rundown of the shell script.

This sets the downloads directory as a variable for the next steps

  • dirPath="/Users/$USER/Downloads"

This next part looks complex, but basically builds list of most recently added files in that directory, removes and DS_Store files from that list, then sorts them by most recently added (most recent at top), then saves that list to a variable.

  • itemList=$(find $dirPath \( ! -name ".DS_Store" \) -type f -print0 | xargs -0 stat -f "%m%t%N" | sort -nr | sed "/.*\/\..*/d")

This part get most recently added file from the top of the list and saves it as a variable.

  • file=$(head -n "1" <<< "$itemList" | cut -f2- -d$'\t')

This part opens the downloads directory and then opens the most recent file. You can delete everything before the 2nd open if you want to open just the file and not the directory.

  • open $dirPath && open "$file"

This part is actually AppleScript, and it closes the Recents directory if it’s open for some reason (which frequently happens on my system haha)

  • osascript -e 'tell application "Finder"' -e 'if exists window "Recents" then' -e 'close window "Recents"' -e 'end if' -e 'end tell'
2 Likes

works wonderfully !!!!! Thank you so much

So now in my Macro - how do refer to this script ?

If I’m understanding you correctly, you need to know how to trigger it the macro? It sounds like you are new to Keyboard Maestro... so I’m going to refer you at this point to the Getting Started page.

While everybody on this forum is very friendly and helpful, we also appreciate folks becoming familiar with at least the basics. So there’s a good amount of information there you will need to look over to get the most out of the application. :wink:

Hi, @discoman. I use a really cool and inexpensive ($5 to activate all features) application, Dropover, that can do this and much more.

For example, when I downloaded @cdthomer's macro, Dropover displayed this shelf (because ~/Downloads is configured as a Watched Folder):

2023-08-25 20.00.49@2x

When you click on the icon in the shelf, the filename appears:

In the Preferences, the double-click action can be configured:

Many additional actions can be revealed by right-clicking in the shelf or using the â‹Ż in the upper-right:

My apologies for delayed response...as I get older I realize I am having a hard time explaining myself - I meant to say - how to run a script. I have some workflows setup in the program but all of those include clicking on an image/using keystrokes. I did not know how and where to use the shell script as it was written....like I said - getting old is no fun !
Thank you for your help - and it is working beautifully !!

1 Like

Thank you my friend - found a solution !

1 Like

No apologies needed! Glad you got it up and running, and don’t hesitate to reach out if you need further help with it! :grin: