change ... into true ellipsis

I have just noticed that DT Pro doesn’t recognize ellipses when they are dragged-and-dropped or copied-and-pasted from PDFs (fun fact: not true when they are copied from other programs like Mellel and Scrivener RTFs). This is a problem for me, so I’d like to develop an Applescript that searches for “…” (successions of three periods) and replaces it with “…” (a true ellipsis). The catch, and the part I can’t figure out, it that all of the files I want to run the search and replace in are rich text files. I have Satimage installed, so if those “find” and “change” commands work for you, have at it.

The relevant portion of the script I tried —and which was a complete shot in the dark— was:


set allRichText to rich text of this_item
set rich text of this_item to change "\\.{3}" into "…" in allRichText

but, while it did change the … into a true ellipsis, it also annihilated my formatting, and changed the font from Adobe Garamond Pro to Helvetica (who knows why). Can anyone help?

Many thanks in advance!

Rich text scripting is, due to limitations of Mac OS X, limited to visible views/windows. Therefore you have to display the record in a window and access the “text” property of a “think window”. For examples, see ~/Library/Application Support/DEVONthink Pro 2/Scripts/Format.

Too bad, though I confess I suspected something like this would be the case. Thanks for the tips!

Edit: I thought I’d be able to do this based on what you told me, but I still can’t seem to figure it out. Bold and italic text keeps getting wiped out. Is there a way to prevent this from happening? My current script reads:


tell application id "com.devon-technologies.thinkpro2"
	try
		if not (exists think window 1) then error "No window is open."
		if not (exists content record) then error "Please open a document."
		set theText to selected text of think window 1
		if theText is missing value or theText is "" then error "No text is selected."
		set theText to change "\\.{3}" into "…" in theText with regexp
		set selected text of think window 1 to theText
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell

Alternatively, is there a way to search my database for a series of 3 consecutive periods? I can be vigilant about pasting ellipses in the future, but this script was supposed to help me automate the process of converting old ellipses into what I wanted them to be in the first place. It seems as though DT doesn’t like searching for punctuation, and tells me I have no “…” strings anywhere in the database.

This script should work without any add-ons:


tell application "DEVONthink Pro"
	tell selected text of think window 1
		repeat with theAttributeRun in attribute runs
			set theString to (theAttributeRun as string)
			if theString contains "..." then
				set theString to my replaceText(theString, "...", "…")
				set text of theAttributeRun to theString
			end if
		end repeat
	end tell
end tell

on replaceText(theString, find, replace)
	if theString contains find then
		local od
		set od to text item delimiters of AppleScript
		set text item delimiters of AppleScript to find
		set theString to text items of theString
		
		set text item delimiters of AppleScript to replace
		set theString to "" & theString
		set text item delimiters of AppleScript to od
	end if
	return theString
end replaceText

I was experimenting with your example script for guidance with another problem and found this exact script (select all) fails. It ends with an “invalid index number -1719” after a single successful replacement in the selected text.