I have imported a large number of old mails (in text format, not mail format) from Tinderbox to DTP. Now I need to set the creation date of each mail record in DTP. It is stored in the second line of the mails, and I try with the following script that successfully sets the title:
tell application "DEVONthink Pro"
activate
set selectedItems to the selection
repeat with z from 1 to count selectedItems
try
set itemText to rich text of item z of selectedItems
set name of item z of selectedItems to first paragraph of itemText
set datoen to second paragraph of itemText
set creation date of item z of selectedItems to datoen
end try
end repeat
end tell
It seems to me that I need to convert the text datoen to date format. How do I do that - or is there another way?
It’s interesting you should mention this, Tellef, and I’m disappointed that nobody else answered you even though this was posted more than 2 months ago. As it happens, I’m also in the process of importing content out of Tinderbox and other applications into DevonThink and I also needed to set the creation/modification dates of each imported note.
In my case, the date (but not the time, which I never cared about) happened to be included in the title of each note. So, for example, I might have a note named “051013 Making Magic with Merlin”, where “05” is the year (2005), “10” is the month (October), and “13” is the day of the month.
I’m not an AppleScript guru by any stretch of the imagination but I thought I should be able to cobble together something to grab the “date representation” from the title and convert it into a Date object to be used to set the creation and modification dates of the selected DevonThink note. I started off by looking at the logic of the provided Set name to title of Web page and Creation > Modification Date scripts. The latter I particularly used as an outline for structuring my resultant script.
I was able to write a test script with a subroutine that, when given a “date string”, would return a proper date object. It looked like this:
set this_title to "011207 I am the yellow banana"
set title_date_part to first word of this_title
set the_date to make_date(title_date_part)
on make_date(date_string)
-- the year (last two digits only) is the first two characters in the string. We need to add the correct century to it
set our_year to characters 1 thru 2 of date_string as string
if our_year as number > 90 then
set our_year to "19" & our_year
else
set our_year to "20" & our_year
end if
set our_month to characters 3 thru 4 of date_string as string
set our_day to characters 5 thru 6 of date_string as string
set our_date_string to our_month & "/" & our_day & "/" & our_year
set this_date to date our_date_string
return this_date
end make_date
When run, exactly as above, in the Script Editor, this would give a result of:
However, when I dropped this into my modified DevonThink script, it wouldn’t compile. I always would get an error in the line “set this_date to date our_date_string”. It didn’t want anything after the word “date”. I eventually realized that this is because the context (or scope) of the code was DevonThink and DevonThink has redefined “date” as a property of an item, so it doesn’t behave the way it does elsewhere. I needed to change the scope somehow, which is what led to having a subroutine but that wasn’t enough. Several hours of research later (thank goodness for institutional Safari O’Reilly online bookshelf subscriptions!), I figured out how to do it. Here’s my complete script:
-- Set creation and modifications dates based on first part of note title
-- Based on Modification > Creation Date.
-- Originally created by Christian Grunenberg on Fri Jul 22 2005.
-- This version: Michelle A. Hoyle
-- October 14, 2005
on make_date(date_string)
-- date_string is expected to be yymmdd here
-- the year (last two digits only) is the first two characters in the string. We need to add the correct century to it
set our_year to characters 1 thru 2 of date_string as string
if our_year as number > 90 then
set our_year to "19" & our_year
else
set our_year to "20" & our_year
end if
-- set month and day pieces so we can rebuild the string
set our_month to characters 3 thru 4 of date_string as string
set our_day to characters 5 thru 6 of date_string as string
-- build the string
set our_date_string to our_month & "/" & our_day & "/" & our_year
-- convert it to an actual Date object
set final_date to date our_date_string
return final_date
end make_date
tell application "DEVONthink Pro"
activate
try
set this_selection to the selection
if this_selection is {} then error "Please select some contents."
repeat with this_record in this_selection
-- get the title
set this_title to the name of this_record
-- get the date string from first word of title
set title_date_part to first word of this_title
-- make that into a date
-- DevonThink has a "date" property which interferes
-- with the standard date stuff so need to use "my"
-- syntax to change the scope for that
set this_date to my make_date(title_date_part)
set the creation date of this_record to this_date
set the modification date of this_record to this_date
end repeat
on error error_message number error_number
if the error_number is not -128 then
try
display alert "DEVONthink Pro" message error_message as warning
on error number error_number
if error_number is -1708 then display dialog error_message buttons {"OK"} default button 1
end try
end if
end try
end tell
The key was the “my” bit which forced the contextual script to be moved back outside of DevonThink and therefore behave as normal Date methods do in AppleScript. You probably need to do something similar in your own context. Maybe try:
tell application "DEVONthink Pro"
activate
set selectedItems to the selection
repeat with z from 1 to count selectedItems
try
set itemText to rich text of item z of selectedItems
set name of item z of selectedItems to first paragraph of itemText
set datoen_string to second paragraph of itemText
set datoen to my make_date(datoen_string)
set creation date of item z of selectedItems to datoen
end try
end repeat
end tell
on make_date(date_string)
-- you may have to do other stuff in here.
-- convert it to an actual Date object
set final_date to date date_string
return final_date
end make_date
PS: This whole process, as unused to AppleScript programming as I am, took me about 3 hours from start to finish. The sample scripts provided with DevonThink Pro (1.01) were very helpful, as was examining the AppleScript Dictionary for DevonThink, and consulting AppleScript: The Definitive Guide.