How to run python script after logging in? - python

So i've done this program that helps me to manage windows and applications that i wanna have opened after i log in , and i realized that if i put this script to startup folder it'll start the program before i even login, and that's not what i want because the program depends on the time between starting application and pressing keyboard shortcut.I need the program to start after i login.I'm using pycharm with python 3.8 . This is the code i wanna run after i Login.
import os
import time
import pyautogui
os.startfile("C:\\Program Files\\JetBrains\\PyCharm Community Edition
2019.3.2\\bin\\pycharm64.exe")
time.sleep(2)
os.startfile('C:\\Users\\Igor\\AppData\\Local\\Programs\\Opera\\launcher.exe')
time.sleep(25)
pyautogui.keyDown('ctrl')
time.sleep(0.2)
pyautogui.keyDown('win')
time.sleep(0.2)
pyautogui.keyDown('down')
time.sleep(0.5)
pyautogui.keyUp('ctrl')
time.sleep(0.1)
pyautogui.keyUp('win')
time.sleep(0.1)
pyautogui.keyUp('down')
pyautogui.press('enter')
I've already looked up the same question on stack-overflow but there was no exact answer.

Take a look at this SuperUser answer. Maybe Task Scheduler has what you're looking for?
You can adjust the trigger so that it's not every time you unlock your machine but instead only when you log on.

Related

Using Python Script to run an Alteryx workflow via the Microsoft Task Scheduler on Remote Desktop

my python script works perfectly fine as long as I'm logged into the remote desktop while the task scheduler initiates it. However, if I'm no longer connected to the remote desktop, it terminates because pyautogui has a Failsafe. When I turn the failsafe to false, the whole workflow just doesn't run at all (no matter where in the workflow I put it).
My goal is for this to run on remote desktop while I don't have to be logged onto it.
Current path:
Windows Task Scheduler
Runs Run_Alteryx_Workflow.bat
Runs Run_Alteryx_Workflow.py
Runs Alteryx_Workflow.yxmd
Code for Run_Alteryx_Workflow.py:
import pyautogui as pg
import os
import time
# pg.moveTo(800, 800, 1.5)
# pg.FAILSAFE = False
time.sleep(10)
# OPEN ALTERYX
os.startfile(r'C:\Users\eXXXXXX\Desktop\Alteryx_Workflow.yxmd')
time.sleep(30) #build in enough time for Alteryx to open the workflow
# RUN ALTERYX WORKFLOW
pg.hotkey('ctrl', 'r')
# time.sleep(300)
time.sleep(100)
# CLOSE ALTERYX
pg.hotkey('alt', 'F4')
Code for Batch File (Run_Alteryx_Workflow.bat):
C:\Users\eXXXXXX\Desktop>"python" "C:\Users\eXXXXXX\Desktop\Run_Alteryx_Workflow.py"
Thank you for your help! This is my first post to StackOverflow!
Much of my code is commented out as I've tested things.
My biggest issue is running it while I'm not logged into my remote desktop!
You can't automate Alteryx like this, it just doesn't work.
You need to purchase Desktop Automation.

How to simulate the run button with a command line in Pycharm

I'm working on a script using the PyAutoGUI module. Sometimes the script gets stuck in a while loop because it's looking for pictures/images that are not shown due to connection problems etc. If this happens I want the program to start again from zero, so I want to simulate the play/run-button in Pycharm with a command line. Is this possible?
What it sounds like you want to do is restart your program if it doesnt respond. A similiar question appears to have been posted here: Python help - Need the ability to restart the script when it hangs or automatically set a timer so I would reccommend having a look at that. Simulating the run button in Pycharm might seem like a good idea at first but it is very specific and a bad practice to simulate user actions like that unless there is absolutely no viable alternative.
Thanks to Patel I managed to solve my issue. You can use ctrl+f5 to restart a script so now when I'm stuck in a while loop I'm using this code:
#Click toolbar Pycharm
pyautogui.moveTo(1672,15,1)
pyautogui.click()
#Rerun the script
pyautogui.hotkey("ctrl","f5")

why does subprocess launch an "extra" application?

I wrote a Python script to detect a Print Screen keypress, and launch the Snipping Tool. The script uses subprocess.call to handle the application launch.
The problem I am facing is that when I am done with Snipping Tool and close the application, I get an "extra" application being launched. For example, if I took a snip of a PowerPoint presentation, then when I close Snipping Tool I get a new / blank instance of Power Point being launched automatically. I do not want this to happen, and just want the Snipping Tool to close without any additional action.
Can someone please help explain what I am missing in my code?
# https://stackoverflow.com/questions/24072790/detect-key-press-in-python
# https://pypi.org/project/keyboard/
# https://github.com/boppreh/keyboard#api
import keyboard #pip install keyboard
import time
import subprocess
while True:
if keyboard.is_pressed('print screen'):
subprocess.call(r'SnippingTool.exe') # blocking; waits until open
keyboard.press_and_release('ctrl+N')
#elif keyboard.is_pressed('ctrl+print screen'): # not recognizing "print screen" here
elif keyboard.is_pressed('ctrl+esc'):
print 'killing it now'
break
else:
time.sleep(0.1)
I'm guessing (and I'm not on Windows), but I think that the subprocess.call waits until you have finished with Snipping Tool, and so the keyboard.press_and_release('ctrl+N') is going to PowerPoint.

Python time.sleep interrupted after click on terminal

I am building a command line tools using Python script. it's a loop to check data and print out some stuff after some delay seconds. It works fine until I click anything or selecting text by mouse on the terminal without keyboard event. it doesn't do anything after that, doesn't print and recheck
import time
import sys
print('some thing')
for remaining in range(10, 0, -1):
sys.stdout.write("\r")
sys.stdout.write("recheck in {:2d}.".format(remaining))
sys.stdout.flush()
time.sleep(1)
sys.stdout.write("\rComplete! \n")
input()
My environment is anaconda 64bit on windows 10
The console is blocking in the Windows SDK function WriteConsole because the console window is in a mode called QuickEdit mode.
To fix the issue, go to the properties option in the upper left corner menu of the console.
Then uncheck QuickEdit mode.
QuickEdit mode is there to help with copying and pasting text from the console. So when the console is in that mode, it stops all writing to the console so that the text isn't moving while you are trying to select and copy/paste.
Python significantly changed its system signal handling in Python 3.5. https://www.python.org/dev/peps/pep-0475/
It used to throw an InterruptedError whenever a signal interrupted a system call. Now the system call wrapper code upon signal interruption will recall the system call recalculating any timeouts if necessary. A bug at this level could recall the system call with an absurdly long value.
Attach a debugger and see where the process is at when it is stuck.
EDIT: after attaching windbg to stuck console. I discovered that this isn't the problem. I posted the real solution in a new answer.

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.

Categories