Temp AHK file does not get deleted and checks KeyState only once Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
A Keymaker
Posts: 457
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Temp AHK file does not get deleted and checks KeyState only once

06 Aug 2023, 15:44

This script

Code: Select all

#SingleInstance, Force

Code_For_The_Temporary_File =
(
#Persistent
SetTimer, Check_For_Esc_Key_Being_Pressed, 50
Return

Check_For_Esc_Key_Being_Pressed:
KeyState := GetKeyState("q", "P")
if (KeyState)
    {
        SetTitleMatchMode, 2
        WinClose, TEST TITLE ahk_exe cmd.exe

        FileDelete, , %Name_Of_The_Temporary_File%

        ExitApp
    }
Return

)

Name_Of_The_Temporary_File := "Close CMD Window Entitled  ''TEST TITLE'' When Pressing Of ''q'' Is Detected.ahk"
FileAppend, %Code_For_The_Temporary_File%, *%Name_Of_The_Temporary_File%

Run, %Name_Of_The_Temporary_File%
correctly closes a CMD window entitled TEST TITLE if I press q [and still also writes letter q - which is needed for some part of a larger script of mine]. But the problem with it is that:


#1] It only closes one such window, leaving other CMD TEST TITLE windows opened


#2] It does not delete the temporary Name_Of_The_Temporary_File.ahk file


#3] If there already is a Name_Of_The_Temporary_File file present in the folder with the mother script when I execute it then I get a pop-up message saying
---------------------------
Close CMD Window Entitled ''TEST TITLE'' When Pressing Of ''q'' Is Detected.ahk
---------------------------
Error at line 21.

Line Text: Check_For_Esc_Key_Being_Pressed
Error: Duplicate label.


I thought I could easily work around issues #2 and #3 with such version

Code: Select all

#SingleInstance, Force

FileDelete, , %Name_Of_The_Temporary_File%
Sleep 1000

Code_For_The_Temporary_File =
(
#Persistent
SetTimer, Check_For_Esc_Key_Being_Pressed, 50
Return

Check_For_Esc_Key_Being_Pressed:
KeyState := GetKeyState("q", "P")
if (KeyState)
    {
        SetTitleMatchMode, 2
        WinClose, TEST TITLE ahk_exe cmd.exe
    }
Return

)

Name_Of_The_Temporary_File := "Close CMD Window Entitled  ''TEST TITLE'' When Pressing Of ''q'' Is Detected.ahk"
FileAppend, %Code_For_The_Temporary_File%, *%Name_Of_The_Temporary_File%

Run, %Name_Of_The_Temporary_File%
but the temp file still does not get deleted and I get the same message [but with Error at line 17]. And that is because:


A] The Close CMD Window Entitled ''TEST TITLE'' When Pressing Of ''q'' Is Detected.ahk file is still not being deleted


B] The mother script adds content of Code_For_The_Temporary_File variable to the content of the Close CMD Window Entitled ''TEST TITLE'' When Pressing Of ''q'' Is Detected.ahk file instead of overwriting that whole content
Last edited by A Keymaker on 13 Aug 2023, 11:56, edited 2 times in total.
Master_X
Posts: 33
Joined: 04 Aug 2023, 13:59

Re: Temp AHK file does not get deleted and checks KeyState only once

06 Aug 2023, 17:46

For your pressing q closing all windows. Does this work? I did not test

Code: Select all

SetTitleMatchMode, 2

~q::
	Loop
	{	WinClose, TEST TITLE ahk_exe cmd.exe
		if not WinExist, TEST TITLE ahk_exe cmd.exe
		break
	}
	FileDelete, , %Name_Of_The_Temporary_File%
	ExitApp
Master_X
Posts: 33
Joined: 04 Aug 2023, 13:59

Re: Temp AHK file does not get deleted and checks KeyState only once

06 Aug 2023, 18:47

Does this work as you wish?

Code: Select all

#SingleInstance, Force

