Hi All,
I need some help please as have very little (to no) scripting knowledge.
I found this script for EagleFiler (but as I am a DT Pro Office user) is there a way to adapt this script to work with DT?
I’d be grateful for the help!
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 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.
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
1 Like
Replacing the first tell/end tell block of the script with this one should be sufficient:
tell application id "DNtp"
set theSelection to the selection
repeat with theRecord in theSelection
set theDate to my dateFromComponents(my dateComponentsFromFilename(name of theRecord))
set theRecord's creation date to theDate
end repeat
end tell
Thank you for such a speedy response. I’m still having issues. Replacing the first tell/end tell block of the script not working.
My filename follows this format:
201705122306 - title of note
Amended Script (YYYYMMDDHHMMSS)
Didn’t work on a note with following formats: 20170512230612 - title of note / 20170512230612-title of note*
tell application id "DNtp"
set theSelection to the selection
repeat with theRecord in theSelection
set theDate to my dateFromComponents(my dateComponentsFromFilename(name of theRecord))
set theRecord's creation date to theDate
end repeat
end tell
on dateComponentsFromFilename(_filename)
-- yyyymmddhhmm
set _regex to "(\\d{4})(\\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
Amended Script (YYYYMMDDHHMM)
Didn’t work on a note with following formats: 201705122306 - title of note / 201705122306-title of note*
tell application id "DNtp"
set theSelection to the selection
repeat with theRecord in theSelection
set theDate to my dateFromComponents(my dateComponentsFromFilename(name of theRecord))
set theRecord's creation date to theDate
end repeat
end tell
on dateComponentsFromFilename(_filename)
-- yyyymmddhhmm
set _regex to "(\\d{4})(\\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 _date's time to _hours * hours + _minutes * minutes
return _date
end dateFromComponents
Note Scripts are saved to DT’s scripts folder.
Thank you again for your help in advance!
I tried this successfully once using the original dateComponentsFromFilename/dateFromComponents functions. Now it doesn’t work any longer. Weird. But I don’t know how reliable these parts of the script actually are.
If the filenames are indeed that conforming, a much simpler pure Applescript approach can be taken…
tell application id "DNtp"
repeat with thisRecord in (selection as list)
set recordName to (name of thisRecord)
set creation date of thisRecord to my getDateString(recordName)
end repeat
end tell
on getDateString(recordName)
set ampm to "AM"
set theYear to (characters 1 thru 4 of recordName) as string
set theMonth to (characters 5 thru 6 of recordName) as string
set theDay to (characters 7 thru 8 of recordName) as string
set theHour to (characters 9 thru 10 of recordName) 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 recordName) as string
set dateString to (theMonth & "/" & theDay & "/" & theYear & " " & theHour & ":" & theMinutes & ampm)
return (date dateString)
end getDateString
Note: This may fail in other locales, so you’ll need to test in your environment to be certain.
the variable _regex got truncated (by the board software?). In the original script it has 6 pieces. 1st copy has 5, 2nd copy has 4.
You are a genius! Thank you! Works perfectly!
1 Like
One more thing please
Just something that I’ve noticed that I need a little more help with; the script works perfectly, thank you! However it renders in U.S format (my dates sort YYYYMMDD - 202004271428) so U.K would be 27/04/2020 instead of U.S. 04/27/2020).
I doubt it is the script causing this, rather the default DT date format?
Is there a DT system setting I can change the format of the date to U.K standard instead of U.S standard?
Example:
** Note the month and day have switched positions under the creation date column**
info panel shows creation date also sorted/rendered differently to modification date?

@cgrunenberg would have to respond on this.
Thank you for all your effort. I will await further response.
UPDATE - Issue Solved
Thought I’d double-check the script (just in case) and yup, found the issue with the set dateString, so just moved theDay and theMonth around. Works now 
Amended Script for U.K users
set dateString to (theDay & “/” & theMonth & “/” & theYear & " " & theHour & “:” & theMinutes & ampm)
tell application id "DNtp"
repeat with thisRecord in (selection as list)
set recordName to (name of thisRecord)
set creation date of thisRecord to my getDateString(recordName)
end repeat
end tell
on getDateString(recordName)
set ampm to "AM"
set theYear to (characters 1 thru 4 of recordName) as string
set theMonth to (characters 5 thru 6 of recordName) as string
set theDay to (characters 7 thru 8 of recordName) as string
set theHour to (characters 9 thru 10 of recordName) 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 recordName) as string
set dateString to (theDay & "/" & theMonth & "/" & theYear & " " & theHour & ":" & theMinutes & ampm)
return (date dateString)
end getDateString
Ahh… yes, I thought it may need massaging based on locale.