Help needed: script for extracting first line from record

Hi,

after having successfully imported my records from NoteTaker with the help of a humble macro processor that simulates key presses (see devon-technologies.com/phpBB … php?t=4907 for more details), I now need to edit the records.

More specifically, I want to extract the date I created a clipping from the records, and set the DTP record creation date to it. This will allow simple sorting by date, which is often needed for my analyses.

As I am a bloody amateur at AppleScript, any and all help in writing such a script will be much appreciated! Let me state what I have in mind.

The clipping date is in the first line of each record. Its format is something like this:

Clipping von heise.de/newsticker/meldung/print/46746 26.04.04 10:45:

So the general format is
Text Address Date Time:

I would need to extract the Date and Time (and ideally the Address), and put them into a script that loops through the records of the group in which I store these clippings (it contains 1625 records, thus automation is advisable :smiley: ).

Already a rough skeleton for such a script would greatly help me, as I have very little experience in AppleScript so far. Also, how I select text from a record is completely unclear to me. Once I know the general direction, I can experiment with the details.

Many thanks in advance for any help,

Andreas

tell application "DEVONthink Pro"
	set theSelection to the selection
	repeat with thisSelection in theSelection
		set AppleScript's text item delimiters to ASCII character 10
		set theText to the rich text of thisSelection
		set theFirstLine to text item 1 of theText
		set theRest to text items 2 through ((count of text items of theText) - 1) of theText
		set AppleScript's text item delimiters to " "
		set theURL to (text item ((count of text items of theFirstLine) - 3)) of theFirstLine -- the URL.
		set theDate to (text item ((count of text items of theFirstLine) - 2)) of theFirstLine -- the date of creation
		set theTime to (text item ((count of text items of theFirstLine) - 1)) of theFirstLine -- the time of creation
		set AppleScript's text item delimiters to ASCII character 10
		set theContent to theRest as string -- the content of the original document, minus the first line, in case you want it.
		set AppleScript's text item delimiters to "."
		set theDateItems to the text items of theDate
		set theDateDay to the first text item of theDateItems
		set theDateMonth to the second text item of theDateItems
		set theDateYear to the third text item of theDateItems
		set AppleScript's text item delimiters to ":"
		set theTimeItems to the text items of theTime
		set theTimeHour to the first text item of theTimeItems
		set theTimeMinute to the second text item of theTimeItems
		set theCreationDate to (theDateMonth & "/" & theDateDay & "/" & theDateYear & ", " & theTimeHour & ":" & theTimeMinute & ":00") as string
		set theNewDate to my subDate(theCreationDate)
		set the creation date of thisSelection to theNewDate
	end repeat
end tell

on subDate(theCreationDate)
	return (date theCreationDate) as date
end subDate

You will no doubt need to fudge with the “set theCreationDate” part because it does some things with American localization. It does Month/Day/Year, HH:MM:SS (24-hour clock). You probably need Day/Month/Year, HH:MM:SS. Or whatever.

Hope that helps. It works for me.

Just select all the documents you want to change and run the script.

You might possibly need to change “ASCII character 10” to “ASCII character 13” or something else, depending on localization.

You can learn a lot from this script, hopefully. The instructions I use cover at least 70% of the stuff I do in DEVONthink Pro.

Hi kalisphoenix,

many thanks for this – extremely helpful. I spent three hours yesterday evening and had already cooked up a structurally similar script, but got stuck in the date conversion, and in the problem of killing the trailing “:” of the time.

I only had to make a few alterations to your script, and added a line to set the URL, and my Powerbook has been grinding away for the last hour to change the database! There have been a few stops as not all entries comply completely with the given format, but that is only two handfuls in more than 2000 entries (there is a second group with 650 further entries), so it’s easiest to deal with those by hand.

Thanks again – and I hope that Google will guide others who look for a solution to this problem to this forum!

Andreas

Here is my amended version of the script:

tell application "DEVONthink Pro"
	activate
	set theSelection to the selection
	repeat with thisSelection in theSelection
		set AppleScript's text item delimiters to ASCII character 10
		set theText to the rich text of thisSelection
		set theFirstLine to text item 1 of theText
		set theRest to text items 2 through ((count of text items of theText) - 1) of theText
		set AppleScript's text item delimiters to " "
		set theURL to (text item (3)) of theFirstLine
		-- the URL. 
		set theDate to (text item ((count of text items of theFirstLine) - 1)) of theFirstLine
		-- the date of creation 
		set theTime to (text item (count of text items of theFirstLine)) of theFirstLine
		-- the time of creation 
		set AppleScript's text item delimiters to ASCII character 10
		set theContent to theRest as string
		-- the content of the original document, minus the first line, in case you want it. 
		set AppleScript's text item delimiters to "."
		set theDateItems to the text items of theDate
		set theDateDay to the first text item of theDateItems
		-- display dialog theDateDay
		set theDateMonth to the second text item of theDateItems
		-- display dialog theDateMonth
		set theDateYear to the third text item of theDateItems
		-- display dialog theDateYear
		set AppleScript's text item delimiters to ":"
		set theTimeItems to the text items of theTime
		set theTimeHour to the first text item of theTimeItems
		-- display dialog theTimeHour
		set theTimeMinute to the second text item of theTimeItems
		-- display dialog theTimeMinute
		set theCreationDate to (theDateDay & "." & theDateMonth & "." & theDateYear & ", " & theTimeHour & ":" & theTimeMinute & ":00") as string
		set theNewDate to my subDate(theCreationDate)
		set the creation date of thisSelection to theNewDate
		set the URL of thisSelection to theURL
	end repeat
end tell

on subDate(theCreationDate)
	return (date theCreationDate) as date
end subDate