Struggle for 3 days to make up easy code

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MPS12232311111
Posts: 20
Joined: 13 Nov 2021, 07:18

Struggle for 3 days to make up easy code

25 Nov 2021, 10:12

Code: Select all

#SingleInstance, force
#persistent
#WinActivateForce
SetWorkingDir %A_ScriptDir%
activeICO := "active.ico"
TimerICO := "timer.ico"

a:
Menu,Tray,Icon,%activeICO%
Menu,Tray, NoStandard
Menu, Tray, Tip, Timer
DetectHiddenWindows, On
SETTIMER, settttimer, 50

settttimer:
sleep 100
if Winexist("ahk_exe firefox.exe")
{
    if A_TimeIdle > 0
   
    Loop
    {
        initSecs := 600
        Menu,Tray,Icon,%TimerICO%
        Gui, 99:+AlwaysOnTop	
        Gui, 99:font,s8 q4 bold,Verdana
        Gui, 99: +Caption -border -sysmenu +ToolWindow  +LastFound
        Gui, 99:add, text, x10 y5 w66 h17  vTX , %initSecs%
        Gui, Color, 000000
        WinSet, Transparent, 200
        Gui, 99:Show,x1185 y642, remaining
      
        loop % initSecs-1
        {		
            SETTIMER, ab, 500
            GuiControl,99:,TX,% Frmt(--initSecs)
            sleep, 1000
            SETTIMER, ab, 500
            if A_TimeIdle < 2000
            {
                check()
                Gui, 99:Destroy
                Menu,Tray,Icon,%activeICO%
                Gui, 99:Destroy
                goto, a
            }
            SETTIMER, ab, 500
        }
        Gui, 99:Destroy
       
        loop,5
        {
            if A_TimeIdle < 2000
            {
                Gui, 99:Destroy
                goto, a
            }
            SETTIMER, ab, 500
            Process, Close, firefox.exe
            goto, a
            return
        }
        Gui, 99:Destroy
        DetectHiddenWindows, on
        Return  
        Gui, 99:Destroy
        Frmt(secs) 
        {
            time = 20000101
            time += %secs%, seconds
            FormatTime, mmss, %time%, mm:ss
            return mmss
        }
    }
}
Hello🙃 I am trying to create auto-closing script when idle. I might forget about closing my browser (it happens really often😁)
And I want to create script that will start cutdown timer if idle.
if idle for 30 sec ⟶ I get notified that it has been idle for 30 sec
if 60, 2000, 600000 sec idle ⟶ I get notified that is has been idle for 60, 2000, 600000 correspondingly
But if I move it disappear
I cannot finirh the scipt. The Timer.ico don't change to active sometimes. And my attempts to create 60, 2000, 600000 sec notification without pausing the script failed.
I would be extremely happy if you helped me
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Struggle for 3 days to make up easy code

25 Nov 2021, 12:07

Where is ab:?
Where is check()?
Why do you define Frmt(secs) inside the loops?
Why do you use settttimer every 50 ms?
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
Rohwedder
Posts: 7555
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Struggle for 3 days to make up easy code

25 Nov 2021, 13:17

Hallo,
perhaps this helps:

Code: Select all

#InstallKeybdHook
#InstallMouseHook
SetTimer, T, 500
T:
secs =
IF WinExist("ahk_exe firefox.exe")
	For N, Time in [30,60,2000,600000]
		IF (A_TimeIdle/1000 > Time)
			secs := Time
IF (secsOld <> secs) And secs
	ToolTip,% "idle for " Frmt(secsOld := secs)
IF secs =
	ToolTip
While, (secs = 600000) And ID := WinExist("ahk_exe firefox.exe")
	WinClose, ahk_Id %ID%
Return
Frmt(secs)
{
	time = 20000101
	time += %secs%, seconds
	FormatTime, mmss, %time%, mm:ss
	return mmss
}
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Struggle for 3 days to make up easy code

25 Nov 2021, 14:22

@Rohwedder, nice script!

Btw, guys, are you aware of 600000 seconds = 1 week?
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
sofista
Posts: 645
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Struggle for 3 days to make up easy code

25 Nov 2021, 18:13

amateur+ wrote:
25 Nov 2021, 14:22
Btw, guys, are you aware of 600000 seconds = 1 week?
Close but not quite ;)

Code: Select all

Time := 600000
MsgBox, % ConvertTime(Time) ; 6d 22h 40m 00s

ConvertTime(Time) {
	seconds := format("{:02}", floor(mod(Time, 60)))
	minutes := format("{:02}", mod(floor(Time/60), 60))
	hours   := format("{:02}", mod(floor(Time/3600), 24))
	days    := floor(Time/86400)
	return days "d " hours "h " minutes "m " seconds "s"
}
Edited: corrected bug
MPS12232311111
Posts: 20
Joined: 13 Nov 2021, 07:18

