Prepending note title to note text

After my earlier success at figuring out how to parse a note title and modify the note’s creation and modifications properties, I thought I’d move on to my second task: inserting the note title at the beginning of a styled note.

Some background:
I’m doing this because I imported a large number of notes from another application where, when the notes were printed, the page contained the title of the note on the printout. DevonThink doesn’t seem to include this, so I have no idea what the title of a printed note is or when it was created.

To work around that, my cunning plan was just to insert the note’s title (name property) before the first paragraph of the note content. Well… I had some mixed success. I can do this with the following code:


-- Add the title to the beginning of the note
-- This version: Michelle A. Hoyle
-- October 14, 2005


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
			
			set this_title to "Title: " & the name of this_record & return
			set theData to the rich text of this_record
			set theData to this_title & theData
			set the rich text of this_record to theData
		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

However, there’s a small catch: the original formatting of the styled note is lost. Everything in the note becomes Helvetica 12 pt. I have no idea why it becomes Helvetica 12 pt as that doesn’t seem to be a default preference setting, but that’s definitely the case.

I don’t want to change the style characteristics of the entire document. I just want to prepend the title at the beginning, preferably using the style of the first paragraph of the content. Can someone give me a pointer in the right direction? Thanks!

I’ve been trying to do the same thing, with similar results. As far as I can tell storing the rich text in a variable strips it down to plain text. Unless I’m mistaken, the only way a script can retain formatting is to use cut and paste (as in the copy selection to incoming script(s))
Bummer.

Drat! I thought it was just me. I haven’t done anything significant with AppleScript since I integrated a StarNine web server with a FileMaker 3.x database on a Quadra to allow people to search information on published journal articles and see list of articles in upcoming issues.

I actually went looking to see if I could find the “Services” stuff, because there is an Append to Rich Text in there, as you noted, and I wondered if that was AppleScript. I couldn’t find it anywhere. So would we be able to do this somehow by:

  • Repeat over a selection of records
    [list]
    [*]With the current record, open it
  • Select all the text in it
  • Copy that text to the clipboard
  • Replace all the text in the current record with the title information
  • Paste the text back from the clipboard to the bottom of the document
    [/*:m][/list:u]

I thought there wasn’t a way to place the cursor at a specific place in the document via AppleScript, though? (I think I read that in another post) If that’s so, how does the Services append to Rich Text work in the first place?

there’s the rub: i don’t think its possible to access part of the document. If one could, I could think of all kinds of interesting ways to use something like “get paragraph x of document y”, but as far as i can tell (admittedly that’s not very far) this is not possiple.
As for:

Veeeeerry interesting question.
Bill? care to take a shot?

I just saw something interesting included in the new DevonThink Pro 1.0.2 release: a script called “merge.scpt”. It’s in the Example Scripts. It looks like:


-- Merge Selected Items.
-- Created by Christian Grunenberg on Fr Apr 23 2004.
-- Copyright (c) 2004-2005. All rights reserved.

using terms from application "DEVONthink Pro"
	tell application "DEVONthink Pro"
		activate
		try
			set this_selection to the selection
			if (count of this_selection) < 2 then error "Please select multiple items."
			set this_name to ""
			set this_result to "" as styled text
			set these_feeds to (return & return) as styled text
			repeat with this_item in this_selection
				if this_name is "" then
					set this_name to name of this_item
				else
					set this_name to this_name & ", " & name of this_item
				end if
				set this_result to this_result & (rich text of this_item) & these_feeds
			end repeat
			set theRecord to create record with {name:this_name, type:rtf}
			set the rich text of theRecord to this_result
		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
end using terms from

Maybe our mistake was in trying to use “rich text” instead of “styled text”? Of course, it looks like this creates a brand new record (as most examples do), but it looks worthwhile to experiment with. Alas, I have no time to pursue it at the moment; there’s marking to be done.

OK… I couldn’t resist: I tried it. Unfortunately, it gives us the same results as we already had: the original formatting is lost. What’s interesting, however, is there’s also a “Merge” option in DevonThink’s Data menu and that does not lose the formatting.

I must say, that gave me a good belly laugh of recognition. I too have tried the merge approach without success. I sincerely think we’ll have to wait for a higher power (ahem…Bill? Christian?) to answer this one. I may be a “phpbb GOD” now, but I’m not even sure what that means.

Get back to marking. As for me I’m back to writing … or maybe I’ll have a muffin. Hmm where’s that muffin pan…

Cheers
Eiron.

Scripting of rich/styled texts of all Cocoa applications is quite limited (up to Mac OS X 10.4.x) , e.g. it’s possible to transfer rich text from one visible document to another visible one but it’s not possible to transfer rich text from one invisible record to another or to create new rich texts using existing ones. It’s also impossible to transfer rich text from one application (e.g. Safari or Mail) to another one (e.g. DEVONthink Pro or TextEdit). That’s a shortcoming (or should I say bug?) of Cocoa/Mac OS X.