@Alan-Kilborn said in Remove “save file” prompt?:
STOP! You’re doing it wrong!
Every time you run that script, you put another callback in place.
So let’s explore this a bit further.
Create a script much like the earlier one in this thread:
# -*- coding: utf-8 -*-
from __future__ import print_function
from Npp import *
console.show()
print('user ran the script! (let us flash the screen for fun)')
editor.flash(250)
def BufferActivated(args):
print('system-generated buffer activated! tab:', notepad.getCurrentFilename().rsplit('\\', 1)[-1])
notepad.callback(BufferActivated, [NOTIFICATION.BUFFERACTIVATED])
Run it once via the Plugins > Python Script > Scripts menu. Then click on different tabs, get something like this as output in the PythonScript console window:
user ran the script! (let us flash the screen for fun)
system-generated buffer activated! tab: new 3
system-generated buffer activated! tab: new 1
system-generated buffer activated! tab: new 2
system-generated buffer activated! tab: new 3
All good. Now run the script 3 more times from the Plugins > Python Script > _Run Previous Script() _ menu.
Now select a few different tabs (I did same sequence as above) to obtain:
user ran the script! (let us flash the screen for fun)
user ran the script! (let us flash the screen for fun)
user ran the script! (let us flash the screen for fun)
system-generated buffer activated! tab: new 3
system-generated buffer activated! tab: new 3
system-generated buffer activated! tab: new 3
system-generated buffer activated! tab: new 3
system-generated buffer activated! tab: new 1
system-generated buffer activated! tab: new 1
system-generated buffer activated! tab: new 1
system-generated buffer activated! tab: new 1
system-generated buffer activated! tab: new 2
system-generated buffer activated! tab: new 2
system-generated buffer activated! tab: new 2
system-generated buffer activated! tab: new 2
system-generated buffer activated! tab: new 3
system-generated buffer activated! tab: new 3
system-generated buffer activated! tab: new 3
system-generated buffer activated! tab: new 3
As one can see, each time you switch a tab now, the callback gets called FOUR times! That’s because the script was run 4 times, and 4 separate callback functions were registered. It doesn’t matter that it was the “same” function that was registered each time.
How does one avoid/prevent this kind of thing from happening? Well, don’t run the script multiple times. But, accidents happen, so change the code to protect yourself. But how?
The changed script below shows how I like to do it (there are certainly other ways to achieve it):
# -*- coding: utf-8 -*-
from __future__ import print_function
from Npp import *
console.show()
print('user ran the script! (let us flash the screen for fun)')
editor.flash(250)
def BufferActivated(args):
print('system-generated buffer activated! tab:', notepad.getCurrentFilename().rsplit('\\', 1)[-1])
# --- vvv --- differences from original script are below --- vvv ---
try:
i_am_installed
except NameError:
notepad.callback(BufferActivated, [NOTIFICATION.BUFFERACTIVATED])
i_am_installed = True
else:
print('callback is already installed!')
The effect should be somewhat self-explanatory from the code. If you try the multiple runs experiment with this new version of the script, you shouldn’t see multiple buffer-activated processing when tabs are switched in the Notepad++ UI.