Set foo to selection of text field

Hello,

Is it possible to retrieve what text is selected (highlighted) in a text field?

I have a text field where the user can enter some text (file name). I’d then like for them to be able to highlight part of the text and click a button, which in the background would set a variable to whatever text was highlighted.

Possible? I’ve tried:

set foo to selection of text field "textField" of window "window1"

but I get an error saying it can’t make it into a reference.

Thanks!
Dan

A text field (NSTextField) is a control, not a text object. For all intensive purposes, it is essentially a button with an editable title field. The nstextfield class does not support any of the features of the NSText class, and so does not have the methods needed to find the current selection. If you want to be able to manage text selections, you’ll have to use a text view instead.

j