automating, "opening a windows app and logging in" with python; which module? - python

I'm trying to write a script that automates opening a windows software and then type the login information (username and password) to fully open said software. Ideally, I would like these actions to be automated to run daily at a certain time.
An example of such a software would be Skype but there are many other softwares that I want to try this one.
I'm looking for suggestions for which modules I should look into.
I have been browsing python automation tutorials on youtube any the only thing I came upon was the module Pywinauto. Most of the tutorials seem to be focused on opening notepad and using notepad specific commands, so I"m not sure how much freedom I have with other windows applications. I visited the Pywinauto offical site, but the homepage wasn't as detailed as I hoped.
I may plan to automate some more down the road ( executing some commands down after opening a program), so ideally I want a module that has a lot of flexiblity.
I'm looking for general suggestions and recommendations for modules.
Thanks!

for opening programs there is os.system and subprocess.call some people also use win32api.
import os
os.system('notepad.exe')
import win32api
win32api.WinExec('notepad.exe')
import subprocess
subprocess.Popen(['notepad.exe'])
subprocess is "cross-platform".
import subprocess
program = 'C:\\Program Files (x86)\\Skype\\Phone\\skype.exe'
subprocess.Popen(program)
this opens skype already, so the first step is done.
Next you want to interract with skype. win32api is windows only but ill use it for this example. If you save the login in skype, you can already save the login, press enter, put in the password and press enter again. We do this with keyboard commands. Here is a working example which opens skype and logs you in automatically
import subprocess
import win32api
import win32com.client
program = 'C:\\Program Files (x86)\\Skype\\Phone\\skype.exe'
subprocess.Popen(program)
shell = win32com.client.Dispatch("WScript.Shell")
win32api.Sleep(5000)
shell.SendKeys("{ENTER}")
win32api.Sleep(6000)
shell.SendKeys("tsadfdsgr")
shell.SendKeys("{ENTER}")

Related

Python automate open application

Please give me idea regarding how to tackle this problem. I am not able to find any resource regarding this. Your help will be immensely valuable. So we have one limited license software. And want to reiterate the python invoking the application. If the application gives the error that licence is not available it should close the application and wait for sometime say 1 min and again invoke the process, it should do so endlessly until a licence is available and the application is finally open.
I am able to open the application using
Import os
os.startfile('application executable')
After this I want the application to know if there is an error window popping , it should close the window and wait for sometime and again open the application
os.startfile returns as soon as the associated application is launch so use Popen instead.
As you are using windows use these steps.
To Open a Shortcut using Popen on Windows first install pywin32
Step one:
python -m pip install pywin32
Step two:
Navigate to your python Scrips folder something like
C:\Users\Name\AppData\Local\Programs\Python\Python38-32\Scripts then type the command.
pywin32_postinstall.py -install
Then the code to use Popen is.
import subprocess
import win32com.client, win32api
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(r'path to shortcut')
long_path = shortcut.Targetpath
p = subprocess.Popen(long_path)
p.wait()

Run Python script quickly via HotKey

I've created a simple script that executes a "moving mouse and keyboard" sequence. Although currently to get this to work I use a Shell (Idle) to run and that is to slow with boot up time and such.
Is there a way to have this python file on desktop och with a hotkey swiftly run the code? I tried doing it through terminal but it doesn't like my module.
Some info:
This is for both mac and windows.
The module I imported is pyautogui from PyPi and it works in the shell.
Thank you in advance!
Some things to consider when trying to setup a hotkey to successfully execute any kind of command:
Each Operating System has its own ways to setup hotkeys and sometimes this may differ between distributions as well as between desktop managers.
Many of the relevant how-to-descriptions are easily found via regular search machines as Google.
If you would in fact like your script to set-up its own hotkeys you would have to re-write it in such a manner that it can detect the current operating system/distribution/desktop manager by itself and execute the commands relevant to that particular set-up.

Can I open windows task scheduler with Python?

I've just been experimenting with python so that I can make life a little more continent for myself. I tend to be doing multiple things at a time and python has been extremely helpful with moving files and opening programs. I was just wondering if there is a way it can open the scheduler, and if you can bind it to a key?
With standard os library
import os
os.system('control schedtasks')
Or for more complicated options, with subprocess
import subprocess
p = subprocess.Popen("control schedtasks")
I would try something simple like this to open the scheduler:
import os
os.system("taskschd.msc")
Note that if you don't want the UAC popup to ask you for elevation whenever your script runs, your script itself will need to be run as admin (so that every sub-process that it executes, such as the Task Scheduler, will also subsequently be run as admin).
As far as binding the script to a key...you may honestly have the most luck using something like https://autohotkey.com/ to do so. Alternatively, you could make your script run at login, and then use a package like https://pypi.python.org/pypi/system_hotkey/1.0.3 to implement hotkey watchers into it (note that this is a Python 3 package).

How do I add a python script to the startup registry?

I'm trying to make my python script run upon startup but I get the error message windowserror access denied, but I should be able to make programs start upon boot because teamviewer ( a third-party program I downloaded ) runs every time I restart my computer so I know that I should be able to make my program run at startup (I might be doing something different though, so if you could shed some light on what teamviewer is doing differently to get its script to run at startup that would be helpful).
Here is my script
import _winreg, webbrowser
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,'Software\Microsoft\Windows\CurrentVersion\Run')
_winreg.SetValueEx(key,'pytest',0,_winreg.REG_BINARY,'C:\Users\"USERNAME"\Desktop\test.py')
key.Close()
webbrowser.open('www.youtube.com')
Any input is appreciated.
import webbrowser
webbrowser.open('www.youtube.com')
Get rid of all of that _winreg stuff. Instead, you (assuming double-clicking on a py file opens the console) should simply place it in your startup folder (C:\Users\yourusername\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup on Windows 7, and C:\Documents and Settings\yourusername\Start Menu\Programs\Startup in XP). This works because Windows tries to open all files in the startup folder, and if Python opens PYs by default, Windows will open the Python console. Try restarting, that should work.
Baboon:
I am a little late posting, but you seem to have left off the sam at the end of your code here.
When you open a key you need to add the user rights, if you do not _winreg defaults to "READ":
Here is a snippet from the python site
http://docs.python.org/2/library/_winreg.html#access-rights
sam is an integer that specifies an access mask that describes the desired security access for the key. Default is KEY_READ. See Access Rights for other allowed values.
Here is the code corrected:
import _winreg, webbrowser
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,'Software\Microsoft\Windows\CurrentVersion\Run',_winreg.KEY_SET_VALUE)
_winreg.SetValueEx(key,'pytest',0,_winreg.REG_BINARY,'C:\Users\"USERNAME"\Desktop\test.py')
key.Close()
webbrowser.open('www.youtube.com')

how to control the buttons or tabs in the launch application using python

i launched the application using the following code i.e.
import os
cmd = r'start C:\WindowsMediaPlayer\wmplayer.exe'
os.system(cmd)
say i launched win media player.. now i want to control the buttons/the menus to open a file . or do some audio settings.. any thing that is related to media player using the python script.. please any on can help me with this....
Maybe you should use a framework such as pywinauto
Using this, you can launch windows applications and control them.
Check this out

Categories

Resources