Code_For_The_Temporary_File =
(
#Persistent
SetTitleMatchMode, 2

~q::
	Loop
	{	if WinExist("TEST TITLE ahk_exe cmd.exe")
			WinClose
		else
			break
	}
	FileDelete, `%A_ScriptName`%
	ExitApp
)
Name_Of_The_Temporary_File := "Close CMD Window Entitled  ''TEST TITLE'' When Pressing Of ''a'' Is Detected.ahk"
FileAppend, %Code_For_The_Temporary_File%, *%Name_Of_The_Temporary_File%

Run, %Name_Of_The_Temporary_File%
User avatar
A Keymaker
Posts: 457
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: Temp AHK file does not get deleted and checks KeyState only once

07 Aug 2023, 04:21

Master_X wrote:
06 Aug 2023, 18:47
Does this work as you wish?
[...]
This one is only able to delete the temp file. CMD windows are not closed and is impossible to run it if Close CMD Window Entitled ''TEST TITLE'' When Pressing Of ''q'' Is Detected.ahk" already exists
Last edited by A Keymaker on 13 Aug 2023, 11:55, edited 1 time in total.
Master_X
Posts: 33
Joined: 04 Aug 2023, 13:59

Re: Temp AHK file does not get deleted and checks KeyState only once  Topic is solved

07 Aug 2023, 04:56

Then how about this

Code: Select all

#SingleInstance, Force
Name_Of_The_Temporary_File := "Close CMD Window Entitled  ''TEST TITLE'' When Pressing Of ''a'' Is Detected.ahk"

FileDelete, %Name_Of_The_Temporary_File%

Code_For_The_Temporary_File =
(
#Persistent
SetTitleMatchMode, 2

~q::
	Loop
	{	if WinExist("TEST TITLE ahk_exe cmd.exe")
			WinClose
		else
			break
	}
	FileDelete, `%A_ScriptName`%
	ExitApp
)

FileAppend, %Code_For_The_Temporary_File%, *%Name_Of_The_Temporary_File%

Run, %Name_Of_The_Temporary_File%
User avatar
A Keymaker
Posts: 457
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: Temp AHK file does not get deleted and checks KeyState only once

07 Aug 2023, 11:46

Firstly: I made a mistake, as this first version
Master_X wrote:
06 Aug 2023, 18:47
Does this work as you wish?
[...]
also closes all eligible CMD windows [but still has issue with running when temp file is already present, like after a crash of system]


Secondly: this
Master_X wrote:
07 Aug 2023, 04:56
Then how about this
[...]
seem to work A-OK


Although: if it deletes the temp file then it does it without sending it to the Recycle Bin. Even if I dumb it down to

Code: Select all

Name_Of_The_Temporary_File := "Close CMD Window Entitled  ''TEST TITLE'' When Pressing Of ''q'' Is Detected.ahk"

FileDelete, %Name_Of_The_Temporary_File%
then it also deletes the Close CMD Window Entitled ''TEST TITLE'' When Pressing Of ''q'' Is Detected.ahk file - but it is nowhere to be found afterwards. Its not a big thing but I always prefer to leave traces of what I do, because every couple of weeks I need to dig out some niche / obscure info, sometimes by digging in my Recycle Bin

Hence I created this test file

Code: Select all

#SingleInstance, Force

Name_Of_The_Temporary_File := "Close CMD Window Entitled  ''TEST TITLE'' When Pressing Of ''q'' Is Detected.ahk"

; Check if the Recycle Bin exists
EnvGet, sd, SystemDrive
rb := sd . "\$Recycle.Bin"

If !FileExist(rb) {
    ; Create the $RECYCLE.BIN folder
    FileCreateDir, %rb%
}

; Move the file to the $RECYCLE.BIN folder
file := A_ScriptDir . "\" . Name_Of_The_Temporary_File
FileMove, %file%, %rb%

ExitApp
so that it would move the file to the Recycle Bin but also create it if is not present - but it then moves it to literally e.g.

C:\$RECYCLE.BIN

and not my

C:\$RECYCLE.BIN\S-1-5-21-3797103458-798797042-3558267449-1001

But even if I did manage to make place it in that sub-folder then I would need to rework it to read this directory from the Security Identifier (SID), as on different systems it will be different


