HotKeySet() issue - autoit

I cannot figure why this script does not work:
HotKeySet("^!k", "StartKodi")
StartKodi()
While 1
Sleep(200)
WEnd
Func StartKodi()
ShellExecute("C:\Program Files\Kodi\kodi.exe")
EndFunc
I would like to run the function only when the HotKeySet is pressed, But the HotKeySet does not start the function apparently....

The first line HotKeySet("^!k", "StartKodi") sets the hotkey CTRL + ALT + K to run the function StartKodi() and then the second line, StartKodi() directly runs the function StartKodi(), so if you had intended for the StartKodi() function to only run when the hotkey is pressed (and not when the script is run), then this would explain the unintended behavior.
I don't know how Kodi works, but some applications only allow one instance to be run at a time, so if you had intended to run Kodi once when the script is run and then also via the hotkey, this could explain why it would not run when the hotkey is pressed.

Related

Intercept python script completion

is there any way to intercept the completion of a python script, if so how can this be done? And before finishing use some kind of code? the script may end simply by closing to the cross or ctrl + c
I would recommend using a context manager.
class Context(object):
def __enter__(self):
pass
def __exit__(self, type, value, trace):
print("Ending")
with Context():
print("script")
This will run anything in the __exit__ function at the end of the program, even if it is terminated prematurely.
If you put something in the __enter__ function it will run at the beginning of the script.

AutoIt Scripting for an External CLI Program - eac3to.exe

I am attempting to design a front end GUI for a CLI program by the name of eac3to.exe. The problem as I see it is that this program sends all of it's output to a cmd window. This is giving me no end of trouble because I need to get a lot of this output into a GUI window. This sounds easy enough, but I am begining to wonder whether I have found one of AutoIt's limitations?
I can use the Run() function with a windows internal command such as Dir and then get the output into a variable with the AutoIt StdoutRead() function, but I just can't get the output from an external program such as eac3to.exe - it just doesn't seem to work whatever I do! Just for testing purposesI I don't even need to get the output to a a GUI window: just printing it with ConsoleWrite() is good enough as this proves that I was able to read it into a variable. So at this stage that's all I need to do - get the text (usually about 10 lines) that has been output to a cmd window by my external CLI program into a variable. Once I can do this the rest will be a lot easier. This is what I have been trying, but it never works:
Global $iPID = Run("C:\VIDEO_EDITING\eac3to\eac3to.exe","", #SW_SHOW)
Global $ScreenOutput = StdoutRead($iPID)
ConsoleWrite($ScreenOutput & #CRLF)
After running this script all I get from the consolWrite() is a blank line - not the text data that was output as a result of running eac3to.exe (running eac3to without any arguments just lists a screen of help text relating to all the commandline options), and that's what I am trying to get into a variable so that I can put it to use later in the program.
Before I suggest a solution let me just tell you that Autoit has one
of the best help files out there. Use it.
You are missing $STDOUT_CHILD = Provide a handle to the child's STDOUT stream.
Also, you can't just do RUN and immediately call stdoutRead. At what point did you give the app some time to do anything and actually print something back to the console?
You need to either use ProcessWaitClose and read the stream then or, you should read the stream in a loop. Simplest check would be to set a sleep between RUN and READ and see what happens.
#include <AutoItConstants.au3>
Global $iPID = Run("C:\VIDEO_EDITING\eac3to\eac3to.exe","", #SW_SHOW, $STDOUT_CHILD)
; Wait until the process has closed using the PID returned by Run.
ProcessWaitClose($iPID)
; Read the Stdout stream of the PID returned by Run. This can also be done in a while loop. Look at the example for StderrRead.
; If the proccess doesnt end when finished you need to put this inside of a loop.
Local $ScreenOutput = StdoutRead($iPID)
ConsoleWrite($ScreenOutput & #CRLF)

Can we pause the debugging process in R?

I wrote code which has been running for more than 12 hours and I run it by mtrace() and go() function (from the debug package). Now I want to pause this and go through the remaining process myself by pressing enter, is there a way to do that?
mtrace(main)
F<-main()
go()

R: I fail to pause my code

I am trying to pause my code for a little while, time for me to observe the plots.
I tried:
print('A')
something = readline("Press Enter")
print('B')
print('C')
, then there is no pause, the line print('B') is fed to readline and get stored into something and therefore only A and C got printed on the screen. Note that if I add an empty line between Something = readline("Press Enter") and print("B"), then print("B") get printed on the screen but still the console doesn't allow the user to press enter before continuing.
And I tried:
print('A')
Sys.sleep(3)
print('B')
print('C')
The program waits 3 seconds before starting and then run "normally" without doing any pause between print('A') and print('B').
What do I missunderstand?
Here is my R version: R 3.1.1 GUI 1.65 Snow Leopard build (6784)
The problem with readline is that if you paste your script into an R console, or execute it from eg Rstudio, the redline function is read and then the next line of the script is read in as the console entry, which in your case sets the value of something to print('B).
An easy way to get around this is to stick your entire code in a function, then call the function to run it. So, in your case:
myscript = function(){
print('A')
something = readline(prompt = "Press Enter")
print('B')
print('C')
}
myscript()
The output of this for me (in Rstudio, with R version 3.1.1):
[1] "A"
Press Enter
[1] "B"
[1] "C"
This has always felt like a bit of a hack to me, but it's essentially what the readline documentation recommends in its example.
I've never used sleep in my code, so I can't help you there.
Edit to clarify based on comments: This will only work if myscript() is the very last line of your script, or if it is manually entered into the console after running the script to generate the function. Otherwise, you will run into the same problem as before- the next line of code will be automatically entered.

Premature exit from script after fork call in Python (creating pipeline)

Code fragment inside call(argv) function
if '|' in argv:
# Split argv into two commands, lst[0] and lst[1]
r,w=os.pipe()
pid=fork()
# Parent
if pid >0:
os.close(w)
os.dup2(r,0)
run(lst[0])
os.close(r)
os.wait()
# Child
if pid==0:
os.close(r)
os.dup2(w,1)
run(lst[1])
os.close(w)
os._exit(1)
The code above gives the result from a simple pipeline of only two commands, but it causes the shell to exit prematurely. How can I get this code to stop exiting my script and have it return to command prompt?
How the program works
The child executes the second command. Its output is sent to the pipe by using the dup2() call to redirect the output along the pipe. This is accomplished through changing pipeline write file descriptor with the value sys.stdout.
The parent then uses input redirection with the dup2() call. This produces the final output which is then displayed on screen, but directly after the script exits.
The run function call takes in the command and its arguments. It executes the command given. It also runs globing and input and output redirection.
It's probably something simple, but I can't seem to spot what's causing the problem...

Resources