How to read the variable value in AHK? - autohotkey

I want AHK to do something, ONLY if a particular variable has a particular value.
Code:
<^<!/::
MsgBox,4,Timer,Do you want to set a timer?
IfMsgBox Yes
{
InputBox,Time1 , Timer, What hour? (Please show in 24:00 format)
InputBox,Time2,Timer, What minute?
IfTime1 := A_Hour, Time2 := A_Min
MsgBox,64,Timer,Timer done.
}
else{}
return
If you don‘t understand what i am trying to do, I‘m trying to set a timer.
What i want:
When I press ctrl+alt+/, I want an msgbox to open, asking to set a timer or not.
If I click on yes, I want two input boxes open one by one, in which i will write the time.
Next, when i am done setting the time, the input box will close. Now, The time in the input box will write to a variable named time and time2. Then, if the time1 is same as A_Hour and time2 is same as A_Min, An MsgBox will show up with an info icon.
What happens:
After setting the time, the msgbox with an info icon immediately shows up.
Can anyone correct the code above and send an answer? Thanks.

<^<!/::
MsgBox,4,Timer,Do you want to set a timer?
IfMsgBox Yes
{
InputBox, Time1, hour, What hour? (Please show in 24:00 format)
if ErrorLevel ; CANCEL was pressed
return
if (Time1 > 23)
{
MsgBox, wrong hour specification
return
}
InputBox,Time2, minute, What minute?
if ErrorLevel
return
if (Time2 > 59)
{
MsgBox, wrong minute specification
return
}
SetTimer, Timer, 1000
}
return
Timer:
if (A_Hour = Time1 && A_Min = Time2)
{
SetTimer, Timer, off
MsgBox,64,Timer,Timer done.
}
return

Related

How to repeat triggering a script in AutoHotkey by holding down a key?

I am trying to write a script in autohotkey so that, when I hold i for every one seconds it trigger a specific script for each of the one seconds. for example when I hold this key for 7 seconds something be done 7 times, or when I hold it for 8.3 seconds it to the same thing 8 times. and when I just press that key ( which takes less than one second) it type that letter.
$i::
KeyWait,i,T0.25
If (ErrorLevel)
{
Click, 768,192,10
KeyWait,i
}
Else
{
Send, i
}
return
By running this script it just do the process once when I hold the key more than one seconds and to do it again I should hold the button again. I'm not sure how to achieve my goal. I added Loop{} but the result was typing " i " repeatedly.
You were fairly close:
$i::
KeyWait, i, T1
If (ErrorLevel) {
Loop 10
Click, 768,192
} else {
Send, i
}
return

Timer drags a bit when too much is happening

I'm working on a timer that needs to do some calculations and run some functions at a certain interval. I'm trying to keep the interval as big as possible, but I need it to be kind of fine grained.
Here is the periodic timer with some of the stuff that needs to happen.
So as you can see, every second (the milliseconds passed % 1000 == 0) it will do some stuff if some conditions are met. But also every 10 milliseconds I need to check some stuff.
It seems this is a bit too much, and after running the timer for 2 minutes it already drags 1 second behind. I guess I'm approaching this the wrong way. Could/should I somehow put all that logic in a function that just runs async so the timer can just keep going.
It's not the end of the world if the timer display drags for a few milliseconds every now and then, if it catches up later. But now the whole timer just drags.
_timer = Timer.periodic(Duration(milliseconds: 10), (timer) {
passedMilliseconds = passedMilliseconds + 10;
// METRONOME BEEP AND BLINK
if (passedMilliseconds % currentTimerSettingsObject.milliSecondDivider == 0) {
_playMetronomeBeep();
_startMetronomeBlink();
}
// ONE SECOND
if (passedMilliseconds % 1000 == 0) {
secondsDuration--;
// COUNTDOWN
if (secondsDuration < currentTimerSettingsObject.countDown + 1) {
_player.play('sounds/beep.mp3');
}
// SET COUNTDOWN START VALUES
if (secondsDuration == currentTimerSettingsObject.countDown) {
isCountdown = true;
}
notifyListeners();
}
// TIME IS UP
if (secondsDuration < 0) {
switchToNextTimer();
notifyListeners();
}
});
}
You cannot rely on a timer to deliver events exactly on time. You need to use a more exact method than simply incrementing a counter by 10 on every tick. One example would be to start a Stopwatch before the timer and then (knowing that your ticks will only be on approximately 10ms intervals) read stopwatch.elapsedMilliseconds and base your decisions on that.
You will need to change your logic a bit. For example, you want to know when you pass a 1 second boundary. Previously, with your exact increments of 10 you knew you would eventually reach a round 1000. Now, you might see 995 followed by 1006, and need to deduce that you've crossed a second boundary to run your per second logic.

