Drag & Drop?
Posted: Tue Nov 19, 2024 11:37 pm
Hi everyone,
I have a simple stack that I want to be able to drag and drop a text file (and only a text file) from the desktop (or other location) onto the stack interface and have it put the contents of the text file into a text field. Here is my code (which is in the stack script) but it's not working (nothing happens):
Any ideas? This is my first attempt ever at doing drag & drop with LiveCode so be gentle
Thank you and best regards,
Jon
I have a simple stack that I want to be able to drag and drop a text file (and only a text file) from the desktop (or other location) onto the stack interface and have it put the contents of the text file into a text field. Here is my code (which is in the stack script) but it's not working (nothing happens):
Code: Select all
on dragEnter
-- Check if the dropped item is a file
if "file" is among the items of the dragData["type"] then
set the dragAction to "copy" -- Visual feedback for dragging
end if
end dragEnter
on dragDrop
-- Check if the dropped item is a file
if "file" is among the items of the dragData["type"] then
-- Get the file path from the drag data
put the dragData["files"] into tFilePath
-- Ensure only one file is processed (if multiple files are dragged)
put line 1 of tFilePath into tFilePath
-- Validate that the file is a text file
if char -4 to -1 of tFilePath is ".txt" then
-- Read the contents of the file
put URL ("file:" & tFilePath) into tFileContents
-- Place the file contents into the "Specs" field
put tFileContents into field "Specs"
else
-- Notify user if the file is not a text file
answer "Please drop a valid text file." with "OK"
end if
end if
end dragDrop

Thank you and best regards,
Jon
