Do something when I close a file in Notepad

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
aurelius
Posts: 10
Joined: 23 Mar 2019, 18:11

Do something when I close a file in Notepad

12 Nov 2020, 17:39

Hello everybody

I want to create a script that start a process when I close a file opened in Notepad.

I wrote this one:

Code: Select all

#ifwinactive ahk_class Notepad
{
!f4::
winclose,a
msgbox you closed a notepad file
return
}
#ifwinactive
It works but only when I press alt+f4.

If I press
- the x button on top right
or
- alt+spacebar and then "Close"
nothing happens.

Any suggestion?
User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: Do something when I close a file in Notepad

12 Nov 2020, 18:02

Code: Select all

#Persistent
wTitle = ahk_exe notepad.exe
Loop {
 WinWait, %wTitle%
 WinWaitClose, %wTitle%
 MsgBox, 64, Done, Done!
}
Last edited by mikeyww on 12 Nov 2020, 18:03, edited 1 time in total.
User avatar
boiler
Posts: 16932
Joined: 21 Dec 2014, 02:44

Re: Do something when I close a file in Notepad

12 Nov 2020, 18:03

You can monitor the number of Notepad windows using the Count sub-command of WinGet and SetTimer, then act when the count decreases.
User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: Do something when I close a file in Notepad

12 Nov 2020, 18:05

Good point. The script that I posted works only after all of the instances have been closed.
User avatar
boiler
Posts: 16932
Joined: 21 Dec 2014, 02:44

Re: Do something when I close a file in Notepad

12 Nov 2020, 18:07

I think it will notify you if the one that it happened to identify (highest in the z-order when the script was run, I believe) is closed, even while the others are still open.

Edit: I am wrong about that. I thought you were assigning a specific ID. You are just assigning the generic process name info.
aurelius
Posts: 10
Joined: 23 Mar 2019, 18:11

Re: Do something when I close a file in Notepad

13 Nov 2020, 09:42

Thanks for your answers.

Here is my script:

Code: Select all

#Persistent
SetTimer,label,250
return

label:
winget,before,count,ahk_class Notepad
sleep 1000
winget,after,count,ahk_class Notepad
if (after < before)
msgbox you closed a notepad file

Is there a way to get the name of the file I close?
User avatar
boiler
Posts: 16932
Joined: 21 Dec 2014, 02:44

Re: Do something when I close a file in Notepad

13 Nov 2020, 10:18

You would need to keep a list of the titles in an associative array with the title as the key using WinGet (with List instead of Count) and WinGetTitle. Then your timer routine would be looking for which title is no longer there, then remove that element from the array.
User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: Do something when I close a file in Notepad

13 Nov 2020, 10:36

I agree. I'd not have two WinGet within the timer loop, but just check once, and compare to the last timed run. It's a simpler approach. You may otherwise run into some unexpected results. Depending on your timed loop's frequency, you might also need to accommodate the possibility that more than one window could close during the interval.
aurelius
Posts: 10
Joined: 23 Mar 2019, 18:11

Re: Do something when I close a file in Notepad

14 Nov 2020, 12:31

This one seems to work:

Code: Select all

#Persistent
SetTimer,label,250
return

label:
filedelete,list1.txt
filecopy,list2.txt,list1.txt
filedelete list2.txt

winget,mylist,list,ahk_class Notepad

