Page 1 of 1

Rename file with least recent date found in file

PostPosted: Sat Mar 26, 2016 10:30 pm
by tanglemac
I'm scanning a document that has multiple dates in the same format. I have a rule that looks for the nth occurrence of the date and then renames the file using that date, but I've found the OCR results are not always consistent. Sometimes the date I want is the first one found in a search, but sometimes it is the second or possibly even the third occurrence. The date I want to name the file with is always the least recent date in the file. Is there a way to create a rule to find the first 3 occurrences (or even all of them) in a certain format, and then name the file with the least recent date found?

Thanks for any suggestions!

Re: Rename file with least recent date found in file

PostPosted: Mon Mar 28, 2016 12:01 pm
by Mr_Noodle
That would be very tricky, requiring multiple rules to compare all the different combinations. You would have one rule like "date1 is before date2" and "date1 is before date3" then <do something with date1>) and then likewise have separate rules for date2 and date3.

Alternatively, you can write a script.

Re: Rename file with least recent date found in file

PostPosted: Tue Mar 29, 2016 10:12 pm
by tanglemac
Thanks for suggesting the script idea. I was able to get it to work by matching each of the first three dates into separate attributes and then passing those into this embedded applescript in the action section:

Code: Select all
set date1 to item 1 of inputAttributes
set date2 to item 2 of inputAttributes
set date3 to item 3 of inputAttributes
set oldestDate to date1
if date2 < oldestDate then
   set oldestDate to date2
end if
if date3 < oldestDate then
   set oldestDate to date3
end if
return {hazelOutputAttributes:{oldestDate}}

I then was able to name the file using the returned date value.