Where is my glitch/ Set creation date to record prefix?

Dear,

my files in the Inbox are named YYYY-MM-DD. So I wanted to setup an apple script to change the creation date to DD.MM.YYYY.

After some search I wrote together the following one:

repeat with thisRecord in (selection as list)
  set pDate to current date
  
  set recordName to (name of thisRecord)
  set titleDatestring to (characters 1 thru 10 of recordName) as text
  set myYear to (characters 1 thru 4 of titleDatestring) as text
  set myMonth to (characters 6 thru 7 of titleDatestring) as text
  set myDay to (characters 9 thru 10 of titleDatestring) as text
  
  set myDay to day of pDate
  set myMonth to month of pDate
  set myYear to year of pDate
  set time of pDate to "0"
  
  set the creation date of thisRecord to pDate

end repeat
end tell

Even though I (at least I believe I did replaced the pDate (which was introced with the current date) with the “right” date, day, month and year are ignored. Only the time is correctly adjusted to 0.
What am I doing wrong?

Thank you for your support,
best regards,

Connor

You can’t set the creation date to a specific format like that.

Also, why are you getting today’s date. i.e., current date? That would be setting the creation date to today’s date, not one derived from the record’s name.

I used to do so in order to have a predefined variable in date format.
Sorry for asking, but what would be the right syntax to introduce the variable pDate in a date formate that could be manipulated later on?

In addition to that, when executing the script, I receive the following output

tell application "DEVONthink 3"
	get selection
		--> {content id 408217 of database id 2}
	current date
		--> error number -10004
end tell
tell current application
	current date
		--> date "Freitag, 17. Januar 2020 um 08:51:01"
end tell
tell application "DEVONthink 3"
	get name of content id 408217 of database id 2
		--> "2019-10-22 Allianz Anschreiben Herzgesundheit"
	set creation date of content id 408217 of database id 2 to date "Freitag, 17. Januar 2020 um 00:00:00"
end tell

Where does the error come from? And why does it seemingly not have any effect on the execution?

The error -10004 is harmless and is just referring to a command that isn’t explicitly run by the calling application.

Thank you for the feedback - I will simply ignore it.
Do you have any recommendation on the issue with the variable?

As I mentioned, you can’t specify a file date (like creation or modification dates) in a specific format, like DD-MM-YYYY. They are only specified in a fashion like you’ve shown here:
date "Freitag, 17. Januar 2020 um 00:00:00"

Also, I am not a big fan of changing creation dates as those are dates specific to an individual file. If there is a date that relates to the content of a file, what I call a relative date, that can be added as custom metadata. That custom metadata - specified as Single-line Text - can be formatted as you wish.

This can be viewed in the Tools > Inspectors > Info > Custom as we as added as a column in the List view of the item listing.

Obviously sorting strings may not give you the desired results, depending on what you’re doing.

You can also specify the custom metadata as a date, which is sortable but displays in Medum format (see System Preferences > Language & Region > Advanced > Dates) in the item list and in a Short format (though not exactly) in the Custom inspector.

image

image
(Note the date in the Inspector is using the US locale formatting of MM-DD-YYYY, regardless of the format I used in the script below.)

Here is a simple code snippet that works with both data types…

tell application id "DNtp"
	set r to "14.02.2019"
	repeat with thisRecord in (selection as list)
		add custom meta data r for "filedate" to thisRecord
	end repeat
end tell

Thank you for your feedback and sharing your thoughts.
My idea behind manipulating the creation date was to be independent from DT, as the creation date is also set to the file attributes in the filesystem.

Apart from that, I added another custom metadata field called “Belegnummer” to test your approach.
Finally, it gives me the same issues as with the creation date, as I need to define a variable in date format in beforehand. Seemingly, using the definition of “current date” is not working, as this amended script

tell application id "DNtp"
repeat with thisRecord in (selection as list )
set cDate to current date
set recordName to (name of thisRecord)
set titleDatestring to ( characters 1 thru 10 of recordName) as text
set myYear to ( characters 1 thru 4 of titleDatestring) as text
set myMonth to ( characters 6 thru 7 of titleDatestring) as text
set myDay to ( characters 9 thru 10 of titleDatestring) as text
set myDay to day of cDate
set myMonth to month of cDate
set myYear to year of cDate
add custom meta data cDate for "belegdatum" to thisRecord
end repeat
end tell

is only returning the current date.
What am I missing here to refine the cDate variable as date format using my defined variables myDay/ myMonth/ myYear?

Thank you for pusihng me in the right direction.

set myDay to day of cDate
set myMonth to month of cDate
set myYear to year of cDate
  • You are referencing the current date with these variables, so it’s only returning info for today.
  • You are replacing the values of the same variables culled from the record name with these values.
  • They are also not being used in any way after assigning them.
add custom meta data cDate for "belegdatum" to thisRecord

cDate is the current date so that’s why it’s using that value.
You need to string together the string pieces you set as variables.

add custom meta data (myDay & "-" & myMonth & "-" & myYear as string) for "belegdatum" to thisRecord

However, it is unclear why are you getting values from the name and from the current date.
Which are you intending to use?

I finally got a working solution. Once again to describe the scenario.
What is intended is to take the first 10 digits from the record name and to use the scheme YYYY-MM-DD as the information for the document date (which is now a custom metadata field).

The idea with the current date was to use it to define a variable in date format. That’s all.
What was finally working for me is

tell application id “DNtp”

repeat with thisRecord in (selection as list )

set today to ( current date )

set recordName to (name of thisRecord)

set titleDatestring to ( characters 1 thru 10 of recordName) as text

set myYear to ( characters 1 thru 4 of titleDatestring) as text

set myMonth to ( characters 6 thru 7 of titleDatestring) as text

set myDay to ( characters 9 thru 10 of titleDatestring) as text

set day of today to myDay

set month of today to myMonth

set year of today to myYear

copy today to cDate

set time of cDate to 0

add custom meta data cDate for “belegdatum” to thisRecord

end repeat

end tell

I am sure that it is not elegant, but it works :smiley:.

Thank you for getting me on the track!

P.S.: I just saw your last post. Thank you for taking the time. I will also try it to learn a little more on scripting.

You’re welcome.

It’s still unclear why you are getting the myDay, myYear, etc. from the record name and from the current date then not using them.

OK. Now I got your point, but I am unsure on whether I really understand.
From my poor knowledge, I am defining a date variable

set today to ( current date )

Then extracting the information for year, month and day with these lines from the record name

set titleDatestring to ( characters 1 thru 10 of recordName) as text

set myYear to ( characters 1 thru 4 of titleDatestring) as text

set myMonth to ( characters 6 thru 7 of titleDatestring) as text

set myDay to ( characters 9 thru 10 of titleDatestring) as text

And finally, I am replacing the current date in today with the extracted information using these lines

set day of today to myDay

set month of today to myMonth

set year of today to myYear

So where do you see that the myXX-variables are used twice?

What data type are you using for the custom attribute belegdatum ?

It’s a datefield.

Yes, it could be done that way then.

:partying_face:
Got it - thank you for your patience!

You’re welcome :slight_smile: