AutoIt script doesn't run - autoit

My AutoIt script should do a left click every 40 minutes inside a given time interval:
Func Main()
Run("kocske.jpg")
While 0 < 1
If CheckTime() == true Then
MouseClick("left")
EndIf
; Sleep for 40 minutes
Sleep(60000 * 40)
WEnd
EndFunc
; The function checks if the current time is between 17:00 and 20:00
Func CheckTime()
If #Hour >= 17 AND #Hour <= 20 Then
Return true
Else
Return false
EndIf
EndFunc
I saved it as .au3 file and compiled it to an executable. But when I run it, nothing happens (as if it never started).
I added Run("kocske.jpg") to test if the script starts at all, and placed a JPG file named "kocske.jpg" in the script's folder. It does not open the file, and the task manager does not show it running.
Why doesn't my script run?

I rewrote your program a bit to include usual habits (commented below)
Main() ; calls the Main() Function
Func Main()
ShellExecute("kocske.jpg") ; opens the image with it's default viewer; Note: you should add full path
While True
If CheckTime(17, 21) Then ; good habit to work with parameters; makes your function flexible
; probably you want to locate your mouse to a special location before clicking
; and also activate a certain application? Consider ControlClick()
MouseClick("left")
EndIf
Sleep(60000 * 40) ; Sleep for 40 minutes
WEnd
EndFunc ;==>Main
; The function checks if the current time is between 17:00 and 20:00 (19:59:59)
Func CheckTime($TimeA = 17, $TimeB = 20) ; defines default parameters, if they are not given
If #HOUR >= $TimeA And #HOUR < $TimeB Then Return True ; no Else needed
Return False
EndFunc ;==>CheckTime
Note: #HOUR < $TimeB instead of #HOUR <= $TimeB

Running functions
Why doesn't my script run?
Because functions are defined, but not called.
If you want Main() to be executed then add a line "Main()" outside of function definitions (global scope). Example (first line, as per Documentation - Keyword Reference - Func...Return...EndFunc):
Main()
Func Main()
Run("kocske.jpg")
While 0 < 1
If CheckTime() == true Then
MouseClick("left")
EndIf
; Sleep for 40 minutes
Sleep(60000 * 40)
WEnd
EndFunc
; The function checks if the current time is between 17:00 and 20:00
Func CheckTime()
If #Hour >= 17 AND #Hour <= 20 Then
Return true
Else
Return false
EndIf
EndFunc
Opening files
I added Run("kocske.jpg") to test if the script starts at all …
As per Documentation - Function Reference - Run():
Runs an external program.
"kocske.jpg" is not "an external program"; use ShellExecute("kocske.jpg") instead:
Runs an external program using the ShellExecute API.
Comparison operator
There is no differentiation for = -use between assignment and comparison (as per Documentation - Language Reference - Operators). Example:
; Equal sign (=) as assignment operator:
Global Const $g_bValue = True
; Equal sign (=) as comparison operator:
If $g_bValue = True Then; Or just: If $g_bValue Then
Beep(500, 1000)
EndIf
As per Documentation - Language Reference - Operators:
==Tests if two strings are equal. Case sensitive. The left and right values are converted to strings if they are not strings already. This operator should only be used if string comparisons need to be case sensitive.

Related

How to build a setInterval-like task?

This is my idea
task=#task begin
while true
sleep(1)
foo()
if should_end
# ?
end
end
end
But there are some problems
are there any simple ways to contact other than using global should_end?
how to end the task from inside the expression?
While implementing something like this can be a good exercise, note that Timers are in the standard library and they are similar to the JS functions you may be used to because Julia and Node.js both use libuv internally. Whether you use Node.js setInterval or Timeout, or Julia Timer eventually uv_timer_start is called (although the creation and low-level management of timers is different in the respective runtimes).
To answer your 2nd question, you can just break in the if expression:
julia> should_end = false
false
julia> task = #task begin
while true
sleep(1)
if should_end
break
end
end
"Tasks can return a value"
end
Task (runnable) #0x00007fca1c9c3990
julia> schedule(task)
Task (runnable) #0x00007fca1c9c3990
julia> task
Task (runnable) #0x00007fca1c9c3990
julia> should_end = true;sleep(1)
julia> task
Task (done) #0x00007fca1c9c3990
julia> fetch(task)
"Tasks can return a value"
As for the 1st question, there is a lot of information in the Julia docs, e.g. the Asynchronous Programming section. As is described there, Channels can be used for communication between tasks (when suitable). Note that should_end doesn't have to be global. A task wraps a function and that function can capture variables in enclosing scopes (#task begin a = 1 end is really just Task(() -> begin a = 1 end).
Here is a short example using a Timer and a Channel to send data to a task:
function pinger(n)
ping() = parse(Float64, match(r"time=(.*) ms", read(`ping -c 1 8.8.8.8`, String))[1])
ch = Channel{Float64}(0)
pinger = Timer(0.0; interval=1.0) do timer
put!(ch, ping())
end
summer = #task begin
total = 0.0
count = 0
while count < n
total += take!(ch)
count += 1
end
total / count
end
schedule(summer)
fetch(summer)
end
julia> pinger(3)
19.5

Gnat 2020 non-preemptive tasking windows

I am trying to achieve true non-preemptive tasking using gnat 2020 CE on a windows 10 environment. I have placed this in the gnat.adc file:
pragma Task_Dispatching_Policy(Non_Preemptive_FIFO_Within_Priorities);
Without this gnat.adc setting, the tasks switched back and forth a great deal, as you would expect with preemptive tasking. Putting the pragma in the gnat.adc file seemed to affect the granularity of the task switching on my test program below, in that it lengthened the time that each task executed consecutive loops, but they still switched over to each other eventually. Here is the test program:
with Ada.Text_IO; Use Ada.Text_Io;
procedure Test is
Task Type One is
End;
Task Type Two;
Task body One is
x : Integer := 0;
Begin
put_line("one");
Loop
x:=x+1;
if x > 10000000 Then
exit;
end if;
End Loop;
Put_line("Task one done, x=" & x'img);
End;
Task body Two is
x : Integer := 0;
Begin
put_line("two");
Loop
x:=x+1;
if x > 1000 Then
exit;
end if;
End Loop;
Put_line("Task two done, x=" & x'img);
End;
a : One;
B : two;
begin
Null;
End;
Here is the compile line:
gnatmake -gnat2012 -gnatX -f -g test.adb -gnatwA -I. -I.. -D obj
And here is the output:
one
two
Task two done, x= 1001
Task one done, x= 10000001
I expected the opposite, that task one would execute first, which it did, but that it would also finish first because there's no reason for it to yield to two without preemption. It looks to me like I am not actually getting non-preemptive tasking, and I would like to know why.
Thanks in advance.
edit
After looking at Jeffery's comment, I found the 2012 attribute 'with CPU', and produced test code :
With System.Multiprocessors; use System.Multiprocessors;
with Ada.Text_IO; Use Ada.Text_Io;
procedure Test is
Task Type One with Cpu=>1 is
End;
Task Type Two with Cpu=> 1 is
end;
x,y : integer := 0;
limit : integer := 1000000;
Task body One is
Begin
put_line("one");
Loop
if y > 0 then
raise tasking_error;
end if;
x:=x+1;
if x > limit Then
exit;
end if;
End Loop;
Put_line("Task one done, x=" & x'img);
Exception
When others =>
put_line("task one died, x=" & x'img);
End;
Task body Two is
Begin
put_line("two");
Loop
y:=y+1;
if y > limit Then
exit;
end if;
End Loop;
Put_line("Task two done, y=" & y'img);
Exception
When others =>
put_line("task two died");
End;
a : One;
B : two;
begin
put_line(Number_Of_CPUs'img & " cpu's");
While (x < limit+1) or (y < limit+1) loop
Delay 0.0;
End Loop;
put_line("main done, x " & x'img & " y " & y'img);
End;
which produces output
one
two
24 cpu's
task one died, x= 310528
Task two done, y= 1000001
^C
(of course, I have to ctl-c out since main never finishes.)
This happens whether or not I have the scheduling pragma in gnat.adc. Does Windows just not respect processor assignment and/or the scheduling pragma?

How can I access the current value of an environment variable in a zsh prexec function?

I have a simple function like
function clearit() {
REM=$(($LINENO % $LINES))
DIV=$(($LINENO / $LINES))
if [[ $DIV -gt 0 && $REM -lt 3 && $DIV ]]; then
clear
fi
echo $LINENO, $LINES
}
add-zsh-hook preexec clearit
In the above function a static value of $LINE and $LINES is added to the prehook function. I want to get the current value when the prexec is executed. How can I do that ?
For normal shell variables, you will get the current value:
% x=1
% function test_hook() { echo $x; }
% add-zsh-hook preexec test_hook
% true
1
% x=2
1
% true
2
However, $LINENO is a very special variable. From the documentation:
LINENO <S>
The line number of the current line within the current script, sourced file, or shell function being executed, whichever was started most recently. Note that in the case of shell functions the line number refers to the function as it appeared in the original definition, not necessarily as displayed by the functions builtin.
When your hook function is executing, it is by definition the most recently started shell function, script or sourced file. So $LINENO always gives you the current line within your hook function. There is no way to access the $LINENO for the outer scope.
However, even if you could access the outer $LINENO in your hook, it would lead to very strange behaviour in my opinion. Let's say your terminal is 30 lines high ($LINES is 30), it would clear the terminal before executing the 30th, 31st and 32nd command, then nothing for the next 27 commands, then again clearing before the 60th, 61st and 62nd, and so on. I don't think this is remotely what you are trying to achieve...

How to move the mouse every 3 minutes?

I am trying to move the mouse every 2 minutes so that the session doesn't time out. But despite no syntax errors, it doesn't work.
My code:
global $x = 1
global $y = 1
If Mod(#MIN, 3) = 0 Then
MouseMove (global $x, global $y, 2)
global $x++
global $y++
endif
Its more usefull to perform a callback function for timed calls.
AdlibRegister('_MouseMove', 2000*60) ; calls the function every 2000*60 ms
OnAutoItExitRegister('_UnRegister') ; unregister the callback function when the script ends
Func _MouseMove()
Local $aPos = MouseGetPos()
; move 1px right and back after a short brake - so that your interface can detect the movement
MouseMove($aPos[0]+1, $aPos[1])
Sleep(50)
MouseMove($aPos[0], $aPos[1])
EndFunc
Func _UnRegister()
AdlibUnRegister('_MouseMove')
EndFunc
Btw.: Increasing values with AutoIt works so
$x += 1
Edit:
I'm not sure, if you want 2 or 3 minutes (you've written both). So you can change it in the time parameter in AdlibRegister(). The interval must given in ms.
When you run your script in SciTE you should see the following error message:
You need the global keyword only when declaring a variable. When using a variable you have to ommit the global keyword. You should change your script accordingly and it might work then.
The following script move the mouse one pixel every 3 minutes, preventing the session time out, with minimal impact on the computer usage.
HotKeySet( "{ESC}" , "Sair")
While True
MouseMove(MouseGetPos(0)+1,MouseGetPos(1))
Sleep(180000)
MouseMove(MouseGetPos(0)-1,MouseGetPos(1))
Sleep(180000)
WEnd
Func Sair()
Exit
EndFunc

Why does not init:stop() terminate directly?

My code for display all days in this year.
I don't understand why if NewSec =< EndSec -> init:stop() end did not execute the first time in run_calendar?
I expect init:stop() could be executed first time but it is not.
What is wrong?
Code:
-module(cal).
-export([main/0]).
main() ->
StartSec = calendar:datetime_to_gregorian_seconds({{2009,1,1},{0,0,0}}),
EndSec = calendar:datetime_to_gregorian_seconds({{2009,12,31},{0,0,0}}),
run_calendar(StartSec,EndSec).
run_calendar(CurSec, EndSec) ->
{Date,_Time} = calendar:gregorian_seconds_to_datetime(CurSec),
io:format("~p~n", [Date]),
NewSec = CurSec + 60*60*24,
if NewSec =< EndSec -> init:stop() end,
run_calendar(NewSec, EndSec).
Result:
wk# erlc cal.erl
wk# erl -noshell -s cal main
{2009,1,1}
{2009,1,2}
{2009,1,3}
{2009,1,4}
{2009,1,5}
...
{2009,12,22}
{2009,12,23}
{2009,12,24}
{2009,12,25}
{2009,12,26}
{2009,12,27}
{2009,12,28}
{2009,12,29}
{2009,12,30}
{2009,12,31}
wk#
I believe that init:stop() is an asynchronous process that will attempt to shut down the runtime smoothly. According to the docs, "All applications are taken down smoothly, all code is unloaded, and all ports are closed before the system terminates."
It probably takes a while to actually stop, because you have an actively running process. If you change "init:stop()" to "exit(stop)", it will terminate immediately:
3> cal:main().
{2009,1,1}
** exception exit: stop
in function cal:run_calendar/2
Init:stop is asynchronous and it will take time to quit. An alternate way would be to wrap up the test in the call itself and use pattern matching to terminate the loop:
-module(cal).
-export([main/0]).
main() ->
StartSec = calendar:datetime_to_gregorian_seconds({{2009,1,1},{0,0,0}}),
EndSec = calendar:datetime_to_gregorian_seconds({{2009,12,31},{0,0,0}}),
run_calendar(false, StartSec, EndSec).
run_calendar(true, _StartSec, _EndSec) ->
finished;
run_calendar(false, CurSec, EndSec) ->
{Date,_Time} = calendar:gregorian_seconds_to_datetime(CurSec),
io:format("~p~n", [Date]),
NewSec = CurSec + 60*60*24,
run_calendar(NewSec =< EndSec, NewSec, EndSec).
(or something similar, hopefully you get the idea)
You have a mistake in your if statement
You said
if NewSec =< EndSec -> init:stop() end,
This is incorrect. You have to write something like:
if
A =< B -> do something ...;
true -> do something else
end
The if syntax is
if
Condition1 -> Actions1;
Condition2 -> Actions2;
...
end
One of these conditions must always be true.
Why is this?
Erlang is a functional language, not a statement language. In an functional
language every expression must have a value. if is an expression, so it must have a value.
The value of (if 2 > 1 -> 3 end) is 3 but what is the value of
(if 1 > 2 -> 3 end) - answer it has no value - but it must have a value
everything must have a value.
In a statement language everything is evaluated for its side effect -so this would
be a valid construction.
In Erlang you will generate an exception.
So your code generates an exception - which you don't trap so you don't see it and
init:stop() never gets called ...

Resources