Dragging and Dropping

Hi all! I’ve been messing around with some code this afternoon, looking at examples, but not quite finding anything to push me hard enough in the right direction.

I have an interface where the user chooses a source folder and a destination folder. The user may do this through a button click or by dragging the folder to a text box. I set up the drag and drop script to do this and it appears to half work. When the script runs, I can drag something into the first box and everything works, but I can’t drag anything into the second box (unless I first click on the text box, then I can drag to it.)

What do I have to do so that I can drag to either box without having to click there first?

A second question, I also have. Above each of the text boxes is an image box. The images are set and stationary for this part of the interface, but is it possible to allow a user to drag a folder to the image box and then have the path to that folder appear in the text box? I am assuming it is possible, what extra code would I have to add to make that work? (I attempted to duplicate the code I was using with the text boxes, but it doesn’t seem to work.)

Here is the code I currently am using, if it is needed for any troubleshooting.
Thanks much.

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

on drop theObject drag info dragInfo
	set dataTypes to types of pasteboard of dragInfo
	if "file names" is in dataTypes then
		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 ""
		
		(* Loop through all items dropped *)
		repeat with thePath in thePaths
			set theFile to (POSIX file thePath as string) as file specification
		end repeat
		
		(* Work with only the first item dropped *)
		set thePath to item 1 of thePaths
		set theFile to (POSIX file thePath as string) as file specification
		if name of theObject is equal to "sourceFolderText" then
			set content of text field "sourceFolderText" of window "FolderSelectionWindow" to theFile
			set theSourceFolder to theFile
		else if name of theObject is equal to "destinationFolderText" then
			set content of text field "destinationFolderText" of window "FolderSelectionWindow" to theFile
			set theDestinationFolder to theFile
		end if
	end if
end drop

If you put a simple log message inside the drop handler (for example”log 1), and then drop files on the text fields, you’ll see that the active text field does not log anything and thus is not using the drop handler. It’s doing its own thing. You’ll want to connect the text fields to the ‘conclude drop’ handler.

Hook both of your text fiels up to the ‘conclude drop’ handler as well as the ‘drop’ handler. Then use this code and watch to log. You’ll see that the active text field will use the ‘conclude drop’ handler and the inactive field will use the ‘drop’ handler.

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

on drop theObject drag info dragInfo
	log "in drop"
	
	if "file names" is in types of pasteboard of dragInfo then
		set preferred type of pasteboard of dragInfo to "file names"
		set thePaths to contents of pasteboard of dragInfo
		if (count of thePaths) > 0 then set content of theObject to (item 1 of thePaths)
	end if
end drop

on conclude drop theObject drag info dragInfo
	log "in conclude drop"
	
	if "file names" is in types of pasteboard of dragInfo then
		set preferred type of pasteboard of dragInfo to "file names"
		set thePaths to contents of pasteboard of dragInfo
		if (count of thePaths) > 0 then set content of theObject to (item 1 of thePaths)
	end if
end conclude drop

Now go back to interface builder and in the attributes section of the inspector, uncheck the editable checkbox for the text fields, and then try again. You’ll see that they both use the ‘drop’ handler. So now you should understand what’s going on.

Regarding the image view, it works the same. You can register it for ‘file names’ just like the text fields. It will use the ‘drop’ handler. Just make sure you don’t try to set the image view to the file name… it will error because obviously image views can only be set to an image. But you can get the dropped file and then set the content of your text field to its value.

Hope that helps!

Thank you very much for the help. I was able to work through everything and now that part of the program is running smoothly.

(As I read through your response the first few times I was having trouble understanding what all was going on, but as soon as I started doing things one by one and line by line I really began to understand what was happening. Thanks to you I have a much better understanding of what all is going on behind the scenes. Thanks again!)

You’re welcome. I’m glad I could help! :smiley: