Dropping System Folders on Fields

This lesson demonstrates how to allow a user to drop a system folder onto a field. The folder path is then displayed in the field.

Create UI Elements

Create UI Elements

Create a field that you can use to display the folder path in.

Add Code To Field Script

Add revTalk To Field Script

If the user clicks and drags a system folder over the field then the dragData["files"] property will return the path to the folder the user is dragging. The code to deal with this scenario is as follows.

1) Create a dragEnter handler that checks if the dragData["files"] is a folder. We don't want to accept files. If the property contains a folder path then the field tells the LiveCode engine that it can accept the data being dragged by setting the dragAction property.

2) In the dragDrop handler assign the dragData["files"] property to the field. Since LiveCode paths always use slash as a path delimiter it is a good idea to format the path so that it looks appropriate for the platform your program is running on. Two helper functions are included in this script that can convert from a LiveCode path to a native path (3) and a native path to a LiveCode path (4).

==========

Copy & Paste

==========

on dragEnter
	if there is a folder the dragData["files"] then
		set the dragaction to copy
	end if
end dragEnter

on dragDrop
	## Display the folder path in platform native format
	set the text of me to \
			 fileConvertLCPathToNative(the dragData["files"])
end dragDrop

## Converts LiveCode path to native path
function fileConvertLCPathToNative pPath
	if "Win" is in the platform then
		replace slash with backslash in pPath
	end if
	return pPath
end fileConvertLCPathToNative

## Converts native path to LiveCode path
function fileConvertNativePathToLC pPath
	if "Win" is in the platform then
		replace backslash with slash in pPath
	end if
	return pPath
end fileConvertNativePathToLC

Code in Action

Code in Action

Now when you drag a folder onto the field a drop will be allowed...

and the path to the folder will be displayed.

Additional Notes

You may also want to take into account situations where the user is dragging multiple files or folders. In this case the dragData["files"] will contain one file/folder path per line.

1 Comments

Barry Sumpter

for miltiple selections expand the field "field" to show miltiple lines. And add the following code line:

on dragEnter
set the acceptDrop to true

Add your comment

E-Mail me when someone replies to this comment