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.
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:
------------------------------------------------------------
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.