Drag and Drop, file/folder name into text field

I’ve looked at the other posts regarding drag and drop, and yes, have read the Studio Manual many times, and looked at the sample Drag and Drop app, but I am still having problems getting the function I need. No sample I have seen fully addresses the (apparent) oddness of doing d n d on a text frame, especially with a file or folder name, which is probably the most common d n d a user would do.

I need to drag and drop a folder onto a text field (nsTextfield) and have the text field show the full path to the folder.
I already have a button that does “choose folder…” and then inserts the path to it.
I also have a test field that I can drag and drop text from Simple Text or Text Edit into, and it puts that text in the box. But I can’t make a file path or file name go in there.

Can someone please post a complete example of how I can accomplish this?
Thanks^6, Chris

Make sure your register the drag types for the object, like in an awake from nib handler. Connect the text field’s awake from nib handler to the script and then insert the code found below. Then use the code in the dropped handler to capture the paths and insert them into the text field.

I use the “item 1” of the paths, because the list of dropped items may be more than one file/folder long. This will return only the first item selected if multiples were dropped.

A quirky thing I noticed was that if the text field is the first responder (highlighted), it can only recieve a drop on the blue outline of the text field, and not on the text field itself. What’s that about? Maybe someone can shed some light on this.

on drop theObject drag info dragInfo
	set dataTypes to types of pasteboard of dragInfo
	if "file names" is in dataTypes then
		(* Get the list of files *)
		set preferred type of pasteboard of dragInfo to "file names"
		set thePaths to contents of pasteboard of dragInfo
		set preferred type of pasteboard of dragInfo to ""
		set theFile to item 1 of thePaths
		
		set contents of text field "textField" of window "window" to (theFile as string)
	end if
end drop

on awake from nib theObject
	tell theObject to register drag types {"file names"}
end awake from nib

Good luck…
j