Setting creation date fails (Bug?)

Hi, I’m extracting the year and month a record was published from its name and want to set its creation date to those and the day to 1.

However I get unexpected results, as

  • the day is set to 3 (instead of 1)
  • the time changes (without changing this at all)

Here’s a screenshot

2019-11-05_00-44-17

What I want the selected record’s creation date to be is 01.05.12, 10:39. (The time is not important, just curious how it can change whithout telling it to do so.)

Here’s the script

tell application id "DNtp"
	try
		set windowClass to class of window 1
		if {viewer window, search window} contains windowClass then
			set currentRecord_s to selection of window 1
		else if windowClass = document window then
			set currentRecord_s to content record of window 1 as list
		end if
		
		repeat with thisRecord in currentRecord_s
			set theName to name of thisRecord
			set theCreationDate to creation date of thisRecord
			set theCreationDate's year to (characters -8 thru -7 in theName as string)
			set theCreationDate's month to (characters -6 thru -5 in theName as string)
			set theCreationDate's day to 1
			set creation date of thisRecord to theCreationDate
		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

What am I missing? Or is this a bug?

This seems to be a bug of date scripting:

set theName to "aben1205"

-- Fails
set theCreationDate to current date
set theCreationDate's year to (characters 5 thru 6 in theName as string)
set theCreationDate's month to (characters 7 thru 8 in theName as string)
set theCreationDate's day to 1

-- Works
set theCreationDate to date ("1/" & (characters 7 thru 8 in theName as string) & "/20" & (characters 5 thru 6 in theName as string))

I don’t think this qualifies as a bug. A 2-digit year is ambiguous, so AppleScript requires a 4-digit number for the year.

I strongly suggest using only the 1st method when modifying a date object. The other form, with separator included, may fail when the system’s settings are different.
So:

set theCreationDate’s year to (“20” & characters 5 thru 6 in theName as string)

The year isn’t the issue, the day isn’t set as expected in the first part of the example.

Yes, missed that - sorry.
But it does not error here (10.11). He doesn’t mention his OS version, or the actual day he tried the code first.
Anyway, there’s a bug in that date setting code…
When current date is 31 aug, and you are setting the month to feb, it rolls over into march, and the final result is 1 mar, not 1 feb. Always first set the day to 1, then change the other properties. Then set the required day last.

Thanks for the hint! However, at least today this shouldn’t matter.

Thanks, got it. But what I don’t understand is why you can use

inside a DEVONthink tell block. Over here it’s not possible to compile it when used this way so I used a handler. Is this necessary or am i missing something?

tell application id "DNtp"
	try
		set windowClass to class of window 1
		if {viewer window, search window} contains windowClass then
			set currentRecord_s to selection of window 1
		else if windowClass = document window then
			set currentRecord_s to content record of window 1 as list
		end if
		
		repeat with thisRecord in currentRecord_s
			set theName to name of thisRecord
			set theYear to "20" & characters -8 thru -7 in theName as string
			set theMonth to characters -6 thru -5 in theName as string
			set theDay to "1" as string
			set theDate to my makeDate(theDay, theMonth, theYear)
			set creation date of thisRecord to theDate
		end repeat

	end try
end tell

on makeDate(theDay, theMonth, theYear)
	set theDate to date (theDay & "." & theMonth & "." & theYear & ".")
end makeDate

My example was generic and not for DEVONthink, therefore a handler might indeed be necessary.