Date From Filename

Summary: Sets the creation dates of the selected records based on their filenames.
Requires: EagleFiler
Install Location: ~/Library/Scripts/Applications/EagleFiler/
Last Modified: 2022-03-01


This script looks for a date in the format 2015-05-13 in each record’s filename and uses it to set the record’s creation date. This is useful, for example, if you have old scanned documents and want to set their dates (for sorting or searching purposes) based on the date of the document rather than the date of the scan.

Installation Instructions · Download in Compiled Format · Download in Text Format


use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

tell application "EagleFiler"
set _records to selected records of browser window 1
repeat with _record in _records
set _date to my dateFromString(my dateStringFromFilename(_record's basename))
set _record's creation date to _date
end repeat
end tell

on dateStringFromFilename(_filename)
set _regex to my (NSRegularExpression's regularExpressionWithPattern:"\\d{4}(-\\d{2}){2}" options:0 |error|:(missing value))
set _nsString to my (NSString's stringWithString:_filename)
set _textCheckingResult to _regex's firstMatchInString:_nsString options:0 range:{0, _nsString's |length|()}
set _dateRange to _textCheckingResult's range()
set _dateString to _nsString's substringWithRange:_dateRange
return _dateString as Unicode text
end dateStringFromFilename

on dateFromString(_string)
set _sampleDate to "2015-03-12"
set _dateCharacters to characters 1 thru (length of _sampleDate) of _string
set AppleScript's text item delimiters to ""
set _dateString to _dateCharacters as string
set AppleScript's text item delimiters to "-"
set {_year, _month, _day} to text items of _dateString
return my makeDate(_year as integer, _month as integer, _day as integer)
end dateFromString

on makeDate(_year, _month, _day)
set _date to current date
set _date's year to _year
set _date's month to _month
set _date's day to _day
set _date's time to 0
return _date
end makeDate