Creation Date From yyyymmddhhmm Filename

Hi All,

I need some help please as have very little (to no) scripting knowledge.

I found this script for EagleFiler. Is there a way to adapt this script to work with KM in Finder on a selection of files?

Source: EagleFiler Script - date-from-yyyymmddhhmmss-filename

Summary: Sets the creation dates of the selected records based on their filenames.


This script looks for a date in the format 201402170744 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 creation dates (for sorting or searching purposes) based on the date of the document rather than the date of the scan.

Desired Outcome for a KM Macro

Change the creation date based on the date string in the filename of a selection in finder.

Example: YYYYMMDDHHMM-filename

EagleFiler Script

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

My Attempt at an AppleScript

tell application "Finder"
    set sel to selection
    repeat with theFile in selection
    set fileName to (name of theFile)
		set creation date of thisFile to my getDateString(fileName)
	end repeat
end tell

on getDateString(theFile)
	set ampm to "AM"
	set theYear to (characters 1 thru 4 of theFile) as string
	set theMonth to (characters 5 thru 6 of theFile) as string
	set theDay to (characters 7 thru 8 of theFile) as string
	set theHour to (characters 9 thru 10 of theFile) as string
	if theHour > 12 then
		set theHour to (theHour - 12)
		set ampm to "PM"
	end if
	set theMinutes to (characters 11 thru 12 of theFile) as string
	set dateString to (theDay & "/" & theMonth & "/" & theYear & " " & theHour & ":" & theMinutes & ampm)
	return (date dateString)
end getDateString

Would be grateful for the help with the AppleScript and compiling a KM macro :smile:

Thank you in advance!

This macro works for me:

Set File Creation Date from File Name.kmmacros (4.1 KB)

There weren't any seconds in your sample file name, so I added an If/Then action to account for that possibility, but aside from that, the regex provided in the EagleFiler AppleScript worked fine in KM once I removed the extraneous backslashes.


@gglick, thank you for your help with this! Much simpler than trying to write and execute a script :smile:

Appreciate the help! Works great! :+1:


Gabe, I think you got caught by the time zone issue again:
Just tested:

The time should have been 8:30 am

After this fix:

It is:

@JMichaelTX, just to confirm @gglick macro is working fine for me (with the correct time).

I am in the U.K.

I tried using the GMTOFFSET before, but kept running into errors.

Just a question, is GMTOFFEST used if you are located in the U.S.?

Appreciate the input :smile:

It is needed if you are located anywhere other than in the GMT time zone, but should work fine even there, returning zero.
See GMTOFFSET function.

I'm glad to hear that my macro is working for you fine as-is, but @JMichaelTX is right in that I should have included the GMTOFFSET function just to be sure. Apologies for the oversight!

1 Like