LiveCode LessonsHow To - Step-By-Step Guides To Tasks In LiveCode LiveCode tipsHow to create an application that opens when you drag a file onto it

How to create an application that opens when you drag a file onto it

Lots of applications and programs allow you to open them by dragging a file onto the application icon. The application will then open and load the file you dragged on.

It is quite simple to add this behavior to your Rev standalones. In this lesson we will show you how to create a standalone that displays a text file in a field when you drag a text file onto the standalone.

Attached Files

Creating our stack

Creating our stack

Firstly lets create a very simple stack, all we need is a scrolling field which we will use to display the contents of our text file.

Getting the filename on Windows

Windows standalones start up automatically when a file is dropped on to the. We use the $ keyword to get the name of the file that is dropped onto the standalone. The $ is used to indicate an environment variable on Unix systems and a command-line parameter on Unix or Windows systems.

In this case we will use $1, when a file is dropped onto the standalone $1 contains the path to that file.

We will handle the startup message to retrieve the file name, the startup message is sent to the stack when the application opens. To start we just want to check we are successfully getting the filename so we add the handler

on startup

put $1 into field "display"

end startup

Building a standalone

Building a standalone

We need to build a standalone to test our startup handler.

Open the Standalone Application Settings from the File menu and choose OS X and Windows to build for. Leave all the other settings at the defaults.

Then close the Standalone Application Settings and choose Save as Standalone Application from the File menu then choose where you want to build your standalone.

Testing the standalone

Testing the standalone

Now go to the folder your standalone application was built in and open the Window sfolder, now try dragging a text file onto Text Display.exe. The application will open and the path to the file you dragged on will be displayed in the field.

Loading the text file

Loading the text file

We don't just want to display the file name though, we want to display the contents of the file so we need to include a handler that will put the contents of our file into the field. At this stage we also want to check that the file is a text file before we display it as our startup handler will accept any type of file.

on startup

local tFileName


put $1 into tFileName

loadFile tFileName

end startup

on loadFile pFileName

## Check that is is a text file

if char -4 to -1 of pFileName is ".txt" then

## We use the URL keyword to get the contents of the file

put URL ("file:" & pFileName) into field "display"

end if

end loadFile

The build the standalone and test it again.

Getting the filename on Mac

On Mac we can't use the $ keyword to get the filename, instead we need to hande the appleEvent message. This message is sent to the current card whenever the application receives an Apple event.

We check what type of appleEvent we get, if it is an open document event then we call out loadFile handler.

on appleEvent pClass, pEventID, pSender

if pClass is "aevt" then

## This is an apple event

## We request the data about the apple event


request appleEvent data

put it into tFileName


if pEventID is "odoc" then

## This is an open document event

loadFile tFileName

end if

end if

pass appleEvent  

end appleEvent

Accepting text files on Mac

There is another step we need to take in order for our Mac standalone to accept text files, we need to modify the preferences file within the application bundle to specify the file types our application accepts, in this case it is only text files.

Within the application bundle go into the Contents fodler and open the info.plist file. You can use a text editor or a plist editor.

We need to add an extra CFBundleTypeExtensions entry stating that the app can open text files. Add









<string>IMG Disk Image</string>




after the automatic CFBundleTypeExtensions created by the standalone builder. This states the application can accept files with the extension txt

If you want your application to accept other file types you need to add a CFBundleTypeExtensions entry for each allowed type.



Add your comment

E-Mail me when someone replies to this comment