Date From yyyymmddhhmmss 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

Description

This script looks for a date in the format 20140217074436 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

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 dateFromComponents(my dateComponentsFromFilename(_record's basename))
        
set _record's creation date to _date
    
end repeat
end tell

on dateComponentsFromFilename(_filename)
    
-- yyyymmddhhmmss
    
set _regex to my (NSRegularExpression's regularExpressionWithPattern:"(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{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 _components to {}
    
repeat with _i from 1 to 6
        
set _range to (_textCheckingResult's rangeAtIndex:_i)
        
set _group to (_nsString's substringWithRange:_range)
        
copy _group as Unicode text to end of _components
    
end repeat
    
return _components
end dateComponentsFromFilename

on dateFromComponents(_components)
    
set _date to current date
    
set _date's year to item 1 of _components
    
set _date's month to item 2 of _components
    
set _date's day to item 3 of _components
    
set _hours to item 4 of _components
    
set _minutes to item 5 of _components
    
set _seconds to item 6 of _components
    
set _date's time to _hours * hours + _minutes * minutes + _seconds
    
return _date
end dateFromComponents