loop %mylist%
{
num := mylist%a_index%
wingettitle,mytitle,ahk_id %num%
fileappend,%mytitle%`n,list2.txt
}

fileread,newlist,list2.txt
sleep 1000
loop,read,list1.txt
{
if instr(newlist,a_loopreadline)
continue
else
msgbox you closed "%a_loopreadline%"
}

Thanks for your help
User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: Do something when I close a file in Notepad

14 Nov 2020, 14:02

OK. Best wishes for success.

You're writing to a file on your disk every second. It's not necessarily a problem (in theory, shortens disk life, but may be hard to quantify), but you could simply handle this with strings (variables) if you wanted to do that as an alternative.

It's interesting to see what happens with threads when you have a timed loop containing a sleep. It might be the case that, with a sleep of 1000 ms, a timer interval of anything less (shorter) than this duration yields no difference in the approximate loop frequency of one second. For fun, you can try the following script, but predict what you think will happen before you run it (I already gave away the answer :) ).

Code: Select all

#Persistent
SetTimer, Test, 1
Return

Test:
SoundBeep, 1500, 20
Send x
Sleep, 1000
Send y
Return
Next, change the timer interval to 1000, and run it again.

I recommend ending your subroutine with Return.
User avatar
boiler
Posts: 16932
Joined: 21 Dec 2014, 02:44

Re: Do something when I close a file in Notepad

16 Nov 2020, 22:17

@aurelius - You might find OnWin() useful for this purpose. You can register the closing of Notepad windows in general as an event to trigger a function, and in that function, the event object will contain the title of the window that closed (among other things). So your script doesn’t have to use loops or timers or keep track of anything. It just waits for your function to be triggered.
aurelius
Posts: 10
Joined: 23 Mar 2019, 18:11

Re: Do something when I close a file in Notepad

17 Nov 2020, 18:27

I tried to use the OnWin example but the following message popped up:

Error at line 13.
Line Text: #Include <OnWin>
Error: Function library not found.
The program will exit.

What should I do?
User avatar
boiler
Posts: 16932
Joined: 21 Dec 2014, 02:44

Re: Do something when I close a file in Notepad

17 Nov 2020, 18:29

You need to download the OnWin library into your AHK user library location, or provide the full path to wherever you put it in your #Include statement.
aurelius
Posts: 10
Joined: 23 Mar 2019, 18:11

Re: Do something when I close a file in Notepad

17 Nov 2020, 18:54

I don't know how to download the library, it's something I've never done before.

Where can I find it and what have I got to write and where?
User avatar
boiler
Posts: 16932
Joined: 21 Dec 2014, 02:44

Re: Do something when I close a file in Notepad

17 Nov 2020, 19:42

At the OnWin() page, click on the OnWin.ahk link (the big title centered at the top of the post), which takes you to the GitHub page (you can just use the links provided in this sentence instead of going back to the earlier post).

In case you have trouble downloading from GitHub, here is a link within that GitHub page for the raw code page. Copy everything from that page and save to a file named OnWin.ahk, and place that file in a library location of your choice, then have the #Include directive point to your library file appropriate for where you saved it. If you get that far and it's still not finding it, post a message with what your #Include line looks like and where you saved the file.
aurelius
Posts: 10
Joined: 23 Mar 2019, 18:11

Re: Do something when I close a file in Notepad

20 Nov 2020, 16:37

I downloaded the library but I have trouble understanding how OnWin works.

I manipulated the example from the OnWin page and wrote this:

Code: Select all

#Include C:\Users\Admin\Documents\autohotkey\lib\onwin.ahk
#Persistent

OnWin("exist", ".txt" , Func("C"))
return

C(this)
{
OnWin("close", ".txt" , Func("D"))
return
}

D(this)
{
event := this.Event, window := this.WinTitle
msgbox %event%    %window%
return
}

If I have a single notepad file open and I close it, I get the message (but then if I open and close another file, nothing happens).

If there are more files open, I get the message only when I close the last one (but again it doesn't work anymore with the following ones).
User avatar
boiler
Posts: 16932
Joined: 21 Dec 2014, 02:44

Re: Do something when I close a file in Notepad

21 Nov 2020, 08:25

Sorry. After reading through more posts in that thread, I see that OnWin() isn’t registering shell hooks for individual windows. It is just using SetTimer and checking using standard approaches like WinExist(), which won’t help identify when individual windows of a general grouping are opened or closed. It does not appear to be as capable as I thought it was. I apologize for sending you down that path.
aurelius
Posts: 10
Joined: 23 Mar 2019, 18:11

Re: Do something when I close a file in Notepad

21 Nov 2020, 08:53

Anyway, thank you for your help, boiler.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333 and 356 guests