Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!
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):
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
Any ideas? This is my first attempt ever at doing drag & drop with LiveCode so be gentle
on dragEnter
-- Check if the dropped item is a file
if dragData["Files"] is not empty then ## changed
set the dragAction to "copy" -- Visual feedback for dragging
end if
end dragEnter
on dragDrop
-- Check if the dropped item is a file
if dragData["Files"] is not empty then ## changed
-- 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 for the fix. It now works...IF I drag the text file onto the "Specs" field itself. I want to be able to drag the text file to anywhere on the interface and have this work. Can this be done?
cmhjon wrote: Wed Nov 20, 2024 1:40 pm
I want to be able to drag the text file to anywhere on the interface and have this work. Can this be done?
Only objects can receive the dragEnter message, not the card.
You could however put the exact script you use and put it on the card script instead of using it as script for field "Specs".
Then you make a graphic the size of the card with its opaque set to true. Set the layer of the graphic to 1. Set the backgroundColor of the graphic to the backgroundColor of the card. No drag related script in the graphic or elsewhere, just in the script of the card. Now you would have an object that is able to trigger the dragEnter message as well as all other controls on the card.
If you "drop" it will work in your special case since the receiving object of the payload is hardcoded into your dragDrop handler.
You might have to experiment with you specific needs to see it it fits.