How do I communicate between python and a mac application?

为君一笑 提交于 2019-12-02 04:13:46

问题


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?


回答1:


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.




回答2:


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.




回答3:


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?




回答4:


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.




回答5:


On osx, 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.



来源:https://stackoverflow.com/questions/11392022/how-do-i-communicate-between-python-and-a-mac-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!