Pywinauto run a script on the background - python

I have a certain script that i want to run on the background. I know that similar questions have been asked but i am still having a hard time achieving this.
Here is my code
import pywinauto
from pywinauto import Application
import time
app = Application().start("notepad.exe")
#app = Application().connect(title="test.txt-Σημειωματάριο")
app.window().set_focus()
app.window().minimize()
time.sleep(2)
app.window().send_keystrokes("{ENTER}")
The above code is minmizing the notepad window but i cant see that enter was indeed pressed. Same goes for other keystrokes that i tried. What am i doing wrong?

Related

In pyautogui, it seems to be running in only one window

I am making a bot at the moment using pyautogui. When I run it in pycharm, it clicks on wherever my mouse is after 10 seconds, but if I use it on google, it runs but does not click. here is the code:
import pyautogui
import time
time.sleep(10)
while True:
pyautogui.click()
does anyone know how to fix this (btw i'm using chromebook)

Locking in multi-threaded pywinauto send keys

I'm new to pywinauto and I'm creating several notepad windows and typing a text in all of them. However, this is not dependent on each other, so this can be run concurrently using threads.
However, when I try to do the same, the text get messed up because there are several threads trying to access the type_keys() method at the same time. Is there any way I can achieve the same concurrently?
There is another method .set_text("...") that doesn't require window to be in focus. It's available for edit box only.
.type_keys() or .click_input() is not a good choice for concurrent automation or for a locked machine / minimized RDP. More details can be found in Remote Execution Guide.
you can also try this importing keybord from pywinauto and the send the line you would like to send this is a small exemple :
from pywinauto import application
from pywinauto import keyboard
app = application.Application()
app.start("Notepad.exe")
keyboard.SendKeys('hello')
with this code you will open Notepade and write hello in Notepad , I just created to .py file and both have same code and I did call them on main file and worked perfect
I created A.py and put code on it and I created B.py and put same code and in C.py I did import A , import B it and run it it did open 2 Notpad and wrote the text look for this example :
A.py
from pywinauto import application
from pywinauto import keyboard
app = application.Application()
app.start("Notepad.exe")
keyboard.SendKeys('hello')
B.py
from pywinauto import application
from pywinauto import keyboard
app = application.Application()
app.start("Notepad.exe")
keyboard.SendKeys('hello friends',with_spaces=True)
C.py
import A,B
and run C.py make sur all the files are saved in same folder

Tracking focus changes

I'm trying to track the window focus changes (what application is in the foreground) with python 3.7 in a windows 10 (64b) machine because I'd like to log how much time I spend using each application (I hope chrome is a different app for each tab XD)
I tried to search in the web about how to do it but didn't find it (odd). For now I learnt that I have to install win32api (pypiwin32) and that with this code I can get the name of the window
from win32gui import GetWindowText, GetForegroundWindow
print(GetWindowText(GetForegroundWindow()))
That works fine but I don't want to make a loop with that every second, I'd like to have a callback that when the event 'onWindowFocusChange' or something like that is called run that.
EDIT: As David Heffeman pointed out, I was using wrong terminology. What I meant is the piece of software that I'm interacting with at each point. (None if the screen is blocked, If I'm playing a game and listening to music, the game, if I'm reading a web or a pdf that browser or reader, etc... hope this clarifies the matter.
This example code here logs all focus changes: https://gist.github.com/keturn/6695625

GUI program with toggable console?

I've seen some apps allow you to show/hide the console when you need to read log messages. For example Blender3D allows that (blender.org).
I was wondering if this can be done in Python and how.
My main window is a Panda3D (panda3d.org) window.
I've read somewhere that one option is to hide the "real" console (pythonw) and create another console and just redirect everything from the "real" one to it, every time you want to "show" the "real" console. No idea how this can be done.
Or at least a way to choose whether to start the program with the console or without it by reading a configuration file or something.
I'm assuming you are talking about Windows because this console toggling in blender is Windows exclusive. I'm guessing Blender uses GetConsoleWindow and ShowWindow on Windows.
This is how you could do it in python with pywin32:
import win32gui, win32console, win32api, win32con
import time
console_window = win32console.GetConsoleWindow()
time.sleep(1)
win32gui.ShowWindow(console_window, win32con.SW_HIDE)
time.sleep(1)
win32gui.ShowWindow(console_window, win32con.SW_SHOW)
time.sleep(1)
If you run this program with python and not pythonw it will show the console, sleep for a second, hide the console, sleep for another second and then hide it again.
Mind that this code only works on Windows. On other platforms silly stuff like this is not necessary because if you want a program to show a console then you run it from the console.

How to send simulated keyboard strokes to the active window using SendKeys

I am new to Python and am trying to send a combination of key strokes to an application I open under Windows 7. While my script will work flawlessly when I invoke Notepad.exe for testing purposes, it does not with the actual application I am trying to use.
Here is my code so far:
import win32com.client
import time
import SendKeys
import os
from ctypes import *
shell = win32com.client.Dispatch("WScript.Shell")
os.startfile("C:\...exe")
time.sleep( 5 )
shell.SendKeys('%{F4}') # 'Alt+F4' to close the application again.
For some reason, the application does not close in reaction to the script. When I hit 'Alt + F4' on my keyboard, it closes as expected. Any ideas on what might be going on here?
Any help is welcome! Please bear in mind that I am new to Python ;-)
PS: I have already verified that the application runs in the active window by including this code snippet:
import win32ui
wnd = win32ui.GetForegroundWindow()
print wnd.GetWindowText()
OK ... I rebooted the system and for some reason it is working now. I am wondering if some process instance from previous programme invocations might have been lingering on the system. Anyway, I am now able to perform the manipulations as expected, even if I don't really understand what went wrong in the first place.
Thanks to everyone who took the time to repsond.
I know this was asked 6 years ago, but someone might be with the same problem, so here is a possible solution:
shell.AppActivate('Put_The_Name_Here')
The code above will select the program, file, etc that is opened but not activated, that, maybe, is your problem.
Hope this helps someone!

Categories