Botman typesAndWaits adds initial delay

When I use typesAndWaits method with 2 seconds delay, I have to wait for 2 seconds in order to typing indicator display and when it is displayed I need to wait for another 2 seconds to display message.
I'm using BotMan chatbot service.
Here is my code:
$botman->hears('test', function (BotMan $bot) {
$bot->typesAndWaits(2);
$bot->reply('Prvo upiši svoje ime i prezime, i pritisnite SEND');
}
In the BotMan.php file, go to line 583 and replace
sleep($seconds);
With
usleep($seconds * 1000000);
It should work fine then...

Command button enable/disable based on textbox value

I am trying to enable/disable a command button based on the value of a textbox. Ex. "08-09-2015 15:06:24", taken from a table column field
It seems that it will either enable OR disable it, depending on < or >.
I want it to find out if txt.Value is MORE than 15 hours ago, then it should activate the button. If not, leave it "false"
The textbox and command button are on the same form.
This is what I have so far, and apparently not working.
Public Sub Kommandoknap184_Click()
If Me.txtOpdTid.Value < DateAdd("h", -15, Date) Then
Kommandoknap35.Enabled = False
Else
Kommandoknap35.Enabled = True
End If
End Sub
Date() gives you the current date with midnight as the time. Now() gives you the current date and time. So I think you want Now() instead of Date().
Public Sub Kommandoknap184_Click()
If Me.txtOpdTid.Value < DateAdd("h", -15, Now) Then
' Value is MORE than 15 hours ago, then it should activate the button
Me.Kommandoknap35.Enabled = True
Else
Me.Kommandoknap35.Enabled = False
End If
End Sub
It looks like there are two problems with your code.
In the DateAdd function you want to use Now() instead of Date(). Now() will include the current time along with the date. This is important since you are comparing the number of hours and not the number of days.
The enable/disable logic is backwards (enabling the button when it should be disabled and vice-versa).
Below is a simplified version with the corrections:
Public Sub Kommandoknap184_Click()
Dim isMoreThan15HoursAgo As Boolean
isMoreThan15HoursAgo = Me.txtOpdTid < DateAdd("h", -15, Now)
Me.Kommandoknap35.Enabled = isMoreThan15HoursAgo
End Sub
You might also consider having this code run in the After Update event of the text box instead of when clicking a separate button. This would make the enable/disable of the button more seamless.

AppleScript (or swift) add hours to time

I'm trying to add 8Hours to a date (from the clipboard)
set date1 to the clipboard
set newdate to date1 + (8 * hours)
display dialog "Purchases were downloaded at " & newdate buttons {"OK"} default button 1
But this is not working as expected, I'm having the error
Can’t make "06/22/2015 08:15:27 " into type number.
You need to pick the hours as an individual variable, like shown below:
set currentDate to current date
set newHour to ((hours of currentDate) + 8)
You can also use this for days, minutes and seconds.
This will work. You can then use the variables to construct a new date to be used in the display dialog.
PS. Don't forget to change the day if the newHour variable is bigger than 24 hours.
EDIT
Setting a date to the clipboard can be done like this:
set currentDate to current date
set the clipboard to currentDate as Unicode text
Getting the current clipboard and adding it to a variable goes like this:
set currentDate to get the clipboard
display dialog currentDate
I hope this helps!
In Swift, you can call dateByAddingTimeInterval on an NSDate object.
The time interval is measured in seconds.
yourDate.dateByAddingTimeInterval(8 * 60 * 60)
If you wanted to add another method to add 8 hours directly, you could define an extension:
extension NSDate {
func addEightHours() -> NSDate {
return self.dateByAddingTimeInterval(8 * 60 * 60)
}
}