Get the currently selected text

How to I get the currency selected text, I tried

set t to selected text
get text of t

But that doesn’t work (I’ve been away from AppleScript for a very long time)

tell application id "DNtp"
	set t to selected text of think window 1
	get text of t
end tell

You apparently need to define the window to get the selected text from.

1 Like

And you can check whether there’s actually text selected by joining selected text and and empty string "".

Without that you can end up with an undefined variable which of course would throw an error.

tell application id "DNtp"
	try
		try
			set theSelectedText to selected text of think window 1 & "" as string
		on error
			error "Please select some text"
		end try
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		return
	end try
end tell

If you want to get selected text of a specific window class:

  • Use viewer window for “main” windows
  • Use document window for, well, document windows

Thanks to both of you.

2 Likes