3

This might be a vague question, but I failed to rephrase it properly. So here's an explanation.

I developed an app that was originally developed as a Mac application, using API's from both Carbon/Cocoa to achieve my task. (The goal of the application is to send 'keystrokes' to an app after mouse movements / other keystrokes. I use Accessibility API's for that).

But now, I want to add scripting support, so there can be conditional keystrokes. Such as: after 5 times of pressing 'Y', I want to press the button 'B'.

Within OSX, there's no possibility for a Python app to do such a thing, and I can't find any libraries that allow me to do it. So I figured I'd make the app two-fold. First: I'll write an app in Cocoa/Carbon with the basic functions: SendKeyToApp(pid,key) and WaitForMovement().

The python app will communicate with that app, using those method calls. The question here is: how do I communicate between those two apps? Will I use a HTTP Server, a Socket Server, some kind of subprocess.Popen() hacks. I frankly have no idea.

Does anyone have any ideas to achieve such a task?

gecco
  • 17,969
  • 11
  • 51
  • 68
Mats Willemsen
  • 815
  • 1
  • 9
  • 25

5 Answers5

3

Easier. I went to use Accessibility API's from within Python, and it all allowed me to easily do this without any Cocoa / Carbon at all.

For the ones interested, it's called atomac.

Mats Willemsen
  • 815
  • 1
  • 9
  • 25
2

PyObjC is perhaps what you're looking for:

PyObjC (pronounced pie-obz-see) is the key piece which makes it possible to write Cocoa applications in Python. It enables Python objects to message Objective-C objects as if they're fellow Python objects, and likewise facilitates Objective-C objects to message Python objects as brethren.

You could write a bridge between your python app and your cocoa app using PyObjC.

gecco
  • 17,969
  • 11
  • 51
  • 68
0

There are many ways to synchronize/communicate the data between two applications, but let me explain the simplest :

RESTful Serialization:
Serialize the objects into XML/JSON or any other custom format both app frameworks can parse. eg. http://docs.python.org/library/json.html
If both apps are on the same server/machine I'm expecting all you have to do is to encode/store the data into the same file(s) and read/parse the data from respective file(s) in other application.

Otherwise you may need to create a web service for accessing the data file.

Let me know in comments if you have further queries.

Usman Chaudhry
  • 430
  • 4
  • 11
  • I need it to be 'instant'. It seems like File I/O would be too slow for that? I need things to be settled within 100 milliseconds for it to be efficient enough. – Mats Willemsen Jul 09 '12 at 10:08
  • Use HTTP GET/POST request that will be over a network connection on the local machine or remote machine. On a local machine you could be talking 1 millisecond or less, remote on a private local network probably not more than 30 milliseconds – Neal Davis Nov 13 '17 at 01:42
0

In my eyes the simplest way to establish communication between two applications is the client-server protocol XMLRPC. Both Cocoa and Python do support it.

The Python part is fairly simple:

import xmlrpc.client
rpcProxy = xmlrpc.client.ServerProxy(URL_OF_SERVER)
rpcProxy.SendKeyToApp(pid,key)

As for the Cocoa-part, I don't know, but it seems to be possible: XML-RPC Server in Cocoa or Best way to use XML-RPC in Cocoa application?

Community
  • 1
  • 1
gecco
  • 17,969
  • 11
  • 51
  • 68
  • It seems like Cocoa doesn't do a fine job at serving XMLRPC, yet the client support seems to be extensive. How would I do this? I need Python to send something to the Cocoa server, not the other way around. (Python invents the 'commands', Cocoa executes them). – Mats Willemsen Jul 09 '12 at 10:20
0

On , many native applications support AppleScript (aka OSA) as their native scripting API. Thus your question becomes one of interacting between Python and Applescript (and figuring out how to talk to your target application in AppleScript in the first place).

There is some OSA support in the Python standard library and a third-party module py-applescript you might want to look at.

tripleee
  • 175,061
  • 34
  • 275
  • 318