[python-win32] Using os.startfile() for automation
Tim Roberts
timr at probo.com
Thu Feb 3 22:18:40 CET 2011
Tony Cappellini wrote:
>
> On Windows XP, I'm able to launch the application associated with
> file1.xxx.
>
> os.startfile("file1.xxx")
>
> I want to start more instances of the application, so that I can
> process several files at the same time.
>
> calling os.startfile("file2.xxx") causes the instance of the
> application to also open file2.xxx.
>
> The problem with this is- I have to wait until file1 has finished
> processing, before I can start file2.
Ah, so you're saying you CAN execute the second os.startfile, but the
application opens them both in the same instance? It's really up to the
application to decide how to handle this. os.startfile is exactly like
double-clicking the file icons in Explorer. Is this a public
application? There may be a smarter way to handle this. os.startfile
is a bit "brute force".
> If, after opening the first instance for file1, I can use the Start
> Menu, Program Files, open another instance of the application, I can
> then start
> processing file2 at the same time as file1, but using a separate instance.
>
> Is there any other way to invoke startfile() or other function- to
> force a new instance of the application to open?
Not through os.startfile, but you can go look up the application's
executable on your own, and then start the process yourself. For
example, instead of
mydoc.doc
you can say:
"C:\Program Files\Microsoft Office\Office10\winword.exe" mydoc.doc
However, it's still up to the application to decide whether it will
handle that in one instance or two.
> How would I close the application from Python, that was started with
> os.startfile()?
Well, you could go searching for the window by name, since you know the
file name, and send it a WM_CLOSE. However, it would probably be
smarter to see if the application can be automated through COM. That
way, you're in complete control of the operation.
Alternatively, you can use CreateProcess through the API to launch the
application. That way, you get a process handle that you can control.
os.startfile is a shortcut. For that convenience, you give up some
flexibility.
> Once the application is open, I just want to use File Save As Text, to
> get the data into a text file.
> Python's app-automation capabilities are kinda weak and not well
> documented, so I may resort to AutoIt3.
That's a ridiculous complaint. Languages do not have "app automation
capabilities". If the application you're controlling has the ability to
be automated through COM, you can use pywin32's amply documented COM
facilities to do it. If it doesn't, then you'll have to inject
keystrokes using the API, exactly like you would if you were writing
this in C.
--
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the python-win32
mailing list