Image Pasting into Rich Text?

Does anyone know if there is a way in Apple script to paste an image into the current rich text document at the current cursor position?
I have a script to pull the currently selected text and process it, but I can’t find a way to put the resulting image back in.

This inserts an image before the first word of the selected text:


tell application "DEVONthink Pro"
	set thePath to "/Users/your_username/Pictures/sample.jpg"
	set theFile to (thePath as POSIX file) as alias
	make new text attachment with properties {file name:theFile} at before the first word of the first paragraph of selected text of think window 1
end tell

Works great!

BTW. If anyone has use for this. It lets you highlight an equation in a rich text document written in TeX format and converts it to a formatted PNG of the equation. It uses TeX and ImageMagick from Darwin Ports.


-- Gather the selected text
tell application "DEVONthink Pro"
	set this_text to the selected text of think window 1 as string
end tell

-- Our input and output files
set texfile to POSIX path of "/tmp/.tex2dt.tex"
set pngfile to POSIX path of "/tmp/.tex2dt.png"

-- Create the tex file
set tex to open for access POSIX file texfile with write permission
write "\\documentclass[12pt]{book}
\\pagestyle{empty}
\\usepackage{amssymb,amsmath}
\\begin{document}
\\Large\\(" to tex
write this_text to tex
write "\\)
\\end{document}" to tex
close access tex

-- Process the tex file with latex and ImageMagick
set cmd to "export PATH=/opt/local/bin:$PATH
latex -output-directory /tmp /tmp/.tex2dt.tex
convert /tmp/.tex2dt.dvi -trim /tmp/.tex2dt.png"
do shell script cmd

-- Paste it back in to the doc
tell application "DEVONthink Pro"
	set theFile to POSIX file pngfile as alias
	make new text attachment with properties {file name:theFile} at after the last word of the last paragraph of selected text of think window 1
end tell

do shell script "rm -f /tmp/.tex2dt.*"