1

I have a program that is always open and every now and again I need to update some background files for it but then I need to hit a key in the program to refresh it. I'm looking for a way to automate that refresh once the updating script has finished.

What i need to do:

1 - go to the open program on my computer which is called Program

2 - press ctrl + shift + r to perform the refresh

This was a script that I have tried but no such luck from within Pycharm:

import pyautogui
from time import sleep
import subprocess

p = subprocess.Popen(r'C:\Users\xxx\Desktop\xxx\Program.exe')
pyautogui.keyDown("ctrl")
pyautogui.keyDown("shift")
pyautogui.press("r")
pyautogui.keyUp("ctrl")
pyautogui.keyUp("shift")
 

p.kill()

This script keeps me in pycharm and sends me to a find and replace task. Any ideas? thanks!

SOK
  • 1,732
  • 2
  • 15
  • 33
  • 1
    This is independent of PyCharm - PyCharm is merely an IDE and whatever solution you get would be the same in any regular Python IDE or editor. – Grismar Dec 19 '20 at 01:17
  • ok thanks - do you have any sugestions as to how i would solve this problem? Do i need to run it straight in a python console or soemthing like that – SOK Dec 19 '20 at 01:19
  • The problem is you'll need the window to get focus - I'm no expert on the library, but this post suggests that pyautogui can't help there, so you'll need to find another way to set focus to the spawned window. https://stackoverflow.com/questions/43785927/python-pyautogui-window-handle – Grismar Dec 19 '20 at 01:20

2 Answers2

1

There is a simple bypass to this, put your program to sleep for a few seconds till you focus on the window manually.

Here, I have tried this on StachOverflow and it worked!

import pyautogui
from time import sleep

sleep(5)
pyautogui.keyDown("ctrl")
pyautogui.keyDown("shift")
pyautogui.press("r")
pyautogui.keyUp("shift")
pyautogui.keyUp("ctrl")

After running the code, I switched to the tab and waited. Soon the webpage refreshed successfully!

Note: This is just a small bypass. I recommend you to look out on how to get other window in focus as suggested by Grismar

AwesomeSam
  • 153
  • 1
  • 16
  • 1
    Thanks very much, I actually found the answere here https://stackoverflow.com/questions/2090464/python-window-activation – SOK Dec 19 '20 at 07:35
0

The answer that worked for me is here to put the window in focus:

Python Window Activation

and then run the following:

pyautogui.keyDown("ctrl")
pyautogui.keyDown("shift")
pyautogui.press("r")
pyautogui.keyUp("ctrl")
pyautogui.keyUp("shift")
SOK
  • 1,732
  • 2
  • 15
  • 33