I want to find out if I can launch a function or a method in a python program right before the computer sleep, something that goes along the line of
def f():
if about_to_sleep:
do_stuff
else:
do_some_other_stuff
that can work in windows 10
If you didn't mind writing a small C++ application to run in the background, it could call your python script when it detects the computer going into sleep.
Take a look at:
Qt detect when computer goes into sleep?
Detecting computer/program shutdown in Python?
This was a question similar to yours, but might still be relevant.
Related
I've been working on a small script in python to press some keys when I press one specific (I created a macro).
All works fine since I try to use it in any other application like for example a videogame or other programs.
The problem is that it only executes once, and then the only way it executes again is chainging to my IDE tab (Visual studio code) and returning back to the application, and this is quite annoying.
Any idea about it and how to solve that window problem? Here is the code
def macros():
t=threading.Timer(0.1,macros)
t.start()
if keyboard.is_pressed('k'):
keyboard.press('e')
keyboard.press('q')
print("active")
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 am new to both blender and python.
I tried to manipulate some properties of an object via python script in script console of blender.
What I don't understand is I can do it in this way.
bpy.data.object['Cube'].rotation_euler.x+=1
but when I put it in a loop.
import time
i=1
while i<100:
i+=1
bpy.data.object['Cube'].rotation_euler.x+=1
print('run once')
time.sleep(5)
Blender freezes without any output of 'run once'.
Would someone please tell me what is wrong with this code.
Your script isn't freezing, blender just isn't getting a chance to update during the loop.
The time.sleep(5) command sleeps for 5 seconds, being run 100 times means the script takes 8 minutes to run at which stage blender updates it's interface again.
You may want to look at a modal operator - there are several samples within the python templates available in blender's text editor.
I'm working on a script that grabs control of my mouse and runs within a simple infinite while loop.
def main():
while True:
do_mouse_stuff()
Because of the mouse control, it's a pain to click on the python window and hit ctrl-c, so I've been looking for a way to implement a global hotkey in windows. I'm also a relative Python noob so I've probably missed an obvious answer. Stuff I've found:
pyhk - the closest I've gotten, but this module does nasty things to my computer for some reason (probably something I'm doing wrong), it introduces major mouse lag, complete input lockout, all kinds of stuff I'm not smart enough to deal with.
pyHook - Followed the tutorial, works fine but the infinite running message pump and my while loop appear to run exclusively and I haven't figured out how to make it work.
Another Method - I found this method as well, but I have the same problem as pyHook, the try loop and my while loop cannot coexist.
I've tried to figure out how to integrate my loop into these examples rather than maintaining a separate loop but I've not been able to make that work, again likely due to my noobishness. Would someone be able to straighten me out on how to make this work?
Perhaps using msvcrt? I'm not sure if it's "global", and I can't test it right now, unfortunately, but here's an example of detecting the Escape key (taken from this question), integrated with your keyboard stuff:
import msvcrt
def main():
while True:
do_mouse_stuff()
# Check if `Esc` has been pressed
if msvcrt.kbhit() and msvcrt.getch() == chr(27).encode():
aborted = True
break
I'd like to know how to have a program wait for another program to finish a task. I'm not sure what I'd look for for that...
Also, I'm using a mac.
I'd like to use Python or perhaps even applescript (I could just osascript python if the solution if for applescript anyway)
Basically this program "MPEGstreamclip" converts videos, and it opens what appears to be 2 new windows while it's converting. One window is a conversion progress bar, and the other window is a preview of the conversion. (Not sure if these actually count as windows)
(Also, MPEGstreamclip does not have an applescript dictionary, so as far as I know, it can't listen for certain window names existence)
But basically I want my program to listen for when MPEGstreamclip is done, and then run its tasks.
If it helps, when the conversion is done, the mpegstreamclip icon in the dock bounces once. I'm not sure what that means but I'd think you could use that to trigger something couldn't you?
Thanks!
I realized GUI applescript was the answer in this scenario. With it I could tell the PROCESS to get every window, and that worked. However, I'm leaving this up because I'd like to know other ways. I'm sure this GUI workaround won't work for everything.
If the MPEGstreamclip actually ends when it is done, you could wrap the whole thing up in a python script using various techniques already discussed in another post. Just be sure to wait for the external process to end before continuing with your other steps.