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.
Description
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(re.search(r'" & _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
Thank you in advance!