Modify File Creation Date From File Naming Scheme NAME YYYYMMDD

Hey everyone, I’ve been painstakingly searching the these forums and trying to adjust some of the scripts posted to here to solve my problem. I have thousands of emails I’ve converted to PDF files in the file format NAME YYYYMMDD. I’m trying to match the creation date in DT to that of the date in the filename. I’ve found a few scripts, but they all seem to have the date at the beginning of the file name or the date format is YYYY-MM-DD and I can’t seem to figure out how to modify them to fit my use case. Anyone here in forums that could walk me through how (I’m trying to learn to fish) to modify one of the existing scripts? TIA!

If you post an existing script, we can advise on the modification

I’ve found these two;

1.) This first one I couldn’t get to work without modifying the filename to match the YYYY-MM-DD NAME format.

This script takes the date in the filename prefixed in the format YYYY-MM-DD and makes the creation date shown in DT as the same

tell application “DEVONthink Pro”
set selectedItems to selection

set the_date to current date
repeat with selectedItem in selectedItems
set the_name to name of selectedItem
set the_year to characters 1 thru 4 of the_name
set the_month to characters 6 thru 7 of the_name
set the_day to characters 9 thru 10 of the_name
set the year of the_date to the_year as string
set the month of the_date to the_month as string
set the day of the_date to the_day as string
set date of selectedItem to the_date
–uncomment the following two lines if you want to remove the date from the description
–set the the_name to (characters 12 thru -1 of the the_name) as string
–set name of selectedItem to the_name
end repeat
end tell

2.) This one I got to work, but for some reason it was making the creation date two days later than what was indicated in the file name.

– Set creation date based on name "Title YYYYMMDD”

tell application id “DNtp”
try
set theRecords to selection of viewer window 1
if theRecords = {} then error “Nothing selected.”

  repeat with thisRecord in theRecords
  	set theName to name of thisRecord
  	set theFileName to filename of thisRecord
  	
  	if theName = theFileName then
  		set revPath to reverse of characters in theFileName as string
  		set theSuffix to reverse of characters 1 thru ((offset of "." in revPath) - 1) in revPath as string
  		set theName to characters 1 thru -((length of theSuffix) + 2) in theName as string
  	end if
  	
  	set theCreationDate to creation date of thisRecord
  	
  	try
  		set day of theCreationDate to (characters -1 thru -2 in theName as string) as integer
  		set month of theCreationDate to (characters -3 thru -4 in theName as string) as integer
  		set year of theCreationDate to (characters -5 thru -6 in theName as string) as integer
  		
  		set creation date of thisRecord to theCreationDate
  	end try
  end repeat

on error error_message number error_number
if the error_number is not -128 then display alert “DEVONthink” message error_message as warning
end try
end tell

Here’s the original of this one unmodified.

– Set creation date based on name "# Title - YYYY-MM-DD-HHmmss”

tell application id “DNtp”
try
set theRecords to selection of viewer window 1
if theRecords = {} then error “Nothing selected.”

  repeat with thisRecord in theRecords
  	set theName to name of thisRecord
  	set theFileName to filename of thisRecord
  	
  	if theName = theFileName then
  		set revPath to reverse of characters in theFileName as string
  		set theSuffix to reverse of characters 1 thru ((offset of "." in revPath) - 1) in revPath as string
  		set theName to characters 1 thru -((length of theSuffix) + 2) in theName as string
  	end if
  	
  	set theCreationDate to creation date of thisRecord
  	
  	try
  		set seconds of theCreationDate to (characters -1 thru -2 in theName as string) as integer
  		set minutes of theCreationDate to (characters -3 thru -4 in theName as string) as integer
  		set hours of theCreationDate to (characters -5 thru -6 in theName as string) as integer
  		set day of theCreationDate to (characters -8 thru -9 in theName as string) as integer
  		set month of theCreationDate to (characters -11 thru -12 in theName as string) as integer
  		set year of theCreationDate to (characters -14 thru -17 in theName as string) as integer
  		
  		set creation date of thisRecord to theCreationDate
  	end try
  end repeat

on error error_message number error_number
if the error_number is not -128 then display alert “DEVONthink” message error_message as warning
end try
end tell

This should do it.

You can post properly formatted code if you wrap it in three backticks and a new line:

```
script

```

-- Set creation date based on name "NAME YYYYMMDD”

tell application id "DNtp"
	try
		set theRecords to selected records
		if theRecords = {} then error "Please select some records"
		show progress indicator "Renaming... " steps (count theRecords) as string with cancel button
		
		repeat with thisRecord in theRecords
			set thisName to name without extension of thisRecord
			step progress indicator thisName
			set theCreationDate to creation date of thisRecord
			try
				set seconds of theCreationDate to 0
				set minutes of theCreationDate to 0
				set hours of theCreationDate to 0
				set day of theCreationDate to ((characters -2 thru -1 in thisName) as string) as integer
				set month of theCreationDate to ((characters -4 thru -3 in thisName) as string) as integer
				set year of theCreationDate to ((characters -8 thru -5 in thisName) as string) as integer
				set creation date of thisRecord to theCreationDate
			end try
		end repeat
		
		hide progress indicator
		
	on error error_message number error_number
		hide progress indicator
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell
1 Like

This worked perfectly! Thank you! May I ask how you figured to modify the original script for it to work?

1 Like

I wrote the original script :slight_smile:

It’s quite easy once you understand how (characters X thru Y in theText) as string works. Then you just need to count the characters. The minus - indicates that we’re counting from the end of the string. Probably best to simply try it yourself.

You might want to take a look at Class Reference - text in the AppleScript Language Guide. It’s definitely not needed though. If you read it it’s probably best to ignore what you don’t understand and simply try the given examples.