Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger drop event on another application in OSX

I use a bit of proprietary software for DJing (Native Instruments Traktor). If you're not familiar with this kind of thing, just think of it as a glorified iTunes: it browses and plays audio files.

I want to build a custom file browser application for this, but there isn't any kind of API. It does, however, allow audio files to be dragged and dropped from the file system, which is a great start.

The nature of the file browser I'm designing means I don't want to actually have to drag and drop things - I just want to click a component in my application, and have the same effect. So I'm looking at ways to programmatically trigger a drop event on the other application, from my file browser application.

My platform of choice is Python with PyQt, but I'm beginning to feel I might have to go a bit lower-level. Haven't done a huge amount of C#/Java though so that could be a bit of a learning curve (I have done a lot of ANSI C but that's probably too low level...)

This is how far I've got:

  • I've made a really simple PyQt application
  • I can create QDrag object when a QLabel in my application is dragged
  • I can attach all the correct MIME data to represent the audio file to this
  • So if I drag and drop that QLabel from my application into Traktor, it recognises the audio file and plays it - good times

So now I need to cut out the middle-man and, on click, package up my MIME data and make Traktor think I've dragged and dropped it onto it.

I've also done some delving in the OSX developers' docs, specifically this stuff, which describes the sequence of messages that are passed to the target application (drop destination).

This all makes sense, but I'm on the verge of dropping down to C#/Java to try and mimic these messages, which sounds like rabbit hole I'd rather not venture down if I can avoid it.

So, before I do...

  1. Is this even possible? Or am I going to hit some kind of cross-app security barriers etc? (drop destinations only accepting messages directly from the OS or something)
  2. If it is, is there an easier way to do it? Ideally with PyQt/Tkinter/wxPython...?

I know I could do this with click automation, but I can imagine that being really unreliable, would be massively reliant on the positions of windows, etc.

Thanks in advance!

like image 992
Stu Cox Avatar asked Dec 21 '12 03:12

Stu Cox


1 Answers

Not tried this, but things like CGEventCreateMouseEvent and CGEventPostToPSN might be helpful. CGEvent.h and CGRemoteOperation.h

I'm also wondering if the target application might respond to apple events - if it does you could create apple events and send those to it which would be cleaner. I'd try running AppleScript Editor or Automator and open dictionary on the app in question to see if it has a dictionary of events that might do what you want.

luck.

like image 50
Dad Avatar answered Sep 18 '22 12:09

Dad