Multiple drag and drop inputs

Hi all

I have a small app that has a text box for input and one for output. I was wanting to make it possible to drag a file or folder onto the boxes and have the folder paths show up in the box. I managed this fine with the first box, but can’t get it to work with the second. Whenever I get one box to work the other stops working. There is actually a third similar text box elsewhere in the app, and that one works great too - except when I try to any of the others to work. Am I making any sense?

Basically, my question is this: Is it possible to have more than one drag and drop enabled text box on the same window, in the same applescript? Is it even possible to have more than one in the same application?

Thanks in advance!

Edd

The first, often-overlooked thing is making sure to register what kind of items can be dropped on an object. This is often done in the ‘awake from nib’ handler, attached to the object you wish to receive drops on…

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

Then, connect the object (in this case your text field) to the ‘on drop’ handler, and use the following code…

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 ""

		(* Get the first item dropped *)
		set theFile to item 1 of thePaths

		(* Display the file path in the dropped field *)
		set contents of theObject to (theFile as string)

		(* Display the file path in another field *)
		set contents of text field "anotherTextField" of window "window" to (theFile as string)
	end if
end drop

This code is modular, and should work if you have 2 or 20 text felds, as long as they are all properly connected to the two handlers.

j

Hi! Thanks for the quick response. I have done the things you suggest and when I try to connect only one text field it all works great. However, when I try to connect another text field it all goes wrong. I must be missing something. It seems to have something to do with text fields being on different tabs. I’ll attach an example of the problem. I’m just guessnig what to do to fix it, I can’t find anything online that explains how this works.

http://ecastle.spymac.net/Example.zip

Thanks fror your help!

Edd

The reason you’re having problems is definitely tab-related. When the text field calls the awake from nib handler, the tab containing it hasn’t yet been instantiated, so it doesn’t know where to send the register command. If you noticed, it only regisers the fields that are contained in the current tab view item. If you set the ‘tabb’ to be visible when the app starts up, the two fields in ‘taba’ do not get registered but ‘text3’ in ‘tabb’ does. So you need to get rid of the text fields’ connections to the awake from nib handler and instead connect the tab view items which contain the text fields to the awake from nib handler. Then, unfortunately, it seems you must implicitly call each text item by name to get it to register. Hopefully you don’t intend to have 20 droppable fields in your final version. :wink:

This worked for me…

on awake from nib theObject
	if name of theObject is "taba" then
		tell text field "text1" of tab view item "taba" of tab view "tabs" of window "window"
			register drag types {"file names"}
		end tell
		tell text field "text2" of tab view item "taba" of tab view "tabs" of window "window"
			register drag types {"file names"}
		end tell
	else if name of theObject is "tabb" then
		tell text field "text3" of tab view item "tabb" of tab view "tabs" of window "window"
			register drag types {"file names"}
		end tell
	end if
end awake from nib

If you did decide to have lots of fields be droppable, I’d set up a list containing all of the names for your fields, and then use a repeat loop to cycle through them and register each.

Cheers,
j

That worked a treat, thanks! It was also a good learning experience :slight_smile:

Edd