How to reference text of TXT/RTF docs

OK, so i’ve the DT database open in “as tags” view and with a TXT document selected. Script Debugger 5’s Explorer indicates this AS tell for the selected doc’s text:

tell application "DEVONthink Pro"
	tell its think window "DEVONthink 2 — \"logs_setup_readme.txt\""
		text
	end tell
end tell

But if I set the script above to ‘log text’:

tell application "DEVONthink Pro"
	tell its think window "DEVONthink 2 — \"logs_setup_readme.txt\""
		set x to text
		log x
	end tell
end tell

…the event log simply shows the entry (text) of the ‘log x’ event rather than the text. Same if I try the ‘selected text’ property. Via Script Debugger’s Explorer I can see the ‘text’ property has the expected text and a value for selected text.

So, I’m falling that first hurdle! How do I target the text of a document to work with it?

Script Debugger frequently makes bad suggestions.

Try this:

tell application id "com.devon-technologies.thinkpro2"
	set mytext to selected text of think window 1
	display dialog mytext
	set selected text of think window 1 to "my new text"
end tell

Thanks. I think I see this issue. I ran this:

tell application id "com.devon-technologies.thinkpro2"
	set mytext to selected text of think window 1
	display dialog mytext
end tell

tell application id "com.devon-technologies.thinkpro2"
	tell think window 1
		set mytext to selected text
		display dialog mytext
	end tell
end tell

The second is a form of nested I’m more used to to avoid repeated long references. However, whilst the first works, the second only ever returns the mystery string “subs”. The two are syntactically equivalent so I’d guess there’s something amiss in the AS discretionary (the sdef stuff - beyond my knowledge!).

FWIW, using “tell application “DEVONthink Pro”” as the app tell works fine substituted in the above, albeit with the same duff result for a nested tell.

Anyway, thanks for setting me straight and I’ll use your form, setting a reference variable for the text object and working off that:

tell application id "com.devon-technologies.thinkpro2"
	set theDocText to text of think window 1
	set theDocSelectedText to selected text of think window 1
	set x to word 12 of text of theDocText
	display dialog x
end tell

I also found (and for other readers here) that selected text is a sibling property of (a doc’s) text and not a sub-property of text … as I’d first thought.

I’ll drop an email to LateNight Software too, flagging SD’s misreading of the DT dictionary. Many thanks.

Why set


set theDocSelectedText to selected text of think window 1

if you don’t use it?

Sorry, I did - but in another test which I’d clipped to shorten the code posted. The point I was making was that the following doesn’t work. Stating the issue more clearly.

tell application id "com.devon-technologies.thinkpro2"
   set theDocText to text of think window 1
   -- The next line fails as 'selected text' is not a property of 'text'
   set x to selected text theDocText
   display dialog x
end tell

In fairness, a reading of the DT Dictionary in SD or in AppleScript Editor would show this up (to those who understand the object structure!).

The fact you can’t directly address the selected text property within a think window tell block, given that the DT dictionary defines the former as a property of the latter looks to be a glitch in how DT implements its publicised AppleScript object hierarchy. That said, your original reply’s example neatly avoids the issue so whilst not easily guessed [sic], it does give a workaround! Anyway, I’m not throwing rocks, just flagging up inconsistency between documentation and actual function. :smiley:

(edit: typo)