But of course there is FileRecycle that can be used - of which I was not aware in the beginning [hence I lost time on producing the above code]. And thus the whole OK working code looks like this:

Code: Select all

Name_Of_The_Temporary_File := "Close CMD Window Entitled  ''TEST TITLE'' When Pressing Of ''q'' Is Detected.ahk"

FileRecycle, %Name_Of_The_Temporary_File%   ; This cleans a leftover of the file [like after a system crash]

Code_For_The_Temporary_File =   ; Content of this will be dumped into the temp file
(
#Persistent
SetTitleMatchMode, 2

~q::
    Loop
    {    if WinExist("TEST TITLE ahk_exe cmd.exe")
            WinClose
        else
            break
    }

    FileRecycle, `%A_ScriptName`%   ; Deletes the file to the Recycle Bin. If that system folder is not present then Windows will create it
;;; FileDelete, %Name_Of_The_Temporary_File%   ; This version deletes the file but without sending it to the Recycle Bin

    ExitApp
)

FileAppend, %Code_For_The_Temporary_File%, *%Name_Of_The_Temporary_File%

Run, %Name_Of_The_Temporary_File%

So thank you very much for providing that code
Last edited by A Keymaker on 13 Aug 2023, 11:56, edited 3 times in total.
User avatar
A Keymaker
Posts: 457
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: Temp AHK file does not get deleted and checks KeyState only once

07 Aug 2023, 11:47

A Keymaker wrote:
07 Aug 2023, 11:46
[...]
the whole OK working code looks like this:
[...]
However there is one issue with it...


The above script [and other ones] is unable to push through to any window [e.g. of Notepad] if I run my other larger script which uses InputLevel 1 for every key and then InputLevel 0 just for [lets say for test reasons the already utilized here by me] q. Or to be precise: it is able but only after second pressing of q

But that is the whole point of this shebang here as I want to do just one pressing of q and make it do two things at the same time: to close that other larger script and to close CMD window - which that other larger script crates

In other words: the q is to serve as a switch off but I have to push it twice, because first pressing is seized by that larger script thus not applied to CMD window - thus I must press q second time in order to perform the Loop defined in the temp Close CMD Window Entitled ''TEST TITLE'' When Pressing Of ''q'' Is Detected file


My tests have shown that it is the usage of InputLevel in that larger script that is causing this semi-blockade - and I must use it because of other reasons I later on in that script must once again hotkey the same keys as in InputLevel 1 but this time normally i.e. without usage of InputLevel. [Long story short: this approach for achieving my end goals is explored in this topic: https://www.autohotkey.com/boards/viewtopic.php?f=76&t=120145]


So my question is [in this temp file approach]: is there a way to stop that larger AHK file from grabbing of first pressing of q just for it - and thus not creating a need to press it the second time at all?

I already have tried tricks like sending twice the same key and adding Sleep before, after and inside that Loop for closing of CMD windows - but apparently it always comes down that larger script somehow putting itself before other AHK scripts and programs [like Notepad]. And yes- both my scripts have the same privileges as I execute all of my scripts as Administrator
Last edited by A Keymaker on 13 Aug 2023, 11:56, edited 1 time in total.
User avatar
A Keymaker
Posts: 457
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: Temp AHK file does not get deleted and checks KeyState only once

13 Aug 2023, 11:49

A Keymaker wrote:
07 Aug 2023, 11:47
[...]
[...]
My tests have shown that it is the usage of InputLevel in that larger script that is causing this semi-blockade
[...]
Yes- and I have managed to deal with that issue: https://www.autohotkey.com/boards/viewtopic.php?f=76&t=119127&p=533490#p533490
User avatar
A Keymaker
Posts: 457
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: Temp AHK file does not get deleted and checks KeyState only once

13 Aug 2023, 12:33

A Keymaker wrote:
06 Aug 2023, 15:44
[...]
but the temp file still does not get deleted
[...]
I shall continue exploration of this issue in this topic https://www.autohotkey.com/boards/viewtopic.php?f=76&t=120347


Thank you for you help

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], peter_ahk and 142 guests