Drag and Drop file to get file anme problem

I have a text box in my interface. I added the below code so that I can drag and drop a file into the text box and have the file name appear in the text box. It works the first time I drag a file. After that, if I drag and drop a file into the text field, the field clears but does not show the new file name. If I click on another object (like a table) to activate it and then click back on the text box, then the drag and drop works again for a single instance.

So, what is going on exactly? By click on something else, am I “resetting” the awake from nib or something? Anyone have this problem and understand what is going on?

Second question is, is there a different way to do this that won’t have this problem or a workround that would maybe simulate clicking on another object?

Thanks.

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

on prepare drop theObject drag info dragInfo
	set contents of text field "compField1" of window "main" to ""
end prepare drop

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 theDropPaths to contents of pasteboard of dragInfo
		set preferred type of pasteboard of dragInfo to ""
		set theDropFile to item 1 of theDropPaths
		set AppleScript's text item delimiters to "/"
		set contents of text field "compField1" of window "main" to (last text item of theDropFile as string)
		set AppleScript's text item delimiters to ""
	end if
end drop

on conclude drop theObject drag info dragInfo
	if (contents of text field "compField1" of window "main") contains "/" then --Trap weird error where the full path gets populated the first time you drag and drop.
		set theTempVariableTextString4Swap to (contents of text field "compField1" of window "main")
		set AppleScript's text item delimiters to "/"
		set contents of text field "compField1" of window "main" to (last text item of theTempVariableTextString4Swap)
		set AppleScript's text item delimiters to ""
	end if
end conclude drop

Model: PowerPC G5 Tower OS 10.4.8
AppleScript: XCode 2.5
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

So, I simplified it to this and it is working. I basically just made it clear the field when you drag over the text field and then format the field after you have dropped something into it. I would still like to know why the original wasn’t working:

on drag entered theObject drag info dragInfo
	set contents of text field "compField1" of window "main" to ""
end drag entered

on conclude drop theObject drag info dragInfo
	set theTempVariableTextString4Swap to (contents of text field "compField1" of window "main")
	set AppleScript's text item delimiters to "/"
	set theTempVariableTextString4Swap to (last text item of theTempVariableTextString4Swap)
	set AppleScript's text item delimiters to "."
	set theTempVariableTextString4Swap to the first text item of theTempVariableTextString4Swap
	set AppleScript's text item delimiters to ""
	set contents of text field "compField1" of window "main" to theTempVariableTextString4Swap
end conclude drop

In case anyone was following this, I think I stumbled upon the problem. I was looking at the example script for drag and drop and found this in the text box example.

on conclude drop theObject drag info dragInfo
	(* We need to have this handler do nothing to keep the text field from doing it's own drop. This is true for text view's as well. If you want to let the text field or text view do the actual drop you can remove the "conclude drop" event handler and then not do anything in the "drop" event handler. *)
end conclude drop

I think my problem was that the built-in drag and drop support was conflicting with what I was trying to do. I should have used the empty on conclude drop handler above and then put all of my code in the on drop handler.

Hey everyone,

I would like to enable drag and drop on a text field in my app, but so that it can process the files that are dropped (rename or replace text content).

Could anyone help me figure out how to get the path of each of them so that the app knows what to process?

Thanks in advance!