Use program B to control A - how to control A? Subprocesses?

This is the place for queries that don't fit in any of the other categories.

Use program B to control A - how to control A? Subprocesses?

Postby Heezea » Tue Apr 15, 2014 3:21 pm

Hey guys, I have a question. I've spent a couple hours looking through posts and trying to find a tutorial for this, but no luck so far. I'm currently taking a Coursera python class, but I don't think we'll be covering this topic any time soon.

Anyway, the question: If I have a Program A that I want to write a program to interact with, say Program B, how would I pass/enter stuff I'd entered in Program B to Program A? In the most simplistic way, I could make the mouse move to specific positions and click, enter text, etc. However, I realize there should be much better ways to accomplish what I'm trying to do, that are less error prone.

I found some vague references to subprocesses, but I couldn't find any basic information on how to actually implement these. Besides, I wasn't even really sure this was the correct approach.

If anyone could point me in the right direction with some advice/tutorials/links, I'd greatly appreciate it.
Heezea
 
Posts: 2
Joined: Tue Apr 15, 2014 3:12 pm

Re: Use program B to control A - how to control A? Subproces

Postby micseydel » Tue Apr 15, 2014 4:34 pm

Most programs aren't designed to be programatically controlled, unfortunately. Some are even designed to make it more difficult. Usually, it's easier to simulate whatever one or both of those programs is doing in pure Python. For example, rather than controlling a web browser, you'd do directly what the web browser does, within Python. (That's a funny example that came to mind though; Selenium is actually a nice way to control browsers from Python.)

I believe for some Microsoft software you can use COM to control them, but I know I and most of the people on this site don't use Windows so it might be hard to get help with that.

What exact programs were you trying to control?
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1223
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Use program B to control A - how to control A? Subproces

Postby Heezea » Tue Apr 15, 2014 5:05 pm

Yikes, not the answer I was hoping for. I figured there would be a way to "intercept" the commands coming from program A (the original) and then replicate those in program B (my coded program), just tweaking the replicated values as needed.

It's a pretty obscure little program we use at work, nothing mainstream at all. It is basically a front-end user interface (call it program A) that takes the user's input and enters it into a database (call this database A) . It's pretty dated and some of the functionality is becoming less useful. However, because I don't have access to the database A directly, my thought is to write a program (B) that has enhanced functionality and formats/feeds the input to be accepted into program A, which will then feed into database A.

I realize this is a bit cumbersome and it would be best to start fresh, but the limitations are pretty firm.
Heezea
 
Posts: 2
Joined: Tue Apr 15, 2014 3:12 pm

Re: Use program B to control A - how to control A? Subproces

Postby micseydel » Tue Apr 15, 2014 6:08 pm

I was (perhaps falsely) assuming that your program has a GUI and lacks a command line interface. If it has a command line interface, then it's very easy to interact with programatically, and we could definitely help. If not...

This is god-awful, but one way to accomplish what you want might be to simulate mouse and keyboard events. It's a fragile solution since it would rely on knowing where on your screen the program is, as well as how the interface will change, and you risk terrible things happening if the interface does something unexpected and then you're clicking randomly. On top of your computer not being human-usable at that time.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1223
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Use program B to control A - how to control A? Subproces

Postby 7stud » Tue Apr 15, 2014 6:36 pm

old_prog.py:
Code: Select all
user_input = raw_input("Enter your data2: ")

f = open('my_db.txt', 'w')
f.write(
    '{}\n'.format(user_input)
)
f.close()


new_prog.py:
Code: Select all
import subprocess as sp

user_input = raw_input("Enter your data1: ")
enhanced_input = "** {} **".format(user_input)


p = sp.Popen(
    ['python', 'old_prog.py'],
    stdin = sp.PIPE,
    stdout = sp.PIPE,
)

prompt_text = p.communicate(enhanced_input)


Code: Select all
~/python_programs$ python new_prog.py
Enter your data1: hello
~/python_programs$ cat my_db.txt
** hello **
7stud
 
Posts: 85
Joined: Wed Apr 02, 2014 2:36 am

Re: Use program B to control A - how to control A? Subproces

Postby Somelauw » Tue Apr 15, 2014 7:57 pm

Heezea wrote: If I have a Program A that I want to write a program to interact with, say Program B, how would I pass/enter stuff I'd entered in Program B to Program A?


If it's a cli program, use subprocess.
If it's a gui program, it's harder. One way to do that would be to call programs like xdotool or autohotkey from within a python script.
Join the #python-forum IRC channel on irc.freenode.net!
Somelauw
 
Posts: 69
Joined: Tue Feb 12, 2013 8:30 pm

Re: Use program B to control A - how to control A? Subproces

Postby Yoriz » Wed Apr 16, 2014 12:05 am

If it's on windows you could try pywinauto.
https://code.google.com/p/pywinauto
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 781
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Use program B to control A - how to control A? Subproces

Postby snippsat » Wed Apr 16, 2014 12:33 am

For me the question is not clear,maybe you are talking about asynchronously message passing between two long running program as an example GUI programs(because you are talking about mouse control?)
You have Actor Model or a better word for it "message passing concurrency".
There are of course a lot of concurrency/asynchronously solutions for Python,all Threading stuff in standard library,and a lot newer solution's that is exacting like "concurrent.futures" and Python 3.4(asyncio).
And a lot of 3 party library stuff like Gevent,Twisted,Greenlets.....

GUI(Wxpython,PyQt(Pyside),PyGTK) can have own solution like "QThreads" PyQt (asynchrnous signals/slots, event loop, etc.).

And a nice solution by 7stud with subprocess,for not long running programs/process but calling dead code and make it do some work.
User avatar
snippsat
 
Posts: 157
Joined: Thu Feb 21, 2013 12:04 am

Re: Use program B to control A - how to control A? Subproces

Postby mckryall » Wed Apr 16, 2014 2:20 am

The old program isn't actually written in python, is it?
Python 2.7
Mac Mini
Laptop w/ Ubuntu 14.04
Steam; PSN
mckryall
 
Posts: 94
Joined: Sat Nov 23, 2013 6:13 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests