KM7: Open the most recent screenshot from a location

Trying to create a macro that will open the most recent screenshot from a location using a chosen app

Screenshots named in the pattern "Screen Shot 2018-07-01 at 10.11.30 AM", are renamed in the first step for easy picking for awk

macro

It throws error rename command not found

How can I make this work in KM7

Not sure if there is a better way in KM7.

But I created a bash alias and made KM7 type at terminal to execute the alias.

Use an Execute an AppleScript with this code:

tell application "Finder" to (sort every file of the ¬
	desktop by creation date)'s last item as alias

POSIX path of result

Save to a variable. This will be the full posix path to the screenshot. Then use that in your macro as you have done in your test case.

1 Like

Thanks for the pointer about Applescript.
Had to refresh Applescript notes from the days of Applescript studio :slight_smile: to modify and do it in 1 step.

tell application "Finder" 
    open (sort every file of the ¬
    desktop by creation date)'s last item
end tell

Figured the shell script to get it working. :smile:

 #!/usr/bin/env bash

cd ~/Desktop
/usr/local/bin/rename -v 's/Screen Shot (\d{4}-\d{2}-\d{2}) at (\d{1,2}\.\d{2}\.\d{2}) ([AP]M)\.png/SShot_$1_$2_$3\.png/' *.png
SCRSHOT=$(ls|grep png| sort -r | head -1)
open -a /Applications/Preview.app ./$SCRSHOT