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: 2015-05-13

Description

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

Script

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 "\\d{4}(-\\d{2}){2}"
    
set _script to "python -c \"import sys, re; print re.search(r'" & _regex & "', sys.argv[1]).group(0)\" "
    
return do shell script _script & _filename's quoted form
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