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?
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?
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.
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.
(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?
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
tellapplicationid “DNtp”
repeatwith thisRecord in (selection aslist )
set today to ( current date )
set recordName to (name of thisRecord)
set titleDatestring to ( characters 1 thru 10 of recordName) astext
set myYear to ( characters 1 thru 4 of titleDatestring) astext
set myMonth to ( characters 6 thru 7 of titleDatestring) astext
set myDay to ( characters 9 thru 10 of titleDatestring) astext
set day of today to myDay
setmonthof 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
endrepeat
endtell
I am sure that it is not elegant, but it works .
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.