I would like to run my python script while I'm working on other things. For example, I want to run the python script while I'm watching a movie. But I don't want to exit full screen and go to the interactive shell and then go back.
I tried to create a batch file and run from the command line. But it's still not as neat and straight-forward as single-button functionality.
If I want to check the time while I'm watching a movie on hulu website.
python script:
import datetime
datetime.datetime.now()
I wonder if I can set a hotkey so that while I'm on other applications, I can just press the key and then python script will run itself in the background.
Try using keyboard module (allows us to assign custom shortcut keys):
import pyttsx3
import datetime
import keyboard
def time():
engine = pyttsx3.init()
hours = int(datetime.datetime.now().time().hour)%12
if not hours:
hours = 12
engine.say("the time is " + str(hours) + " " + str(datetime.datetime.now().time().minute))
engine.runAndWait()
def keyPress():
keyboard.add_hotkey('+', lambda: time())
keyboard.wait()
keyPress()
WHAT THIS PROGRAM DOES:-
This program a first creates a custom shortcut (assigns it to keyboard key +(change this to whatever hotkey combination you want)). Upon pressing this key, a function called time() gets called, which makes use of windows TTS, to tell your the current time (via voice functionality like siri, alisa etc).
HOW DOES THIS WORK:-
I made use of external libraries keyboard(necessary) and pyttsx3(optional), where the module keyboard allows us to define our custom hotkeys and create a event handler for keyboard events, and pyttsx3 makes use of windows Text to Speech feature to read some text via speech (aka voice). I made use of voice functionality, to not interrupt your flow while your are watching something online.
HOW TO SET IT UP:-
Copy the code, and save it as a .pyw file (notice the w in the
end). The difference between a regular .py an a .pyw file is
that, .py invokes console (~commandline) equivalent tool for
execution and executes in foreground, on the other hand a .pyw
file runs in the background at all times (i.e the console won't show
up). So it won't require you to manually open the console at all
times for executing the script as the script will be running in the
background at all time.
Copy this file (example.pyw) in the startup folder of your OS (any
file in this folder will automatically execute once the operating
system has started). What this will do is when your OS, will boot
your example.pyw file will automatically start executing in the
background, so you won't have to manually launch it at every system
startup. And since it is in the background it won't interfere with
your work. (if you're on windows OS, you can access your startup folder by typing shell:common startup on your run and pressing enter) (adding files to this folder requires root privileges)
HOW TO USE IT:-
While using your system press + (for my case), and your OS will tell you what the current time is. (though it requires your OS to have correct time)
PROS:-
Upon using this for long time I can tell you I never encountered a
single application in which the custom defined hotkey won't work.
Slim to None time required after keypress, for command execution.
A lot resistant to spam of hotkey's. e.x. You may press the
hotkey 100 times but the calls won't be made unless the previous
command has completed execution.
CONS:-
Calling the script for the first time after Bootup, may cause a
little latency in command execution (but only for once, i.e
subsequent calls would be really fast)
P.S:-
It is optional to use pyttsx3, I used it as it makes checking time a lot more easier then reading text or something (at least for me).
Secondly, this process could be made more efficient if you want.
RECOMMENDATIONS:-
Alternatively, If you are familiar with AHK (AutoHotKey) then, doing what i just told on a .ahk script would be a piece of cake, and it's scripts are even faster than my Python one. (Honestly I would definitely Recommend you to use AutoHotKey as it is a really robust language, when it comes to make efficient Operating System scripts)
Related
my software uses the subprocess.call([sys.executable, SCRIPT_NAME] instruction in order to open others kind of scripts specified by the user using a GUI (Tkinter). I have two issue with this instruction:
the "command line" scripts start and close themeselves quicly and it means that the user can't interact with them. it's a weird behaviour because in all of them there is an input instruction, so they should wait an input by the user before to close themeselves. how can I solve this issue?
the "GUI" scripts instead, start without any kind of issue, but their "life", let me say, put in stuck the main script (it uses Tkinter). in this case I can interact with the second script but not with the main one. how can I call my other scripts with the subprocess.call() function whithout put in stuck the main one? from my point of view this issue happens because the second script is a part of the same process of the main one and in this case Tkinter has to wait. if we open the others scripts using different processes for all of them the main script would be free to live its life independently of the others. but how can I do it?
So I have a sikuli script running which monitors and executes a said action every 10 minutes continuously. However for various reasons sometimes the run is interrupted and there is no way to alert if the script stops running.
So I tried running a python script, which would monitor the window of the sikuli IDE. When the script runs, the window is no longer visible. So if the window is visible again the python script would run a batch file which would trigger the alert required. The following is the script which I made from seeing other examples here in this site:
WindowName = "SikulixIDE 1.1.3 - C:\\Users\\TestUser\\Downloads\\testing2.sikuli"
while True:
try:
if win32ui.FindWindow(None, WindowName):
subprocess.call([r'C:\Users\TestUser\Documents\notification.bat'])
break
except win32ui.error:
#print("its not running!")
continue
The problem I am running into with the above code is, even when the sikuli script is running and the IDE window (one with the WindowName) is not actually visible to me, it still finds it and goes into the if block. I am not sure what's going wrong here, if the window is not visible in Task Manager, FindWindow shouldn't be able to find it, correct?
I would say you are trying to do this in very complicated way.
I will strongly suggest to look at '-r' option of sikuli and to write only one sikuli script that will handle exceptions by itself and will not need monitoring.
In this case if you really need additional monitoring for the script it will be easier since you can look for process with specific command line(instead of visible window)
I wrote a script using pyautogui that should start an program (an IDE) and then start using it.
This is the script so far:
#! python3
# mouseNow.py - Displays the mouse cursor's current position.
import pyautogui, sys, subprocess
from time import sleep
x,y = 1100,550
subprocess.call([r'C:\...exe', arg1, arg2])
pyautogui.click(x,y)
sleep(5) # 2 sec should suffice but this is for safety
pyautogui.typewrite(my_string)
pyautogui.press('enter')
This works well but I want to be portable. The x,y values were determined by where the program prompt appears on screen after I start the program, but this is not portable, I think. Is there a way to point the mouse to the prompt without giving const parameters? something like move_mouse_to_window_of_this_process_after_starting_it()
Also, I use sleep() so I would write the data to the window after it appears, but I guess it's not a good way (some PC will run this much slower, I guess), so is there a way to know when the prompt appeared and then do the pyautogui.typewrite(my_string)?
EDIT: I found a simple solution for the move_mouse_to_window_of_this_process_after_starting_it()
:
>>> pyautogui.hotkey('alt', 'tab')
If you need portable and reliable solution, you have to find a library that supports accessibility technologies to access GUI elements by text. Basic technologies are:
Win32 API, MS UI Automation (Windows)
AT-SPI (Linux)
Apple Accessibility API (MacOS)
There are several open-source GUI automation libraries supporting some of these technologies (usually 1 or 2). Python solutions:
pywinauto on Windows (both Win32 API & MS UIA, see Getting Started Guide)
pyatspi2 on Linux
pyatom on MacOS
There is also a thread on StackOverflow regarding hard sleeps vs flexible waiting.
Enjoy! :)
The way you are interacting with the .exe excludes alternatives to coordinates or blind firing (Tab, Tab, Enter etc..).
If the application has an API, you could interact with it programatically.
If it doesn't you can only try to match the location for x screen resolutions, and this only if the GUI is used in Fullscreen/windowed Fullscreen.
OS = windows 7
I have a python program (works) that is listening to activity on the usb bus. I want to perform a lot of tests that require a particular user input at a particular time. I would like to pop up a window that says, "press button xxx". The key point is that the mainloop needs to continue running because it's looking for events. I don't care about the window or if it remains or not and I don't need to capture any information from the window. I just want a message to the user to press the correct button at the correct time. Any type of signaling would work; it doesn't have to be a gui window. It doesn't have to look pretty. Appreciate any suggestions or links to something like this. thx
It sounds like the operation of the Python script you're running does not depend upon the user input you request. To run another process without interrupting the Python script execution you can use:
import subprocess
subprocess.Popen([exe,arg1,arg2,arg3])
where
exe = executable/script to run from your OS command line
arg1= first argument to pass to exe
arg2= second argument to pass to exe
etc... (as many arguments as your OS supports in a list)
This separate exe process could request input from the user.
I'm using a Python (3.3) script to run a command that will render a whole bunch of image files for an animation film.
While it's rendering, it creates an empty image file every time it starts a new frame of the animation, and when it's done rendering that frame, it saves the image over that empty file and moves onto the next frame.
The python script starts this rendering process by running another script using:
proc = subprocess.Popen(cmd, shell=True)
Is it at all possible to check which files were created during this process?
The reason being, someone may wish to stop the render before it finishes, but when they do so there will be an empty image file left over. I'd like to have the script delete the empty images when the big red Stop Render button is pushed, but I cannot simply delete all empty images because there may be other computers rendering - the empty images are there to prevent multiple computers rendering the same frame (they skip that frame if the image file for it exists).
If it helps, all the computers use Windows 7, and the animation program that does the rendering is Maya (2014).
If there's no easy way, I'd be happy to use any kind of ugly hack to get the job done :)
If you are doing this in Maya, you can use mayapy interpeter that comes with Maya (rather than mayaBatch or maya -r). Inside of standalone you can run any script(s) you need to setup the render, execute it, and post-process the results. At a minimum, that will let you handle one frame at a time and use python to submit the images one at a time.
A basic setup would be:
Create a python script that does what you need to do and works correctly inside the maya python interpeter. The script will be run as if it were in the maya listener, so you can import maya.cmds and use maya features as needed.
If the script needs command line arguments, use sys.argv to retrieve them. When you run the script, any arguments you pass in will be available in sys.argv.
Your script can get access to all of Maya's functionality by importing maya.standalone and calling it's initialize method (more details here)
From the command line, run the mayapy.exe that lives in the bin folder of your maya install. The -s flag allows you to specify a script to run, so you'll pass in the python script as an argument, followed by any other arguments you need: something like "path/to/mayapy.exe" -s "path/to/yourscript.py" arg1 arg2 arg3.
You won't be able to catch a user cancelling via control-c but you could request cancellation from outside using a TCP connection to the maya command port, or even just by prompting for user input after each frame
For a fancier operation you can run an rpc server inside your Maya using something like ZeroMQ or RpyC. You could also make your own with a WSGI server listening to the http port.
You can also look in to 3d party render management systems. There are some open-source ones here