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)