Re: Struggle for 3 days to make up easy code

26 Nov 2021, 04:51

amateur+ wrote:
25 Nov 2021, 12:07
Where is ab:?
Where is check()?
Why do you define Frmt(secs) inside the loops?
Why do you use settttimer every 50 ms?
I am not quite good at AutoHotkey. I tried to put something to make code work🙃
MPS12232311111
Posts: 20
Joined: 13 Nov 2021, 07:18

Re: Struggle for 3 days to make up easy code

26 Nov 2021, 04:57

Rohwedder wrote:
25 Nov 2021, 13:17
Hallo,
perhaps this helps:

Code: Select all

#InstallKeybdHook
#InstallMouseHook
SetTimer, T, 500
T:
secs =
IF WinExist("ahk_exe firefox.exe")
	For N, Time in [30,60,2000,600000]
		IF (A_TimeIdle/1000 > Time)
			secs := Time
IF (secsOld <> secs) And secs
	ToolTip,% "idle for " Frmt(secsOld := secs)
IF secs =
	ToolTip
While, (secs = 600000) And ID := WinExist("ahk_exe firefox.exe")
	WinClose, ahk_Id %ID%
Return
Frmt(secs)
{
	time = 20000101
	time += %secs%, seconds
	FormatTime, mmss, %time%, mm:ss
	return mmss
}
Oh, year not 600 000 sec but 600 000 ms👽 Also how to move a little the idle alert? I have a tiny monitor. And the last question is how to make cut down timer work? I see only notification about idle
Rohwedder
Posts: 7555
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Struggle for 3 days to make up easy code

26 Nov 2021, 11:43

Oh, year not 600 000 sec but 600 000 ms.
Exchanging the value in the script should be feasible.

Also how to move a little the idle alert? I have a tiny monitor.
Replace the Tooltip with your gui, 99.

And the last question is how to make cut down timer work? I see only notification about idle.
I assumed the timer should run continuously. Only then it is ensured that the automatic Firefox closing works.
But, incorrect idle notifications indicate a bug that still needs to be fixed!
MPS12232311111
Posts: 20
Joined: 13 Nov 2021, 07:18

Re: Struggle for 3 days to make up easy code

27 Nov 2021, 05:39

Rohwedder wrote:
26 Nov 2021, 11:43
Oh, year not 600 000 sec but 600 000 ms.
Exchanging the value in the script should be feasible.

Also how to move a little the idle alert? I have a tiny monitor.
Replace the Tooltip with your gui, 99.

And the last question is how to make cut down timer work? I see only notification about idle.
I assumed the timer should run continuously. Only then it is ensured that the automatic Firefox closing works.
But, incorrect idle notifications indicate a bug that still needs to be fixed!
I cannot combine both gui, 99 and idle alert. As I need notification along with timer. I suppose I miss smth easy and struggle to complete this task
Rohwedder
Posts: 7555
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Struggle for 3 days to make up easy code

28 Nov 2021, 08:30

I took another look at your specifications and your unfortunately non-executable script.
(Never present a non-executable script unless it is about its non-executability!).
Now I tend to think you want a count down:

Code: Select all

#InstallKeybdHook
#InstallMouseHook
; When Firefox is open:
Start := 30 ; > 30 seconds of idle, Firefox remaining time is displayed
End := 600 ; > 600 seconds of idle, Firefox is closed
SetTimer, T, 500
T:
Idle := Round(A_TimeIdlePhysical/1000) ; Seconds
IF !WinExist("ahk_exe firefox.exe")	Or (Idle < Start)
	Gui, 99:Destroy
Else IF Idle between %Start% and %End%
{
	IF WinExist("ahk_id " Gui99)
		GuiControl, 99:, TX,% Frmt(End - Idle) " remaining"
	Else
	{
		Gui, 99:New, +HwndGui99
		Gui, 99:+AlwaysOnTop +LastFound -Caption -border -sysmenu +ToolWindow
		Gui, 99:Color, 000000
		WinSet, Transparent, 200
		Gui, 99:font, s12 q4 bold, Verdana
		Gui, 99:add, text, cWhite x10 y5 h17 vTX ,% Frmt(End - Idle) " remaining"
		Gui, 99:Show, x1185 y642
	}
}
Else While, WinExist("ahk_exe firefox.exe")
	WinClose,,, 0
Return
Frmt(secs)
{
	time = 20000101
	time += %secs%, seconds
	FormatTime, mmss, %time%, mm:ss
	return mmss
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: RussF, skeerrt, vmech and 143 guests