Need macro to select recently added file(s) in Finder

I really hope this isn’t a dumb question… I’m 48 hours into my install of KM and my 39 macros are already changing my life – or as David Sparks said in the Mac Power Users podcast (#164), making my computer “sing and dance.”

I need a macro that will select files in a Finder window based on their attributes like date added, or date last modified. Ideally it would find all the files in a given range, or just with a given name or type.

This would solve a lot of problems for me like this one about pulling the latest screenshot from my desktop and pasting it into a Mail message. In this case I’m trying to get DevonThink to import the latest images from my Scanbot folder on Google Drive.

This is a lot less complicated (I hope) than this post about selecting files by filename.

Thanks if you’re able to make any suggestions!

yours
Michael

1 Like

Hey Michael,

Few newbie questions are dumb, but many are ignorant. :smile:

Welcome to the wide world of Keyboard Maestro!

First off you don’t really want to select files — you want to be able to reference a file or files to send to DevonThink.

Next you need to know that the Finder makes it easy to get files but not so easy to get them sorted by date, and if you do it’s damn slow.

However there are alternatives to the Finder.

What version of OSX are you using. If 10.10.x+ then it will be fairly simple, but I’ll have to dig out the ASObjC code.

While you’re at it tell me more specifically what you want to do.

Getting files by name is easy.

Getting files by kind is easy.

Give your task step-by-step and describe the enclosing folder-structure, the name structure, and the file-type of the files to be gotten.

I’m expecting something similar to this:

~/Google Drive/Scanbot/
                     file01.jpg
                     file02.jpg

This task may be dead-simple, or it may be a trifle pesky — but it is very doable.

-Chris

Hi Chris:

I’m running 10.10.15 (Yosemite) – at least until October, when I’ll update to El Capitan.

Here’s my step by step process:

  1. Go to a specified folder.
  2. Reference the most recently updated or added file in that folder.
  3. Move it to Devonthink’s Inbox. (So I could run this macro from inside Devonthink, if necessary.)

Hey Michael,

Okay, since you’re on 10.10+ you can use ASObjC to do the job.

You can run it from Applescript Editor.app to see how it works and run it from a KM macro using an Execute an AppleScript action.

Note this is an example of what is possible and probably not a turnkey solution for your task. Some adjustments will likely be required.

------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/09/14 20:00
# dMod: 2015/09/15 19:58
# Appl: Finder & DevonThink Pro Office
# Task: Discover Last-Added file in a Folder and add it to DevonThink.
# Tags: @Applescript, @Script, @Finder, @DevonThink, @Last-Added, @File
# Note: REQUIRES OSX 10.10 or later.
------------------------------------------------------------
use framework "Foundation"
use scripting additions
------------------------------------------------------------

try
  
  # User must change this value to the appropriate folder on their System.
  set targetFolder to path to downloads folder
  
  set thePath to POSIX path of targetFolder
  set lastAddedFile to its lastAddedFileIn:thePath sortedBy:(current application's NSURLAddedToDirectoryDateKey)
  
  # Import lastAddedFile into DevonThink:
  tell application id "com.devon-technologies.thinkpro2"
    activate
    import lastAddedFile
    # To manually choose which group to add to remove line above and uncomment the next two lines.
    # set theGroup to display group selector
    # import lastAddedFile to theGroup
  end tell
  
on error e number n
  if e = "missing value doesn’t understand the “valueForKey_” message." then
    set e to "No recent file was found!" & return & return & e
  end if
  tell application "Finder"
    activate
    set ddButton to button returned of ¬
      (display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
        default button "OK" giving up after 30)
    if ddButton = "Copy Error Message" then set the clipboard to e
  end tell
end try

------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------

on lastAddedFileIn:posixPathOfFolder sortedBy:sortKey
  set keysToRequest to {current application's NSURLPathKey, ¬
    current application's NSURLIsPackageKey, ¬
    current application's NSURLIsDirectoryKey, ¬
    sortKey}
  set theFolderURL to current application's class "NSURL"'s fileURLWithPath:posixPathOfFolder
  set theNSFileManager to current application's NSFileManager's defaultManager()
  set listOfNSURLs to (theNSFileManager's contentsOfDirectoryAtURL:theFolderURL ¬
    includingPropertiesForKeys:keysToRequest ¬
    options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) ¬
    |error|:(missing value)) as list
  set valuesNSArray to current application's NSMutableArray's array()
  repeat with oneNSURL in listOfNSURLs
    (valuesNSArray's addObject:(oneNSURL's resourceValuesForKeys:keysToRequest |error|:(missing value)))
  end repeat
  set theNSPredicate to current application's NSPredicate's predicateWithFormat_("%K == NO OR %K == YES", current application's NSURLIsDirectoryKey, current application's NSURLIsPackageKey)
  set valuesNSArray to valuesNSArray's filteredArrayUsingPredicate:theNSPredicate
  set theDescriptor to current application's NSSortDescriptor's sortDescriptorWithKey:(sortKey) ascending:true
  set theSortedNSArray to valuesNSArray's sortedArrayUsingDescriptors:{theDescriptor}
  set lastAddedFile to (theSortedNSArray's lastObject()'s valueForKey:(current application's NSURLPathKey)) as text
  
  return lastAddedFile
  
end lastAddedFileIn:sortedBy:

------------------------------------------------------------

-Chris

I meant to mention that Keyboard Maestro is well able to do this task.

It's not as convenient as a one-shot shell script would be if you were dealing with a modification-date or a creation-date.

You have to get a list of file paths and then loop through it testing the date-added file attribute.

The AppleScript is more convenient to use, because it does all the sorting and comparing for you – but a normal user could more easily construct a KM macro for this than the AppleScript-ObjectiveC.

When I have time I'll probably package up the AppleScript as a KM Action just for the exercise.

-Chris