TLDR; I have a python script that uses autopygui
to click on a file and open it, but it does not work with lnk
files located in the same path.
I have a little script using pyautogui
to screenshot a desktop icon and save it as a png image on the desktop and doubleclick
it to open that image. This works perfect.
However..
I tried implementing the same script on an existing Firefox.lnk
shortcut and it fails with the following error:
Things I have tried:
-
Running VS code as admin.
-
locateOnCenterScreen
andlocateOnScreen
. -
I have tried others, but I’m blanking because I have tried everything I can think of.
It should open my Firefox.lnk shortcut up, it does not. It only works on png file.
2
Answers
It’s because the
locateOnScreen(...)
function only accepts paths to image files, and .lnk files are not image files. If you manually took a screenshot of the Firefox icon on your desktop, saved it as a PNG file, and passed that to your script, it would open fine.If you want to launch a program using a .lnk file from Python, you don’t need pyautogui at all. Consider using
os.startfile(...)
instead.You should put the path of png file in
locateCenterOnScreen(...)
function instead.Please also make sure you minimize all active windows so that pyautogui could locate your Firefox short icon.