Drag and drop a file onto an app icon?

Sorry if this is too basic: I have an old RB app that used to support opening my files with a simple drag and drop onto the app icon, but now that I’ve recompiled it with Xojo and made some tweaks, that doesn’t work anymore. How can I bring back drag and drop functionality? The files use a custom file extension and I can still open them up directly from my app via the Open dialog just fine.

When you drop onto an application you trigger an OpenDocument event. You need to have the event added to App and handle the folderitem from it. Multiple files dropped will each trigger the event separately.

You’ll need to check the folderitem for validity (nil, exists and, depending on your program, readability and writeability).

Also, to be able to drop onto the icon you may need to have the filetypes/extensions mapped. In Mac OS X you can force dropping any file type by pressing cmd while dragging.

Here an example project that shows how to respond to a file dropped on the program’s icon.
Drop Namer

Thanks for your help. I tried to copy that code into my project but it’s not working for me. I’ll have to play with it some more and figure out why. Are there any more examples out there? I’m the kind of person who really needs to see things spelled out sometimes to get them to sink in.

I also heard there’s some AcceptFileTypes app property I should worry about, but I can’t find it anywhere. Is that something I need to enable? If so, where do I find it?

It seems so odd to me that drag and drop to open isn’t baked in by default since it’s standard with all Mac apps.

It’s baked-in, in the sense that it’s all there for you to use, but it can’t know what you want to do with it. So you have to do the basic stuff with it: Tell it what your program accepts (AcceptTypes) and what to do with dropped files (OpenDocument)

It’s vastly easier to implement than it is in native mac apps now (ObjectiveC) or in pre-OS X (CodeWarrior, MacApp) Mac System (where it initially debuted) .

Step 1.-Tell the app what types your program knows about:
1a.-Insert Menu -> File Types Set
1b.-Inside newly create File Type Set click the second “Document” button and select “special/any”

Step 2.-Tell the app what types your application can handle opening:
2a.-Build Settings -> OS X -> Click File Types -> Select your newly-created filetype

Step 3.-Tell the app what to do with it:
3a.-Go to app -> Insert new event handler -> Insert OpenDocument event
3b.-Tell it to do something, e.g.

If item <> Nil And item.exists Then MsgBox("Dropped file "+item.Name) Else MsgBox(item.name+" doesn't seem to exist")

Now you can drop any file or folder onto the icon in the dock or in the finder and it’ll execute the code above.

If you remove Step 2, you won’t be able to drop as you won’t have told the app (and thus the Finder) what your app can open. You can still force drop by pressing the command key while dragging (icon will highlight).

If you want to do the same for a specific window or window control you add the event “DropObject” to said window or control and handle the “obj” you’d receive. It’s a bit different in that you have to loop over this object to split it and get what type of dropped object it is (within a window an object can be anything, even a “promise” of data from another application).

do if obj <> nil then if obj.FolderItemAvailable and obj.FolderItem <> nil then dim f as FolderItem = obj.FolderItem if f <> nil and f.Exists and f.IsReadable Then MsgBox("Dropped file "+f.Name) Else MsgBox(f.name+" doesn't seem to exist") end if loop until not obj.NextItem

Drag and drop is EASY. What isn’t easy is drag and drop, as soon as you try to handle stuff coming from outside your app :smiley:

Thanks that’s much clearer to me. I think I’ve got enough information from everyone to put together and get this to work now. :slight_smile: I really appreciate everyone’s help!

Eduardo’s instructions solved my drag and drop problems too, and I thank him for all his help. However, when I tried to implement the identical approach for Windows, it failed. I can drag the file onto the icon, and the program starts up, but the file is never handled by the app.

I notice that in the Windows build settings, there does not appear to have a File Types sector option. How do I indicate which file types can be handled?

I still can’t get it to work. I’ve done all three steps, and if I hold the key-override the app accepts the drop and opens the document perfectly.

But Finder will not recognize the app as accepting file drops. :frowning:

I have an app that does accept all documents dragged over its icon.

I have added special/any to FileTypes2 and placed this in OS X build settings File Types

then in App.

Sub OpenDocument(item As FolderItem) // For demo purpose msgbox item.shellpath // Do whatever you need with obj.FolderItem End Sub

Now all you have to do is select the file types you need in your FileTypes

Every working example is using Special/Any
Can someone show it being done with a custom filetype?

Again, I don’t have any issues with the OpenDocument section. It won’t accept the drop in the first place.

These things work for me on OSX. They do not work for me on Windows. Has anyone gotten drag and drop to work with a Windows app? If so, I would be grateful for some insights into how to do this.

Thanks,
Bob

I’ve been playing with the test project above, and I can’t get it to not accept every filetype.
So something is wrong somewhere…

It’s something weird with the UTIs.
I can get it to accept every file when there is none. Not useful at all.
If I add a UTI to it, it won’t accept even the proper files.

[quote=122315:@Tim Parnell]I’ve been playing with the test project above, and I can’t get it to not accept every filetype.
So something is wrong somewhere…[/quote]

At least in the Mac OS X Cocoa open file dialog, a bug prevents adequate filtering. It maybe the case for drop file as well. Just do it afterward in Opendocument by checking the extension. If you do not want a file type, just do nothing.

The filtering works fine under Windows.

[quote=122320:@Michel Bujardet]At least in the Mac OS X Cocoa open file dialog, a bug prevents adequate filtering. It maybe the case for drop file as well. Just do it afterward in Opendocument by checking the extension. If you do not want a file type, just do nothing.

The filtering works fine under Windows.[/quote]
This is drop to dock/finder icon; my open dialogs are fine. I haven’t had problems with FileType up to this point. :frowning:

Also, double-clicking a file (even when the app is closed) works just fine.

I just tried a very short project which opens text files when they are dropped over the icon, but other types do not select the icon.

  • Add FileTypes and add text/plain
  • In the OS X build setting, enter Creator Code ??? and select the newly created FileTypes
  • Build the app

When a text file is dropped over the app icon, it opens it.

Adding the default text/plain works.
Here’s a screenshot of my FileTypes to further show my confusion.

There’s variations among the MacType/Creator fields to see if it would have any effect. Nothing works though except plain/text

[quote=122334:@Tim Parnell]Adding the default text/plain works.
Here’s a screenshot of my FileTypes to further show my confusion.

There’s variations among the MacType/Creator fields to see if it would have any effect. Nothing works though except plain/text[/quote]

Indeed it does not work. Then any+filtering seems like a valid solution.

Just encountered the same problem here. My app will only accept drag and drop when special/any is included.